blob: f5689709138ab8073e1a5d0fd6361ff9cb4c5692 [file] [log] [blame]
Guido van Rossumde554ec1997-05-08 23:14:57 +00001#! /usr/bin/env python
2
3# Sanity checker for time.strftime
4
5import time, calendar, sys, string, os
Guido van Rossum228b8e81997-04-02 06:13:34 +00006from test_support import verbose
Guido van Rossum28d4ba21996-09-11 19:07:45 +00007
Guido van Rossumde554ec1997-05-08 23:14:57 +00008def main():
9 global verbose
10 now = time.time()
11 strftest(now)
12 verbose = 0
13 # Try a bunch of dates and times, chosen to vary through time of
14 # day and daylight saving time
15 for j in range(-5, 5):
16 for i in range(25):
17 strftest(now + (i + j*100)*23*3603)
18
19def strftest(now):
Guido van Rossum228b8e81997-04-02 06:13:34 +000020 if verbose:
Guido van Rossumde554ec1997-05-08 23:14:57 +000021 print "strftime test for", time.ctime(now)
22 nowsecs = int(now)
23 gmt = time.gmtime(now)
24 now = time.localtime(now)
Guido van Rossum28d4ba21996-09-11 19:07:45 +000025
Guido van Rossumde554ec1997-05-08 23:14:57 +000026 if now[3] < 12: ampm='AM'
27 else: ampm='PM'
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000028
Guido van Rossumde554ec1997-05-08 23:14:57 +000029 jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000030
Guido van Rossumde554ec1997-05-08 23:14:57 +000031 if now[8]: tz = time.tzname[1]
32 else: tz = time.tzname[0]
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000033
Guido van Rossumde554ec1997-05-08 23:14:57 +000034 if now[3] > 12: clock12 = now[3] - 12
35 elif now[3] > 0: clock12 = now[3]
36 else: clock12 = 12
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000037
Guido van Rossumde554ec1997-05-08 23:14:57 +000038 expectations = (
39 ('%A', calendar.day_name[now[6]], 'full weekday name'),
40 ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'),
41 ('%B', calendar.month_name[now[1]], 'full month name'),
42 ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'),
43 ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'),
44 ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'),
45 ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'),
46 ('%d', '%02d' % now[2], 'day of month as number (00-31)'),
47 ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'),
48 ('%H', '%02d' % now[3], 'hour (00-23)'),
49 ('%I', '%02d' % clock12, 'hour (01-12)'),
50 ('%j', '%03d' % now[7], 'julian day (001-366)'),
51 ('%M', '%02d' % now[4], 'minute, (00-59)'),
52 ('%m', '%02d' % now[1], 'month as number (01-12)'),
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 ('%S', '%02d' % now[5], 'seconds of current time (00-60)'),
58 ('%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' % ((now[7] + jan1[6])/7),
61 'week number of the year (Sun 1st)'),
62 ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7),
63 'week number of the year (Mon 1st)'),
64 ('%w', '%d' % ((1+now[6]) % 7), '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'),
71 )
Guido van Rossum5de1f8d1996-12-10 16:02:14 +000072
Guido van Rossumde554ec1997-05-08 23:14:57 +000073 nonstandard_expectations = (
74 ('%C', '%02d' % (now[0]/100), 'century'),
75 # This is for IRIX; on Solaris, %C yields date(1) format.
76 # Tough.
77 ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'),
78 ('%s', '%d' % nowsecs, 'seconds since the Epoch in UCT'),
79 ('%3y', '%03d' % (now[0]%100),
80 'year without century rendered using fieldwidth'),
81 ('%n', '\n', 'newline character'),
82 ('%t', '\t', 'tab character'),
83 )
Guido van Rossum228b8e81997-04-02 06:13:34 +000084
Guido van Rossumde554ec1997-05-08 23:14:57 +000085 if verbose:
86 print "Strftime test, platform: %s, Python version: %s" % \
87 (sys.platform, string.split(sys.version)[0])
Guido van Rossum228b8e81997-04-02 06:13:34 +000088
Guido van Rossumde554ec1997-05-08 23:14:57 +000089 for e in expectations:
90 try:
91 result = time.strftime(e[0], now)
92 except ValueError, error:
93 print "Standard '%s' format gave error:" % e[0], error
94 continue
95 if result == e[1]: continue
96 if result[0] == '%':
97 print "Does not support standard '%s' format (%s)" % (e[0], e[2])
98 else:
99 print "Conflict for %s (%s):" % (e[0], e[2])
100 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossum5de1f8d1996-12-10 16:02:14 +0000101
Guido van Rossumde554ec1997-05-08 23:14:57 +0000102 for e in nonstandard_expectations:
103 try:
104 result = time.strftime(e[0], now)
105 except ValueError, result:
106 if verbose:
107 print "Error for nonstandard '%s' format (%s): %s" % \
108 (e[0], e[2], str(error))
109 continue
110 if result == e[1]:
111 if verbose:
112 print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
113 elif result[0] == '%':
114 if verbose:
115 print "Does not appear to support '%s' format (%s)" % (e[0],
116 e[2])
117 else:
118 if verbose:
119 print "Conflict for nonstandard '%s' format (%s):" % (e[0],
120 e[2])
121 print " Expected %s, but got %s" % (e[1], result)
Guido van Rossum5de1f8d1996-12-10 16:02:14 +0000122
Guido van Rossumde554ec1997-05-08 23:14:57 +0000123def fixasctime(s):
124 if s[8] == ' ':
125 s = s[:8] + '0' + s[9:]
126 return s
Guido van Rossum5de1f8d1996-12-10 16:02:14 +0000127
Guido van Rossumde554ec1997-05-08 23:14:57 +0000128main()