blob: 288162cb80887a73911c554e76a2bafba9e989ed [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 Warsaw4eb01cd1996-12-13 18:07:07 +000012now = 850499890.282 # time.time()
13fromdate = 'Fri Dec 13 12:58:10 EST 1996' # os.popen('date')
14
15## now = time.time()
16## fp = os.popen('date')
17## fromdate = string.strip(fp.readline())
18## fp.close()
Guido van Rossum483705c1996-12-12 19:03:11 +000019nowsecs = int(now)
20gmt = time.gmtime(now)
21now = time.localtime(now)
22
23if gmt[3] < 12: ampm='AM'
24else: ampm='PM'
25
26jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
27wk1offset = jan1[6] - 6
28
29if now[8]: tz = time.tzname[1]
30else: tz = time.tzname[0]
31
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000032if now[3] > 12: clock12 = now[3] - 12
Guido van Rossum483705c1996-12-12 19:03:11 +000033else: clock12 = now[3]
34
35# descriptions are a mixture of those from the BSD/OS v2.0 man page
36# (known to be incorrect in some instances) and the documentation for
37# Python's time module
38
39expectations = (
40 ('%A', calendar.day_name[now[6]], 'full weekday name'),
41 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
42 ('%B', calendar.month_name[now[1]], 'full month name'),
43 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
44 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
45 ('%c', time.asctime(now), 'asctime() format'),
Guido van Rossum483705c1996-12-12 19:03:11 +000046 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
47 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
48 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
49 ('%H', '%02d' % now[3], 'hour (00-23)'),
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000050 ('%I', '%02d' % clock12, 'hour (01-12)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000051 ('%j', '%03d' % now[7], 'julian day (001-366)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000052 ('%M', '%02d' % now[4], 'minute, (00-59)'),
53 ('%m', '%02d' % now[1], 'month as number (01-12)'),
54 ('%n', '\n', 'newline character'),
55 ('%p', ampm, 'AM or PM as appropriate'),
56 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
57 ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
58 '%I:%M:%S %p'),
59 ('%t', '\t', 'tab character'),
60 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000061 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
62 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
63 ('%U', '%02d' % (1+(wk1offset+now[7])/7),
64 'week number of the year (Sun 1st)'),
65 ('%W', '%02d' % (1+now[7]/7), 'week number of the year (Mon 1st)'),
66 ('%w', '%d' % (1+now[6]), 'weekday as a number (Sun 1st)'),
67 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
68 '%m/%d/%y %H:%M:%S'),
69 ('%Y', '%d' % now[0], 'year with century'),
70 ('%y', '%02d' % (now[0]%100), 'year without century'),
71 ('%Z', tz, 'time zone name'),
72 ('%%', '%', 'single percent sign'),
Guido van Rossum15d10791996-12-12 19:07:19 +000073 )
74
75nonstandard_expectations = (
76 ('%C', fromdate, 'date(1) format'),
77 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
78 ('%s', '%d' % nowsecs, 'seconds since the Epoch in UCT'),
Guido van Rossum483705c1996-12-12 19:03:11 +000079 ('%3y', '%03d' % (now[0]%100),
80 'year without century rendered using fieldwidth'),
81 )
82
Guido van Rossum15d10791996-12-12 19:07:19 +000083if verbose:
84 print "Strftime test, platform: %s, Python version: %s" % \
85 (sys.platform, string.split(sys.version)[0])
86 expectations = expectations + nonstandard_expectations
Guido van Rossum483705c1996-12-12 19:03:11 +000087
88for e in expectations:
89 result = time.strftime(e[0], now)
90 if result == e[1]: continue
91 if result[0] == '%':
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000092 if verbose:
93 print "Does not appear to support '%s' format" % e[0]
Guido van Rossum483705c1996-12-12 19:03:11 +000094 else:
95 print "Conflict for %s (%s):" % (e[0], e[2])
96 print " Expected %s, but got %s" % (e[1], result)