Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. from uuid import UUID
  2. import time
  3. import datetime
  4.  
  5.  
  6. def shogal_uuid1(node, nanoseconds, clock_seq=None):
  7.     """Generate a UUID from a host ID, sequence number, and the current time.
  8.    If 'node' is not given, getnode() is used to obtain the hardware
  9.    address.  If 'clock_seq' is given, it is used as the sequence number;
  10.    otherwise a random 14-bit sequence number is chosen."""
  11.  
  12.  
  13.     # 0x01b21dd213814000 is the number of 100-ns intervals between the
  14.     # UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
  15.     timestamp = nanoseconds // 100 + 0x01b21dd213814000
  16.  
  17.     _last_timestamp = timestamp
  18.     if clock_seq is None:
  19.         import random
  20.         clock_seq = random.getrandbits(14) # instead of stable storage
  21.     time_low = timestamp & 0xffffffff
  22.     time_mid = (timestamp >> 32) & 0xffff
  23.     time_hi_version = (timestamp >> 48) & 0x0fff
  24.     clock_seq_low = clock_seq & 0xff
  25.     clock_seq_hi_variant = (clock_seq >> 8) & 0x3f
  26.     return UUID(fields=(time_low, time_mid, time_hi_version,
  27.                         clock_seq_hi_variant, clock_seq_low, node), version=1)
  28.  
  29. def main():
  30.     def parse_date(s):
  31.         _date, _time = s.split(' ')
  32.         year, month, day = map(int, _date.split('-'))
  33.         hours, minutes, seconds = map(int, _time.split('.')[0].split(':'))
  34.         subsecs = _time.split('.')[1]
  35.         if len(subsecs) < 9:
  36.             subsecs += '0' * (9 - len(subsecs))
  37.         subsecs = int(subsecs[:9])
  38.         secs_timestamp = int(datetime.datetime(year, month, day, hours, minutes, seconds).timestamp())
  39.         secs_timestamp *= 10 ** 9  # Set resolution to 100nanos
  40.         secs_timestamp += subsecs
  41.         return secs_timestamp
  42.     s= parse_date('2020-04-06 10:53:39.364780700')
  43.     print(s, type(s))
  44.  
  45.  
  46. if __name__ == '__main__':
  47.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement