home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyos2bin.zip / Lib / test / test_strftime.py < prev    next >
Text File  |  1997-12-15  |  4KB  |  135 lines

  1. #! /usr/bin/env python
  2.  
  3. # Sanity checker for time.strftime
  4.  
  5. import time, calendar, sys, string, os
  6. from test_support import verbose
  7.  
  8. def 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.  
  19. def strftest(now):
  20.     if verbose:
  21.     print "strftime test for", time.ctime(now)
  22.     nowsecs = str(long(now))[:-1]
  23.     gmt = time.gmtime(now)
  24.     now = time.localtime(now)
  25.  
  26.     if now[3] < 12: ampm='AM'
  27.     else: ampm='PM'
  28.  
  29.     jan1 = time.localtime(time.mktime((now[0], 1, 1) + (0,)*6))
  30.  
  31.     try:
  32.     if now[8]: tz = time.tzname[1]
  33.     else: tz = time.tzname[0]
  34.     except AttributeError:
  35.         tz = ''
  36.  
  37.     if now[3] > 12: clock12 = now[3] - 12
  38.     elif now[3] > 0: clock12 = now[3]
  39.     else: clock12 = 12
  40.  
  41.     expectations = (
  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)'),
  57.     ('%w', '%d' % ((1+now[6]) % 7), 'weekday as a number (Sun 1st)'),
  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.     )
  67.  
  68.     nonstandard_expectations = (
  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'),
  73.     ('(%Z)', '(%s)' % tz, 'time zone name'),
  74.  
  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.     )
  90.  
  91.     if verbose:
  92.     print "Strftime test, platform: %s, Python version: %s" % \
  93.           (sys.platform, string.split(sys.version)[0])
  94.  
  95.     for e in expectations:
  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
  101.     if result == e[1]: continue
  102.     if result[0] == '%':
  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)
  107.  
  108.     for e in nonstandard_expectations:
  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
  116.     if result == e[1]:
  117.         if verbose:
  118.         print "Supports nonstandard '%s' format (%s)" % (e[0], e[2])
  119.     elif result[0] == '%':
  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)
  128.  
  129. def fixasctime(s):
  130.     if s[8] == ' ':
  131.     s = s[:8] + '0' + s[9:]
  132.     return s
  133.  
  134. main()
  135.