import calendar,datetime

def GetNowYMD():
	now=datetime.datetime.now()
	nyear=now.year
	nmonth=now.month
	nday=now.day
	return (nyear,nmonth,nday)

def GetWeekSinceNow():
	now=datetime.datetime.today()
	nyear=now.year
	nmonth=now.month
	nday=now.day
	return GetWeekSince(nyear,nmonth,nday)

def GetWeekSince(nyear,nmonth,nday):
	dotm=calendar.monthrange(nyear,nmonth)[1]
	if nday + 7 <= dotm:
		return (nyear,nmonth,0,range(nday+1,nday+8))
	else:
		rlist=range(nday+1,dotm+1)
		rlist.extend(range(1,8-(dotm-nday)))
		return (nyear,nmonth,dotm-nday,rlist)

def GetBWeekSince(nyear,nmonth,nday):
	dotm=calendar.monthrange(nyear,nmonth)[1]
	if nday + 10 <= dotm:
		return (nyear,nmonth,0,range(nday+1,nday+11))
	else:
		rlist=range(nday+1,dotm+1)
		rlist.extend(range(1,11-(dotm-nday)))
		return (nyear,nmonth,dotm-nday,rlist)

def GetBWeekSinceYesterday():
	now=datetime.datetime.today()-datetime.timedelta(days=1)
	nyear=now.year
	nmonth=now.month
	nday=now.day
	return GetBWeekSince(nyear,nmonth,nday)

def GetTenDaysRangeSinceYesterday():
	opdt=datetime.date.today()-datetime.timedelta(days=1)
	eddt=datetime.date.today()+datetime.timedelta(days=9)
	return (opdt,eddt)

def GetDateoutRangeSinceYesterday():
	opdt=datetime.date(1,1,1)
	eddt=datetime.date.today()-datetime.timedelta(days=1)
	return (opdt,eddt)

def SafetyStrftime(datetime,fmt):
	try:
		return datetime.strftime(fmt)
	except:
		return ""