What was midnight yesterday as an epoch time?

前端 未结 7 1355
太阳男子
太阳男子 2020-12-09 03:08

I\'m trying to get my head around the datetime module. I know the time now as an epoch and the time an event last happened (as an epoch time). What I need to do is figure

相关标签:
7条回答
  • 2020-12-09 03:36
    import time
    
    start_str = time.strftime( "%m/%d/%Y" ) + " 00:00:00"
    end_str = time.strftime( "%m/%d/%Y ") + " 23:59:59"
    start_ts = int( time.mktime( time.strptime( start_str, "%m/%d/%Y %H:%M:%S" ) ) )
    end_ts = int( time.mktime( time.strptime( end_str, "%m/%d/%Y %H:%M:%S" ) ) )
    
    print (start_ts) # timestamp today at 00:00:00
    print (end_ts) # timestamp today at 23:59:59
    # 1552435200
    # 1552521599
    

    Source Python get unix epoch for today’s midnight and today’s 23:59:59 (start of day, end of day)

    0 讨论(0)
  • 2020-12-09 03:37

    Given such a timestamp, you can use divmod to compute the number of days since the epoch (which you don't care about), and how many seconds are leftover (which you do):

    days_since, remaining_seconds = divmod(t, 24*3600)  # Divide by number of seconds in one day
    

    Then, you subtract the leftover seconds from your original timestamp, which produces midnight of the current day.

    t -= remaining_seconds
    

    Rounding up is as simple as shifting your target timestamp forward exactly one day before rounding down.

    tomorrow_t = t + 24 * 3600
    days_since, remaining_seconds = divmod(tomorrow_t, 24*3600)
    t = tomorrow_t - remaining_seconds
    
    0 讨论(0)
  • 2020-12-09 03:43

    In the Middle of the Night

    Generating the last midnight is easy:

    from datetime import datetime, time
    
    midnight = datetime.combine(datetime.today(), time.min)
    

    That combines today's date (you can use date() or a datetime() instance, your pick), together with time.min to form a datetime object at midnight.

    Yesterday

    With a timedelta() you can calculate the previous midnight:

    from datetime import timedelta
    
    yesterday_midnight = midnight - timedelta(days=1)
    

    That Was Yesterday

    Now test if your timestamp is in between these two points:

    timestamp = datetime.fromtimestamp(some_timestamp_from_your_log)
    if yesterday_midnight <= timestamp < midnight:
        # this happened between 00:00:00 and 23:59:59 yesterday
    

    All Together Now

    Combined into one function:

    from datetime import datetime, time, timedelta
    
    def is_yesterday(timestamp):
        midnight = datetime.combine(datetime.today(), time.min)
        yesterday_midnight = midnight - timedelta(days=1)
        return yesterday_midnight <= timestamp < midnight:
    
    if is_yesterday(datetime.fromtimestamp(some_timestamp_from_your_log)):
        # ...
    
    0 讨论(0)
  • 2020-12-09 03:43

    To get the specific timezone's midnight timestamp:

    from datetime import datetime
    import pytz
    
    TZ = "Asia/Shanghai"
    datetime.now(pytz.timezone(TZ)).replace(hour=0, minute=0, second=0, microsecond=0).timestamp()
    
    0 讨论(0)
  • 2020-12-09 03:48

    You can use this code:

    import time
    
    seconds_of_day = 24 * 60 * 60  # 86400
    last_midnight = (round(time.time()) // seconds_of_day) * seconds_of_day
    yesterday_last_midnight = last_midnight - seconds_of_day
    
    0 讨论(0)
  • 2020-12-09 03:50

    Midnight at the start of today is:

    midnight = (int(time.time() // 86400)) * 86400
    

    so yesterday's midnight is:

    midnight = (int(time.time() // 86400)) * 86400 - 86400
    
    0 讨论(0)
提交回复
热议问题