What was midnight yesterday as an epoch time?

前端 未结 7 1366
太阳男子
太阳男子 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: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
    

提交回复
热议问题