blob: 93e7b66fad066c1696db72a8fd79916b63daa1c0 [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
Guido van Rossum2bde7831996-12-20 03:03:39 +00006from test_support import verbose
Guido van Rossum15d10791996-12-12 19:07:19 +00007
Barry Warsaw4c23b5f1996-12-13 18:08:58 +00008if verbose:
9 now = time.time()
10 fp = os.popen('date')
11 fromdate = string.strip(fp.readline())
12 fp.close()
13else:
14 now = 850499890.282 # time.time()
15 fromdate = 'Fri Dec 13 12:58:10 EST 1996' # os.popen('date')
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000016
Guido van Rossum483705c1996-12-12 19:03:11 +000017nowsecs = int(now)
18gmt = time.gmtime(now)
19now = time.localtime(now)
20
21if gmt[3] < 12: ampm='AM'
22else: ampm='PM'
23
24jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
25wk1offset = jan1[6] - 6
26
27if now[8]: tz = time.tzname[1]
28else: tz = time.tzname[0]
29
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000030if now[3] > 12: clock12 = now[3] - 12
Guido van Rossum483705c1996-12-12 19:03:11 +000031else: clock12 = now[3]
32
33# descriptions are a mixture of those from the BSD/OS v2.0 man page
34# (known to be incorrect in some instances) and the documentation for
35# Python's time module
36
37expectations = (
38 ('%A', calendar.day_name[now[6]], 'full weekday name'),
39 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
40 ('%B', calendar.month_name[now[1]], 'full month name'),
41 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
42 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
43 ('%c', time.asctime(now), 'asctime() format'),
Guido van Rossum483705c1996-12-12 19:03:11 +000044 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
45 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
46 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
47 ('%H', '%02d' % now[3], 'hour (00-23)'),
Barry Warsaw4eb01cd1996-12-13 18:07:07 +000048 ('%I', '%02d' % clock12, 'hour (01-12)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000049 ('%j', '%03d' % now[7], 'julian day (001-366)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000050 ('%M', '%02d' % now[4], 'minute, (00-59)'),
51 ('%m', '%02d' % now[1], 'month as number (01-12)'),
52 ('%n', '\n', 'newline character'),
53 ('%p', ampm, 'AM or PM as appropriate'),
54 ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'),
55 ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm),
56 '%I:%M:%S %p'),
57 ('%t', '\t', 'tab character'),
58 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
Guido van Rossum483705c1996-12-12 19:03:11 +000059 ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
60 ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'),
61 ('%U', '%02d' % (1+(wk1offset+now[7])/7),
62 'week number of the year (Sun 1st)'),
63 ('%W', '%02d' % (1+now[7]/7), 'week number of the year (Mon 1st)'),
64 ('%w', '%d' % (1+now[6]), 'weekday as a number (Sun 1st)'),
65 ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)),
66 '%m/%d/%y %H:%M:%S'),
67 ('%Y', '%d' % now[0], 'year with century'),
68 ('%y', '%02d' % (now[0]%100), 'year without century'),
69 ('%Z', tz, 'time zone name'),
70 ('%%', '%', 'single percent sign'),
Guido van Rossum15d10791996-12-12 19:07:19 +000071 )
72
73nonstandard_expectations = (
74 ('%C', fromdate, 'date(1) format'),
75 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
76 ('%s', '%d' % nowsecs, 'seconds since the Epoch in UCT'),
Guido van Rossum483705c1996-12-12 19:03:11 +000077 ('%3y', '%03d' % (now[0]%100),
78 'year without century rendered using fieldwidth'),
Guido van Rossum5eaf4571996-12-18 18:03:10 +000079 ('%n', '\n', 'newline character'),
80 ('%t', '\t', 'tab character'),
Guido van Rossum483705c1996-12-12 19:03:11 +000081 )
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])
Guido van Rossum483705c1996-12-12 19:03:11 +000086
87for e in expectations:
88 result = time.strftime(e[0], now)
89 if result == e[1]: continue
90 if result[0] == '%':
Guido van Rossum5eaf4571996-12-18 18:03:10 +000091 print "Does not support standard '%s' format (%s)" % (e[0], e[2])
Guido van Rossum483705c1996-12-12 19:03:11 +000092 else:
93 print "Conflict for %s (%s):" % (e[0], e[2])
94 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossum5eaf4571996-12-18 18:03:10 +000095
96for e in nonstandard_expectations:
97 result = time.strftime(e[0], now)
98 if result == e[1]:
99 if verbose:
100 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
101 elif result[0] == '%':
102 if verbose:
103 print "Does not appear to support '%s' format" % e[0]
104 else:
105 if verbose:
106 print "Conflict for %s (%s):" % (e[0], e[2])
107 print " Expected %s, but got %s" % (e[1], result)