get unix time from yesterday 0000 hrs to today 0000 hours using python
Question: I need to get the following values

    
    
    Today = 6th Feb
    time 1 = 5th Feb 0000 hrs
    time 2 = 6th Feb 0000 hrs.
    

So I have 24 hours in epoch time. The reference is today but not now()

So far I have this.

    
    
    yesterday = datetime.date.today() - datetime.timedelta(days=1)
    epoch_yesterday =  time.mktime(yesterday.timetuple())
    epoch_today = time.mktime(datetime.date.today().timetuple())
    

Both epoch_ values are actually returning seconds from now() like 1600 hrs
(depends on when I run the script) not from 0000/2400 hrs. I understand it
would be better to get yesterdays epoch time and then ad 24 hours to it to get
the end date. But I need to get the first part right :) , maybe I need sleep?

p.s. Sorry code styling isn't working, SO won't let me post with code styling
and was very frustrating to get SO to post this.


Answer: The simpler way might be to construct the date explicitly as so:

    
    
    now = datetime.now()
    previous_midnight = datetime.datetime( now.year, now.month, now.day )
    

That gets you the timestamp for the just passed midnight. As you already know
`time.mktime` will get you your the epoch value. Just add or subtract 24 * 60
* 60 to get the previous or next midnight from there.

Oh! And be aware that the epochal value will be seconds from midnight 1970,
Jan 1st **UTC**. If you need seconds from midnight in your timezone, remember
to adjust accordingly!

_Update_ : test code, executed from the shell at just before 1 pm, PST:

    
    
    >>> from datetime import datetime
    >>> import time
    >>> time_now = datetime.now()
    >>> time_at_start_of_today_local = datetime( n.year, n.month, n.day )
    >>> epochal_time_now = time.mktime( time_now.timetuple() )
    >>> epochal_time_at_start_of_today_local = time.mktime( time_at_start_of_today.timetuple() )
    >>> hours_since_start_of_day_today = (epochal_time_at_start_of_today_local - epochal_time_now) / 60 / 60
    12.975833333333332
    

Note: time_at_start_of_today_local is the number of seconds between 1970 Jan
1st, midnight, and the start of the current day _in your timezone_. If you
want the number of seconds to the start of the current day in UTC, then
subtract your time zone from time_at_start_of_today_local:

    
    
    >>> time_at_start_of_today_utc = time_at_start_of_today_local - time.timezone
    

Be aware that you get into odd territory here, specifically who's day do you
care about? If it's currently Tuesday in your timezone, but still Monday in
UTC, which midnight do you consider today's midnight for your purposes?

