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