NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

RFC3339.py (3168B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\datetimeutils\RFC3339.py
      2 from datetime import datetime, timedelta
      3 POSIX_EPOCH = datetime(1970, 1, 1, 0, 0)
      4 EPOCH_AS_FILETIME = 116444736000000000L
      5 HUNDREDS_OF_NANOSECONDS = 10000000
      6 HUNDREDS_OF_NANOS_TO_MS = 10000.0
      7 BLUETIME_STRP = '%Y-%m-%d %H:%M:%S'
      8 DTM_RFC_FORMAT = '%04d-%02d-%02dT%02d:%02d:%02d.%06dZ'
      9 DT_RFC_FORMAT = '%04d-%02d-%02dT%02d:%02d:%02dZ'
     10 D_RFC_FORMAT = '%04d-%02d-%02d'
     11 DATETIME_STRF = '%Y-%m-%dT%H:%M:%SZ'
     12 
     13 def blue_to_datetime_micro(blue_ts):
     14     if blue_ts is None:
     15         return
     16     dto = _blue_to_date_time(blue_ts)
     17     return DTM_RFC_FORMAT % (dto.year,
     18      dto.month,
     19      dto.day,
     20      dto.hour,
     21      dto.minute,
     22      dto.second,
     23      dto.microsecond)
     24 
     25 
     26 def blue_to_datetime(blue_ts):
     27     if blue_ts is None:
     28         return
     29     dto = _blue_to_date_time(blue_ts)
     30     return DT_RFC_FORMAT % (dto.year,
     31      dto.month,
     32      dto.day,
     33      dto.hour,
     34      dto.minute,
     35      dto.second)
     36 
     37 
     38 def blue_to_date(blue_ts):
     39     if blue_ts is None:
     40         return
     41     dto = _blue_to_date_time(blue_ts)
     42     return D_RFC_FORMAT % (dto.year, dto.month, dto.day)
     43 
     44 
     45 def blue_date_to_blue(blue_string):
     46     if blue_string is None:
     47         return
     48     blue_dt = datetime.strptime(blue_string, BLUETIME_STRP)
     49     unix_time = long((blue_dt - POSIX_EPOCH).total_seconds())
     50     return unix_to_blue(unix_time)
     51 
     52 
     53 def datetime_to_blue(dt_string):
     54     timestamp = None
     55     try:
     56         unix_time = _date_time_to_unix(dt_string)
     57         timestamp = unix_to_blue(unix_time)
     58     finally:
     59         return timestamp
     60 
     61 
     62 def date_to_blue(d_string):
     63     timestamp = None
     64     try:
     65         d_string = _remove_time_part(d_string)
     66         unix_time = _date_to_unix(d_string)
     67         timestamp = unix_to_blue(unix_time)
     68     finally:
     69         return timestamp
     70 
     71 
     72 def unix_to_blue(unix_time):
     73     return long(unix_time * HUNDREDS_OF_NANOSECONDS + EPOCH_AS_FILETIME)
     74 
     75 
     76 def blue_to_unix(blue_ts):
     77     return long((blue_ts - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOSECONDS)
     78 
     79 
     80 def blue_to_unix_ms(blue_ts):
     81     return (blue_ts - EPOCH_AS_FILETIME) / HUNDREDS_OF_NANOS_TO_MS
     82 
     83 
     84 def _blue_to_date_time(blue_ts):
     85     seconds_since_epoch, _nanoseconds = divmod(blue_ts - EPOCH_AS_FILETIME, HUNDREDS_OF_NANOSECONDS)
     86     return POSIX_EPOCH + timedelta(seconds=seconds_since_epoch, microseconds=_nanoseconds / 10)
     87 
     88 
     89 def _date_to_unix(dt_string):
     90     year, month, day = dt_string.split('-')
     91     dt = datetime(int(year), int(month), int(day), 0, 0, 0)
     92     return long((dt - POSIX_EPOCH).total_seconds())
     93 
     94 
     95 def _date_time_to_unix(dt_string):
     96     dt_string = _remove_ns_part(dt_string)
     97     date, time = dt_string.split('T')
     98     year, month, day = date.split('-')
     99     hour, minute, second = time[:-1].split(':')
    100     dt = datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
    101     return long((dt - POSIX_EPOCH).total_seconds())
    102 
    103 
    104 def _remove_time_part(d_string):
    105     ix = d_string.find('T')
    106     if ix >= 0:
    107         d_string = d_string[:ix]
    108     return d_string
    109 
    110 
    111 def _remove_ns_part(dt_string):
    112     ix = dt_string.find('.')
    113     if ix >= 0:
    114         dt_string = '%sZ' % dt_string[:ix]
    115     return dt_string