def verify_lunarcalendar(): ''' verify lunar calendar against data from HKO''' start = 1949 sql = 'select date, lunardate,jieqi from ical where date>=? and date<=?' while start < 2101: print 'compare %d' % start ystart = '%d-01-01' % start yend = '%d-12-31' % start res = query_db(sql, (ystart, yend)) hko = [] for x in res: if x[2]: hko.append((x[0], '%s %s' % (x[1], x[2]))) else: hko.append((x[0], x[1])) aalc = cn_lunarcal(start) for i in xrange(len(aalc)): aaday, aaldate = aalc[i]['date'], aalc[i]['lunardate'] if aalc[i]['jieqi']: aaldate = '%s %s' % (aalc[i]['lunardate'], aalc[i]['jieqi']) hkoday, hkoldate = hko[i] # print aaday, aaldate if aaday != hkoday or aaldate != hkoldate: print 'AA %s %s, HKO %s %s' % (aaday, aaldate, hkoday, hkoldate) start += 1
def verify_lunarcalendar(): ''' verify lunar calendar against data from HKO''' start = 1949 sql = 'select date, lunardate,jieqi from ical where date>=? and date<=?' while start < 2101: print 'compare %d' % start ystart = '%d-01-01' % start yend = '%d-12-31' % start res = query_db(sql, (ystart, yend)) hko = [] for x in res: if x[2]: hko.append((x[0], '%s %s' % (x[1], x[2]))) else: hko.append((x[0], x[1])) aalc = cn_lunarcal(start) for i in xrange(len(aalc)): aaday, aaldate = aalc[i]['date'], aalc[i]['lunardate'] if aalc[i]['jieqi']: aaldate = '%s %s' % (aalc[i]['lunardate'], aalc[i]['jieqi']) hkoday, hkoldate = hko[i] #print aaday, aaldate if aaday != hkoday or aaldate != hkoldate: print 'AA %s %s, HKO %s %s' % (aaday, aaldate, hkoday, hkoldate) start += 1
def gen_cal(start, end, fp): ''' generate lunar calendar in iCalendar format. Args: start and end date in ISO format, like 2010-12-31 fp: path to output file Return: none ''' startyear = int(start[:4]) endyear = int(end[:4]) if startyear > 1900 and endyear < 2101: # use Lunar Calendar from HKO print 'use Lunar Calendar from HKO' sql = ('select date, lunardate, holiday, jieqi from ical ' 'where date>=? and date<=? order by date') rows = query_db(sql, (start, end)) else: # compute Lunar Calendar by astronomical algorithm print 'compute Lunar Calendar by astronomical algorithm ' rows = [] for year in xrange(startyear, endyear + 1): row = cn_lunarcal(year) rows.extend(row) lines = [ICAL_HEAD] oneday = timedelta(days=1) mon = '' for r in rows: dt = datetime.strptime(r['date'], '%Y-%m-%d') if r['lunardate'] in CN_MON.keys(): mon = r['lunardate'] # ld = ['%s%s' % (lunaryear(r['date']), r['lunardate'])] ld = ['%s%s' % (mon, u'初一')] else: # ld = [r['lunardate']] ld = ['%s%s' % (mon, r['lunardate'])] # if r['holiday']: # ld.append(r['holiday']) # if r['jieqi']: # ld.append(r['jieqi']) uid = '*****@*****.**' % r['date'] summary = ' '.join(ld) utcstamp = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ') line = ICAL_SEC % (utcstamp, uid, dt.strftime('%Y%m%d'), (dt + oneday).strftime('%Y%m%d'), summary) lines.append(line.encode('utf8')) lines.append(ICAL_END) outputf = open(fp, 'w') outputf.write('\n'.join(lines)) outputf.close() print 'iCal lunar calendar from %s to %s saved to %s' % (start, end, fp)
def gen_cal_jieqi_only(start, end, fp): ''' generate Jieqi and Traditional Chinese in iCalendar format. Args: start and end date in ISO format, like 2010-12-31 fp: path to output file Return: none ''' startyear = int(start[:4]) endyear = int(end[:4]) if startyear > 1900 and endyear < 2101: # use Lunar Calendar from HKO print('use Lunar Calendar from HKO') sql = ('select date, lunardate, holiday, jieqi from ical ' 'where date>=? and date<=? order by date') rows = query_db(sql, (start, end)) else: # compute Lunar Calendar by astronomical algorithm print('compute Lunar Calendar by astronomical algorithm ') rows = [] for year in range(startyear, endyear + 1): row = cn_lunarcal(year) rows.extend(row) lines = [ICAL_HEAD] oneday = timedelta(days=1) for r in rows: dt = datetime.strptime(r['date'], '%Y-%m-%d') if not r['holiday'] and not r['jieqi']: continue ld = [] if r['holiday']: ld.append(r['holiday']) if r['jieqi']: ld.append(r['jieqi']) uid = '*****@*****.**' % r['date'] summary = ' '.join(ld) utcstamp = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ') line = ICAL_SEC % (utcstamp, uid, dt.strftime('%Y%m%d'), (dt + oneday).strftime('%Y%m%d'), summary) # lines.append(line.encode('utf8')) lines.append(line) lines.append(ICAL_END) outputf = open(fp, 'w') outputf.write('\n'.join(lines)) outputf.close() print( 'iCal Jieqi/Traditional Chinese holiday calendar from %s to %s saved to %s' % (start, end, fp))
def gen_cal_jieqi_only(start, end, fp): ''' generate Jieqi and Traditional Chinese in iCalendar format. Args: start and end date in ISO format, like 2010-12-31 fp: path to output file Return: none ''' startyear = int(start[:4]) endyear = int(end[:4]) if startyear > 1900 and endyear < 2101: # use Lunar Calendar from HKO print 'use Lunar Calendar from HKO' sql = ('select date, lunardate, holiday, jieqi from ical ' 'where date>=? and date<=? order by date') rows = query_db(sql, (start, end)) else: # compute Lunar Calendar by astronomical algorithm print 'compute Lunar Calendar by astronomical algorithm ' rows = [] for year in xrange(startyear, endyear + 1): row = cn_lunarcal(year) rows.extend(row) lines = [ICAL_HEAD] oneday = timedelta(days=1) for r in rows: dt = datetime.strptime(r['date'], '%Y-%m-%d') if not r['holiday'] and not r['jieqi']: continue ld = [] if r['holiday']: ld.append(r['holiday']) if r['jieqi']: ld.append(r['jieqi']) uid = '*****@*****.**' % r['date'] summary = ' '.join(ld) utcstamp = datetime.utcnow().strftime('%Y%m%dT%H%M%SZ') line = ICAL_SEC % (utcstamp, uid, dt.strftime('%Y%m%d'), (dt + oneday).strftime('%Y%m%d'), summary) lines.append(line.encode('utf8')) lines.append(ICAL_END) outputf = open(fp, 'w') outputf.write('\n'.join(lines)) outputf.close() print 'iCal Jieqi/Traditional Chinese holiday calendar from %s to %s saved to %s' % (start, end, fp)