How to store time without date in Python so it's convenient to compare?
Question: I have a simple task of storing a time in server's timezone and then comparing
if current time if after or before the saved time.

I tried to store the time as a "Jan 1st 2008" date with the time I need. The
problem is a daylight saving time gets in a way, if I save "16:00" on Jan 1st
and try to compare it with "16:00" on April 1st I get an one hour difference.
I want "16:00" to always mean current day's "16:00".

What would be an efficient and reliable way of storing and comparing time of
the day and ignoring the date? I need it to also be json-serializable, so I
can easily manipulate the value in python and JS if needed.


Answer: > I need it to also be json-serializable, so I can easily manipulate the value
> in python and JS if needed.

`"16:00"` string could be used to store the time (if you don't need seconds).

> I want "16:00" to always mean current day's "16:00".

To compare it with the current time:

    
    
    import time
    
    if time.strftime('%H:%M') < '16:00':
        print('before 4pm')
    else:
        print('after 4pm')
    

`%H`, `%M` produce zero-padded decimal numbers such as `07` and therefore the
string comparison works correctly. It works in the presence of DST transitions
too (as long as C `localtime()` works).

To parse it into time, datetime objects:

    
    
    from datetime import datetime
    
    four_pm_time = datetime.strptime('16:00', '%H:%M').time()
    four_pm = datetime.combine(datetime.now(), four_pm_time)
    

To get a timezone-aware datetime object:

    
    
    import tzlocal # $ pip install tzlocal
    
    four_pm_aware = tzlocal.get_localzone().localize(four_pm, is_dst=None)
    

[To get Unix time corresponding to 4pm (assuming `mktime()` works
here)](http://stackoverflow.com/a/36462784/4279):

    
    
    import time
    
    unix_time = time.mktime(time.localtime()[:3] + (16, 0, 0) + (-1,)*3)
    

