Converting datetime to POSIX time

后端 未结 6 1901
忘了有多久
忘了有多久 2021-01-31 02:14

How do I convert a datetime or date object into a POSIX timestamp in python? There are methods to create a datetime object out of a timestamp, but I don\'t seem to find any obv

相关标签:
6条回答
  • 2021-01-31 02:16

    In python, time.time() can return seconds as a floating point number that includes a decimal component with the microseconds. In order to convert a datetime back to this representation, you have to add the microseconds component because the direct timetuple doesn't include it.

    import time, datetime
    
    posix_now = time.time()
    
    d = datetime.datetime.fromtimestamp(posix_now)
    no_microseconds_time = time.mktime(d.timetuple())
    has_microseconds_time = time.mktime(d.timetuple()) + d.microsecond * 0.000001
    
    print posix_now
    print no_microseconds_time
    print has_microseconds_time
    
    0 讨论(0)
  • 2021-01-31 02:22

    It depends

    Is your datetime object timezone aware or naive?

    Timezone Aware

    If it is aware it's simple

    from datetime import datetime, timezone
    aware_date = datetime.now(tz=timezone.utc)
    posix_timestamp = aware_date.timestamp()
    

    as date.timestamp() gives you "POSIX timestamp"

    NOTE: more accurate to call it an epoch/unix timestamp as it may not be POSIX compliant

    Timezone Naive

    If it's not timezone aware (naive), then you'd need to know what timezone it was originally in so we can use replace() to convert it into a timezone aware date object. Let's assume that you've stored/retrieved it as UTC Naive. Here we create one, as an example:

    from datetime import datetime, timezone
    naive_date = datetime.utcnow()  # this date is naive, but is UTC based
    aware_date = naive_date.replace(tzinfo=timezone.utc)  # this date is no longer naive
    
    # now we do as we did with the last one
    
    posix_timestamp = aware_date.timestamp()
    

    It's always better to get to a timezone aware date as soon as you can to prevent issues that can arise with naive dates (as Python will often assume they are local times and can mess you up)

    NOTE: also be careful with your understanding of the epoch as it is platform dependent

    0 讨论(0)
  • 2021-01-31 02:29

    For UTC calculations, calendar.timegm is the inverse of time.gmtime.

    import calendar, datetime
    d = datetime.datetime.utcnow()
    print calendar.timegm(d.timetuple())
    
    0 讨论(0)
  • 2021-01-31 02:35

    Note that Python now (3.5.2) includes a built-in method for this in datetime objects:

    >>> import datetime
    >>> now = datetime.datetime(2020, 11, 18, 18, 52, 47, 874766)
    >>> now.timestamp() # Local time
    1605743567.874766
    >>> now.replace(tzinfo=datetime.timezone.utc).timestamp() # UTC
    1605725567.874766 # 5 hours delta (I'm in UTC-5)
    
    0 讨论(0)
  • 2021-01-31 02:37
    import time, datetime
    
    d = datetime.datetime.now()
    print time.mktime(d.timetuple())
    
    0 讨论(0)
  • 2021-01-31 02:37

    Best conversion from posix/epoch to datetime timestamp and the reverse:

    this_time = datetime.datetime.utcnow() # datetime.datetime type
    epoch_time = this_time.timestamp()      # posix time or epoch time
    this_time = datetime.datetime.fromtimestamp(epoch_time)
    
    0 讨论(0)
提交回复
热议问题