Project

General

Profile

1
# Date/time manipulation
2

    
3
import datetime
4
import dateutil.tz
5
import strings
6
import time
7

    
8
utc = dateutil.tz.tzutc()
9
local = dateutil.tz.tzlocal()
10

    
11
def naive2utc(datetime_):
12
    assert datetime_.tzinfo == None
13
    return datetime_.replace(tzinfo=utc)
14

    
15
def aware2local(datetime_): return datetime_.astimezone(local)
16

    
17
def from_timestamp(utc_timestamp):
18
    return naive2utc(datetime.datetime.utcfromtimestamp(utc_timestamp))
19

    
20
def timestamp(datetime_):
21
    datetime_ = aware2local(datetime_)
22
    return int(time.mktime(datetime_.timetuple())) + datetime_.microsecond/1e6
23

    
24
epoch = from_timestamp(0)
25

    
26
def total_seconds(timedelta_): return timestamp(epoch + timedelta_)
27

    
28
def now(): return datetime.datetime.now(utc)
29

    
30
def strftime(format, datetime_):
31
    '''datetime.strftime() can't handle years before 1900'''
32
    return (datetime_.replace(year=epoch.year, day=1).strftime(format
33
        .replace('%Y', '%%Y')
34
        .replace('%d', '%%d')
35
        )
36
        .replace('%Y', '%04d' % datetime_.year)
37
        .replace('%d', '%02d' % datetime_.day)
38
        )
39

    
40
def strtotime(str_, default=epoch):
41
    import dateutil.parser
42
    return dateutil.parser.parse(str_, default=default)
43

    
44
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
45

    
46
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
47

    
48
def parse_date_range(str_, range_sep='-', part_sep=' '):
49
    default = (str_, None)
50
    # range_sep might be used as date part separator instead
51
    if str_.find(part_sep) < 0: return default
52
    
53
    start, sep, end = str_.partition(range_sep)
54
    if sep == '': return default # not a range
55
    start, end = (strings.single_space(d).split(part_sep) for d in (start, end))
56
    
57
    # Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
58
    if len(start) == 2 and (len(end) == 1 or (
59
            len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
60
            and could_be_year(end[-1])
61
        )):
62
        end.insert(0, start[0]) # make end fully specified
63
    ct_diff = len(end) - len(start)
64
    # Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
65
    if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
66
    # Other forms are invalid and will be left as-is
67
    
68
    return tuple(part_sep.join(d) for d in (start, end))
(12-12/51)