tzparse.py 3.29 KB
Newer Older
1 2
"""Parse a timezone specification."""

3 4 5
# XXX Unfinished.
# XXX Only the typical form "XXXhhYYY;ddd/hh,ddd/hh" is currently supported.

6
tzpat = ('^([A-Z][A-Z][A-Z])([-+]?[0-9]+)([A-Z][A-Z][A-Z]);'
Tim Peters's avatar
Tim Peters committed
7
          '([0-9]+)/([0-9]+),([0-9]+)/([0-9]+)$')
8 9 10 11

tzprog = None

def tzparse(tzstr):
Tim Peters's avatar
Tim Peters committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    """Given a timezone spec, return a tuple of information
    (tzname, delta, dstname, daystart, hourstart, dayend, hourend),
    where 'tzname' is the name of the timezone, 'delta' is the offset
    in hours from GMT, 'dstname' is the name of the daylight-saving
    timezone, and 'daystart'/'hourstart' and 'dayend'/'hourend'
    specify the starting and ending points for daylight saving time."""
    global tzprog
    if tzprog is None:
        import re
        tzprog = re.compile(tzpat)
    match = tzprog.match(tzstr)
    if not match:
        raise ValueError, 'not the TZ syntax I understand'
    subs = []
    for i in range(1, 8):
        subs.append(match.group(i))
    for i in (1, 3, 4, 5, 6):
        subs[i] = eval(subs[i])
    [tzname, delta, dstname, daystart, hourstart, dayend, hourend] = subs
    return (tzname, delta, dstname, daystart, hourstart, dayend, hourend)
32

33
def tzlocaltime(secs, params):
Tim Peters's avatar
Tim Peters committed
34 35 36 37 38 39 40 41 42 43 44
    """Given a Unix time in seconds and a tuple of information about
    a timezone as returned by tzparse(), return the local time in the
    form (year, month, day, hour, min, sec, yday, wday, tzname)."""
    import time
    (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = params
    year, month, days, hours, mins, secs, yday, wday, isdst = \
            time.gmtime(secs - delta*3600)
    if (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend):
        tzname = dstname
        hours = hours + 1
    return year, month, days, hours, mins, secs, yday, wday, tzname
45 46

def tzset():
Tim Peters's avatar
Tim Peters committed
47 48 49 50 51 52 53 54 55
    """Determine the current timezone from the "TZ" environment variable."""
    global tzparams, timezone, altzone, daylight, tzname
    import os
    tzstr = os.environ['TZ']
    tzparams = tzparse(tzstr)
    timezone = tzparams[1] * 3600
    altzone = timezone - 3600
    daylight = 1
    tzname = tzparams[0], tzparams[2]
56

57
def isdst(secs):
Tim Peters's avatar
Tim Peters committed
58 59 60 61 62 63 64 65
    """Return true if daylight-saving time is in effect for the given
    Unix time in the current timezone."""
    import time
    (tzname, delta, dstname, daystart, hourstart, dayend, hourend) = \
            tzparams
    year, month, days, hours, mins, secs, yday, wday, isdst = \
            time.gmtime(secs - delta*3600)
    return (daystart, hourstart) <= (yday+1, hours) < (dayend, hourend)
66 67 68

tzset()

69
def localtime(secs):
Tim Peters's avatar
Tim Peters committed
70 71
    """Get the local time in the current timezone."""
    return tzlocaltime(secs, tzparams)
72 73

def test():
Tim Peters's avatar
Tim Peters committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    from time import asctime, gmtime
    import time, sys
    now = time.time()
    x = localtime(now)
    tm = x[:-1] + (0,)
    print 'now =', now, '=', asctime(tm), x[-1]
    now = now - now % (24*3600)
    if sys.argv[1:]: now = now + eval(sys.argv[1])
    x = gmtime(now)
    tm = x[:-1] + (0,)
    print 'gmtime =', now, '=', asctime(tm), 'yday =', x[-2]
    jan1 = now - x[-2]*24*3600
    x = localtime(jan1)
    tm = x[:-1] + (0,)
    print 'jan1 =', jan1, '=', asctime(tm), x[-1]
    for d in range(85, 95) + range(265, 275):
        t = jan1 + d*24*3600
        x = localtime(t)
        tm = x[:-1] + (0,)
        print 'd =', d, 't =', t, '=', asctime(tm), x[-1]