0

Currently working on a project where I need to manipulate a particular store's opening and closing times to UTC in Python. The opening and closing times are date independent.

For instance , I have a opening/closing times as

00:00:00
00:10:00

I'd like to convert this time to UTC

20:00:00 UTC
20:10:00 UTC

and compare the result with the store times noted in a database to identify if a store was open in the open/close times it should have been open in.

I've used the following code to convert these

    local_tmz = pytz.timezone(y["timezone_str"])
    time_str_start = x["start_time_local"]
    
    time_format = "%H:%M:%S"
    naive_start_time = datetime.strptime(time_str_start,time_format)
    local_start_time = local_tmz.localize(naive_start_time,is_dst=None)
    utc_start_time=local_start_time.astimezone(pytz.utc)
    print(utc_start_time.strftime(time_format),x["start_time_local"] ,y["timezone_str"])

y = Object containing timezone info of a particular store_id(In this case America/New_York)

x = Object containing opening/closing times of the particular store.

The output however is like so

4:56:00

The time conversion is off by an hour.

BJay
  • 1
  • 2
  • 2
    You'd need to start with 1) a complete date, not just a time and 2) a timezone. Without these two pieces of information, there's no converting anything to UTC. – deceze Apr 21 '23 at 10:26
  • once you got a full datetime with timezone ("aware datetime"), the conversion is straight-forward; e.g. `datetime.now(tz=ZoneInfo("Asia/Kolkata")).astimezone(ZoneInfo("UTC"))` – FObersteiner Apr 21 '23 at 10:32
  • @deceze The times are given for any particular day , thus there's no date attached to them , The pytz module doesnt throw any errors for only pushing through the time , but does make errors in the conversion. For instance , it converts 00:00:00 New_york time to 4:56:00 UTC but this conversion should have been 20:00:00 UTC – BJay Apr 21 '23 at 10:49
  • 2
    Again, you need date *and* time to convert between time zones. If you use `time_format = "%H:%M:%S"` to parse a string to datetime object, that object will have the default year-month-day of 1900-1-1 set. Timezone as we have them today simply did not exist back then. That's why you get the Local Mean Time offest for NY -> UTC of 4:56. – FObersteiner Apr 21 '23 at 11:21
  • UTC offsets change throughout the year based on daylight savings rules and other political changes. Thus you need a *specific point in time*, not a detached wall clock time alone if you want to convert it to any other timezone. – deceze Apr 21 '23 at 11:27
  • How could I go towards adding a date into the existing time string? Just do "dd:mm:yy" + "00:00:00" ? I'm a noob at python , thus this is very non-intuitive to me. – BJay Apr 21 '23 at 17:28
  • It’s not about adding something to satisfy a technical constraint. You logically need a time *at a specific day in a specific timezone* to find the equivalent UTC time. Is this information logically available to you in your case? If not, you cannot convert anything. Not on paper, not by asking someone, not in Python. – deceze Apr 21 '23 at 17:35
  • The info about the current timezone along with the start/end times is available , the date however is not available and maybe not necessary as well for my use case. The thing I want to achieve boiled down is this - convert the start time given into an equivalent utc time (just by subtracting the time-delta between the two timezones : For e.g. 00:00:00 today would be 20:00:00 yesterday in UTC) to be used further. – BJay Apr 21 '23 at 17:46
  • If your timezones never change DST and also won’t change for other political reasons, then this is possible. Otherwise it will be 20:00 in summer and, say, 21:00 in winter. – deceze Apr 21 '23 at 18:13
  • 1
    Yes , I've solved the problem by essentially assuming today's date for conversion. I really value your inputs @deceze , learnt a lot today! – BJay Apr 21 '23 at 20:25

0 Answers0