1

For the following code, I expect the seconds_input and seconds_output, to be the same. But I get an hour difference:

import time

seconds_input = 1571875186
date_struct1 = time.gmtime(seconds_input)

tm_str = time.strftime("%Y-%m-%d %H:%M:%S", date_struct1)
date_struct2 = time.strptime(tm_str, '%Y-%m-%d %H:%M:%S')
seconds_output = time.mktime(date_struct2)

print(seconds_input - seconds_output)
print(date_struct1, date_struct2)

I assume it could be due to tm_isdst=-1 in the date_struct2, but I'm not sure how to set it to 0.

I have tried the date_struct2.tm_isdst = 0, but got the following error:

AttributeError: readonly attribute

Update:

if the seconds_input corresponds to the date in the future, then the seconds_input and seconds_output would be the same.

For example if seconds_input = 1673084786.

Gооd_Mаn
  • 343
  • 1
  • 16

1 Answers1

0

In my case (GMT+9) I have got 9 hours difference.

So I suppose you get the difference between your own TZ and GMT -- just as it should be.

To convert properly you have to add (or subtract) 3600sec, which is 1 hour.

lenik
  • 23,228
  • 4
  • 34
  • 43
  • Thank you for your answer. I've done it before and it wouldn't work if the time (seconds_input) corresponds to the date in the future. For example if seconds_input = 1673084786. – Gооd_Mаn Nov 01 '19 at 16:14