| 
716
 | 
     1 #!/usr/bin/env python
 | 
| 
 | 
     2 # -*- coding: utf-8 -*-
 | 
| 
 | 
     3 
 | 
| 
 | 
     4 """
 | 
| 
 | 
     5 parse date
 | 
| 
 | 
     6 """
 | 
| 
 | 
     7 
 | 
| 
 | 
     8 # imports
 | 
| 
 | 
     9 import argparse
 | 
| 
 | 
    10 import calendar
 | 
| 
 | 
    11 import datetime
 | 
| 
 | 
    12 import sys
 | 
| 
 | 
    13 import time
 | 
| 
 | 
    14 from .formatting import format_table
 | 
| 
 | 
    15 from dateutil.parser import parse
 | 
| 
 | 
    16 
 | 
| 
 | 
    17 
 | 
| 
 | 
    18 __all__ = ['main', 'parse_date', 'epoch2local', 'epoch2utc', 'is_dst', 'timezone']
 | 
| 
 | 
    19 
 | 
| 
 | 
    20 
 | 
| 
 | 
    21 def is_dst(localtime=None):
 | 
| 
 | 
    22     """returns if daylight savings time is in effect locally"""
 | 
| 
 | 
    23     return time.localtime(localtime).tm_isdst > 0
 | 
| 
 | 
    24 
 | 
| 
 | 
    25 
 | 
| 
 | 
    26 def timezone(localtime=None):
 | 
| 
 | 
    27     """returns name of local timezone"""
 | 
| 
 | 
    28     return time.tzname[int(is_dst(localtime))]
 | 
| 
 | 
    29 
 | 
| 
 | 
    30 
 | 
| 
 | 
    31 def epoch2local(datestamp):
 | 
| 
 | 
    32     """convert epoch to local time"""
 | 
| 
 | 
    33     return datetime.datetime.fromtimestamp(float(datestamp))
 | 
| 
 | 
    34 
 | 
| 
 | 
    35 
 | 
| 
 | 
    36 def epoch2utc(datestamp):
 | 
| 
 | 
    37     """convert epoch to UTC"""
 | 
| 
 | 
    38     return datetime.datetime.utcfromtimestamp(float(datestamp))
 | 
| 
 | 
    39 
 | 
| 
 | 
    40 
 | 
| 
 | 
    41 def parse_date(datestamp, utc=False):
 | 
| 
 | 
    42     """returns seconds since epoch from the supplied date"""
 | 
| 
 | 
    43 
 | 
| 
 | 
    44     try:
 | 
| 
 | 
    45         # already epoch timestamp
 | 
| 
 | 
    46         return float(datestamp)
 | 
| 
 | 
    47     except ValueError:
 | 
| 
 | 
    48         pass
 | 
| 
 | 
    49 
 | 
| 
 | 
    50     # parse the string
 | 
| 
 | 
    51     parsed_date = parse(datestamp)
 | 
| 
 | 
    52 
 | 
| 
 | 
    53     # convert this to seconds since epoch
 | 
| 
 | 
    54     if utc:
 | 
| 
 | 
    55         return float(calendar.timegm(parsed_date.timetuple()))
 | 
| 
 | 
    56     else:
 | 
| 
 | 
    57         return time.mktime(parsed_date.timetuple())
 | 
| 
 | 
    58 
 | 
| 
 | 
    59 
 | 
| 
 | 
    60 def main(args=sys.argv[1:]):
 | 
| 
 | 
    61 
 | 
| 
 | 
    62     # parse command line
 | 
| 
 | 
    63     parser = argparse.ArgumentParser(description=__doc__)
 | 
| 
 | 
    64     parser.add_argument('date', nargs='+',
 | 
| 
 | 
    65                         help="local date to parse")
 | 
| 
 | 
    66     parser.add_argument('--utc', dest='utc',
 | 
| 
 | 
    67                         action='store_true', default=False,
 | 
| 
 | 
    68                         help="indicate date is in UTC")
 | 
| 
 | 
    69     options = parser.parse_args(args)
 | 
| 
 | 
    70 
 | 
| 
 | 
    71     # parse each date
 | 
| 
 | 
    72     epochs = [parse_date(d, options.utc) for d in options.date]
 | 
| 
 | 
    73 
 | 
| 
 | 
    74     # display results
 | 
| 
 | 
    75     header = ['epoch', 'local', 'UTC']
 | 
| 
 | 
    76     print (format_table([[d, '{} {}'.format(epoch2local(d), timezone(d)), epoch2utc(d)] for d in epochs],
 | 
| 
 | 
    77                         header=header, joiner='|'))
 | 
| 
 | 
    78 
 | 
| 
 | 
    79 
 | 
| 
 | 
    80 if __name__ == '__main__':
 | 
| 
 | 
    81     main()
 |