blob: ca0e48e397ac9434bf350e7614e90dd52e6b461c [file] [log] [blame]
Guido van Rossum483705c1996-12-12 19:03:11 +00001#! /usr/bin/env python
2
3# Sanity checker for time.strftime
4
5import time, calendar, sys, string, os
6
Guido van Rossum15d10791996-12-12 19:07:19 +00007verbose = 0
8
9if __name__ == '__main__':
10 verbose = 1
11
Barry Warsaw4c23b5f1996-12-13 18:08:58 +000012if verbose:
13 now = time.time()
14 fp = os.popen('date')
15 fromdate = string.strip(fp.readline())
16 fp.close()
17else:
18 now = 850499890.282 # time.time()
19 fromdate = 'Fri Dec 13 12:58:10 EST 1996' # os.popen('date')
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000020
Guido van Rossum483705c1996-12-12 19:03:11 +000021nowsecs = int(now)
22gmt = time.gmtime(now)
23now = time.localtime(now)
24
25if gmt[3] < 12: ampm='AM'
26else: ampm='PM'
27
28jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
29wk1offset = jan1[6] - 6
30
31if now[8]: tz = time.tzname[1]
32else: tz = time.tzname[0]
33
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000034if now[3] > 12: clock12 = now[3] - 12
Guido van Rossum483705c1996-12-12 19:03:11 +000035else: clock12 = now[3]
36
37# descriptions are a mixture of those from the BSD/OS v2.0 man page
38# (known to be incorrect in some instances) and the documentation for
39# Python's time module
40
41expectations = (
42 ('%A', calendar.day_name[now[6]], 'full weekday name'),
43 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
44 ('%B', calendar.month_name[now[1]], 'full month name'),
45 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
46 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
47 ('%c', time.asctime(now), 'asctime() format'),
Guido van Rossum483705c1996-12-12 19:03:11 +000048 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
49 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
50 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
51 ('%H', '%02d' % now[3], 'hour (00-23)'),
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000052 ('%I', '%02d' % clock12, 'hour (01-12)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000053 ('%j', '%03d' % now[7], 'julian day (001-366)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000054 ('%M', '%02d' % now[4], 'minute, (00-59)'),
55 ('%m', '%02d' % now[1], 'month as number (01-12)'),
56 ('%n', '\n', 'newline character'),
57 ('%p', ampm, 'AM or PM as appropriate'),
58 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
59 ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
60 '%I:%M:%S %p'),
61 ('%t', '\t', 'tab character'),
62 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000063 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
64 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
65 ('%U', '%02d' % (1+(wk1offset+now[7])/7),
66 'week number of the year (Sun 1st)'),
67 ('%W', '%02d' % (1+now[7]/7), 'week number of the year (Mon 1st)'),
68 ('%w', '%d' % (1+now[6]), 'weekday as a number (Sun 1st)'),
69 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
70 '%m/%d/%y %H:%M:%S'),
71 ('%Y', '%d' % now[0], 'year with century'),
72 ('%y', '%02d' % (now[0]%100), 'year without century'),
73 ('%Z', tz, 'time zone name'),
74 ('%%', '%', 'single percent sign'),
Guido van Rossum15d10791996-12-12 19:07:19 +000075 )
76
77nonstandard_expectations = (
78 ('%C', fromdate, 'date(1) format'),
79 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
80 ('%s', '%d' % nowsecs, 'seconds since the Epoch in UCT'),
Guido van Rossum483705c1996-12-12 19:03:11 +000081 ('%3y', '%03d' % (now[0]%100),
82 'year without century rendered using fieldwidth'),
83 )
84
Guido van Rossum15d10791996-12-12 19:07:19 +000085if verbose:
86 print "Strftime test, platform: %s, Python version: %s" % \
87 (sys.platform, string.split(sys.version)[0])
88 expectations = expectations + nonstandard_expectations
Guido van Rossum483705c1996-12-12 19:03:11 +000089
90for e in expectations:
91 result = time.strftime(e[0], now)
92 if result == e[1]: continue
93 if result[0] == '%':
Barry Warsaw2cc81631996-12-13 18:12:34 +000094 print "Does not appear to support '%s' format" % e[0]
Guido van Rossum483705c1996-12-12 19:03:11 +000095 else:
96 print "Conflict for %s (%s):" % (e[0], e[2])
97 print " Expected %s, but got %s" % (e[1], result)