Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | # Sanity checker for time.strftime |
| 4 | |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 5 | import time, calendar, sys, string, os, re |
Guido van Rossum | 2bde783 | 1996-12-20 03:03:39 +0000 | [diff] [blame] | 6 | from test_support import verbose |
Guido van Rossum | 15d1079 | 1996-12-12 19:07:19 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 8 | def main(): |
| 9 | global verbose |
Barry Warsaw | 4c23b5f | 1996-12-13 18:08:58 +0000 | [diff] [blame] | 10 | now = time.time() |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 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): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 16 | for i in range(25): |
| 17 | strftest(now + (i + j*100)*23*3603) |
Barry Warsaw | 4eb01cd | 1996-12-13 18:07:07 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 19 | def strftest(now): |
| 20 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 21 | print "strftime test for", time.ctime(now) |
Guido van Rossum | 3f11da0 | 1997-05-16 13:51:48 +0000 | [diff] [blame] | 22 | nowsecs = str(long(now))[:-1] |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 23 | gmt = time.gmtime(now) |
| 24 | now = time.localtime(now) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 26 | if now[3] < 12: ampm='AM' |
| 27 | else: ampm='PM' |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 29 | jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6)) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 9d9af2c | 1997-08-12 18:21:08 +0000 | [diff] [blame] | 31 | try: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 32 | if now[8]: tz = time.tzname[1] |
| 33 | else: tz = time.tzname[0] |
Guido van Rossum | 9d9af2c | 1997-08-12 18:21:08 +0000 | [diff] [blame] | 34 | except AttributeError: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 35 | tz = '' |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 37 | if now[3] > 12: clock12 = now[3] - 12 |
| 38 | elif now[3] > 0: clock12 = now[3] |
| 39 | else: clock12 = 12 |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 41 | expectations = ( |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 42 | ('%a', calendar.day_abbr[now[6]], 'abbreviated weekday name'), |
| 43 | ('%A', calendar.day_name[now[6]], 'full weekday name'), |
| 44 | ('%b', calendar.month_abbr[now[1]], 'abbreviated month name'), |
| 45 | ('%B', calendar.month_name[now[1]], 'full month name'), |
| 46 | # %c see below |
| 47 | ('%d', '%02d' % now[2], 'day of month as number (00-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[1], 'month as number (01-12)'), |
| 52 | ('%M', '%02d' % now[4], 'minute, (00-59)'), |
| 53 | ('%p', ampm, 'AM or PM as appropriate'), |
| 54 | ('%S', '%02d' % now[5], 'seconds of current time (00-60)'), |
| 55 | ('%U', '%02d' % ((now[7] + jan1[6])/7), |
| 56 | 'week number of the year (Sun 1st)'), |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 57 | ('%w', '0?%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'), |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 58 | ('%W', '%02d' % ((now[7] + (jan1[6] - 1)%7)/7), |
| 59 | 'week number of the year (Mon 1st)'), |
| 60 | # %x see below |
| 61 | ('%X', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), |
| 62 | ('%y', '%02d' % (now[0]%100), 'year without century'), |
| 63 | ('%Y', '%d' % now[0], 'year with century'), |
| 64 | # %Z see below |
| 65 | ('%%', '%', 'single percent sign'), |
| 66 | ) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 68 | nonstandard_expectations = ( |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 69 | # These are standard but don't have predictable output |
| 70 | ('%c', fixasctime(time.asctime(now)), 'near-asctime() format'), |
| 71 | ('%x', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), |
| 72 | '%m/%d/%y %H:%M:%S'), |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 73 | ('%Z', '%s' % tz, 'time zone name'), |
Guido van Rossum | 2b41fdc | 1997-08-14 22:23:42 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 75 | # These are some platform specific extensions |
| 76 | ('%D', '%02d/%02d/%02d' % (now[1], now[2], (now[0]%100)), 'mm/dd/yy'), |
| 77 | ('%e', '%2d' % now[2], 'day of month as number, blank padded ( 0-31)'), |
| 78 | ('%h', calendar.month_abbr[now[1]], 'abbreviated month name'), |
| 79 | ('%k', '%2d' % now[3], 'hour, blank padded ( 0-23)'), |
| 80 | ('%n', '\n', 'newline character'), |
| 81 | ('%r', '%02d:%02d:%02d %s' % (clock12, now[4], now[5], ampm), |
| 82 | '%I:%M:%S %p'), |
| 83 | ('%R', '%02d:%02d' % (now[3], now[4]), '%H:%M'), |
| 84 | ('%s', nowsecs, 'seconds since the Epoch in UCT'), |
| 85 | ('%t', '\t', 'tab character'), |
| 86 | ('%T', '%02d:%02d:%02d' % (now[3], now[4], now[5]), '%H:%M:%S'), |
| 87 | ('%3y', '%03d' % (now[0]%100), |
| 88 | 'year without century rendered using fieldwidth'), |
| 89 | ) |
Guido van Rossum | 15d1079 | 1996-12-12 19:07:19 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 91 | if verbose: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 92 | print "Strftime test, platform: %s, Python version: %s" % \ |
| 93 | (sys.platform, string.split(sys.version)[0]) |
Guido van Rossum | 483705c | 1996-12-12 19:03:11 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 95 | for e in expectations: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 96 | try: |
| 97 | result = time.strftime(e[0], now) |
| 98 | except ValueError, error: |
| 99 | print "Standard '%s' format gave error:" % e[0], error |
| 100 | continue |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 101 | if re.match(e[1], result): continue |
Guido van Rossum | 9b11279 | 1999-04-08 17:23:11 +0000 | [diff] [blame] | 102 | if not result or result[0] == '%': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 103 | print "Does not support standard '%s' format (%s)" % (e[0], e[2]) |
| 104 | else: |
| 105 | print "Conflict for %s (%s):" % (e[0], e[2]) |
| 106 | print " Expected %s, but got %s" % (e[1], result) |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 107 | |
| 108 | for e in nonstandard_expectations: |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 109 | try: |
| 110 | result = time.strftime(e[0], now) |
| 111 | except ValueError, result: |
| 112 | if verbose: |
| 113 | print "Error for nonstandard '%s' format (%s): %s" % \ |
| 114 | (e[0], e[2], str(result)) |
| 115 | continue |
Guido van Rossum | 7944ea5 | 1998-09-14 15:50:40 +0000 | [diff] [blame] | 116 | if re.match(e[1], result): |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 117 | if verbose: |
| 118 | print "Supports nonstandard '%s' format (%s)" % (e[0], e[2]) |
Guido van Rossum | 0b7dd08 | 1999-04-08 20:22:46 +0000 | [diff] [blame] | 119 | elif not result or result[0] == '%': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 120 | if verbose: |
| 121 | print "Does not appear to support '%s' format (%s)" % (e[0], |
| 122 | e[2]) |
| 123 | else: |
| 124 | if verbose: |
| 125 | print "Conflict for nonstandard '%s' format (%s):" % (e[0], |
| 126 | e[2]) |
| 127 | print " Expected %s, but got %s" % (e[1], result) |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 128 | |
| 129 | def fixasctime(s): |
| 130 | if s[8] == ' ': |
Guido van Rossum | 41360a4 | 1998-03-26 19:42:58 +0000 | [diff] [blame] | 131 | s = s[:8] + '0' + s[9:] |
Guido van Rossum | e69be3e | 1997-03-07 20:30:03 +0000 | [diff] [blame] | 132 | return s |
| 133 | |
| 134 | main() |