1

When coding on my MacBook recently, I wrote the following code:

from datetime import datetime
date = datetime(1969, 1, 1)
value = date.timestamp()

On my MacBook everything worked fine and value was equal to -31539600.0, but when I switch to my Windows PC, the following Error is thrown: OSError: [Errno 22] Invalid argument. Obviously, it has to do with the fact, that Unix time only starts in 1970. But still I wonder why it then works on MacOs.

By the way: Python Version 3.9.7

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
TheUltimateOptimist
  • 1,089
  • 4
  • 19
  • always put full error message (starting at word "Traceback") in question (not in comments) as text (not screenshot, not link to external portal). There are other useful information. – furas Jun 17 '22 at 14:35
  • 2
    this code works also on Linux Mint (based on Ubuntu). Different systems may use different (native) code - and it seems Unix/LInux allows for negative values but Windows doesn't allow for negative values. – furas Jun 17 '22 at 14:37
  • macOS is a Unix (and certified as such). – chepner Jun 17 '22 at 15:31
  • it *does* work on Windows, but you have to set a time zone (tzinfo), see e.g. [here](https://stackoverflow.com/a/71683231/10197418) – FObersteiner Jun 17 '22 at 15:55
  • related: [Why is Datetime's `.timestamp()` method returning `OSError: [Errno 22] Invalid argument`?](https://stackoverflow.com/q/71680355/10197418) – FObersteiner Jun 17 '22 at 16:02
  • @chepner macOS is a Unix, but it's not a Linux. The difference may be significant. – Mark Ransom Jun 17 '22 at 19:22
  • I don't see why, since Linux wasn't mentioned anywhere in the question. – chepner Jun 17 '22 at 19:26
  • @chepner someone else came up with the "works on Linux", and I wanted to point out that just because they're both Unix-like you can't use one to draw conclusions about the other. And is there a definition of Unix other than Posix? – Mark Ransom Jun 22 '22 at 02:25

1 Answers1

1

datetime.timestamp()

Note There is no method to obtain the POSIX timestamp directly from a naive datetime instance representing UTC time. If your application uses this convention and your system timezone is not set to UTC, you can obtain the POSIX timestamp by supplying tzinfo=timezone.utc:

timestamp = dt.replace(tzinfo=timezone.utc).timestamp()

or by calculating the timestamp directly:

timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

Applied to your code:

from datetime import datetime, timezone, timedelta
date = datetime(1969, 1, 1)

value = -31539600.0           # value from the question

# my workaround
stamp0 = (date - datetime(1970, 1, 1)).total_seconds()
print('stamp0', stamp0, stamp0 - value)

# from the docs
stamp1 = date.replace(tzinfo=timezone.utc).timestamp()
print('stamp1', stamp1, stamp1 - value)

# from the docs (alternative)
stamp2 = (date - datetime(1970, 1, 1)) / timedelta(seconds=1)
print('stamp2', stamp2, stamp2 - value)

# compensate the one-hour difference (example) 
stampx = (date - datetime(1970, 1, 1, 1)) / timedelta(seconds=1)
print('stampx', stampx, stampx - value)

Output: .\SO\72660402.py

stamp0 -31536000.0 3600.0
stamp1 -31536000.0 3600.0
stamp2 -31536000.0 3600.0
stampx -31539600.0 0.0
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Hard-coding a 1-hour difference seems error-prone to me - if the machine you run this on is set to a time zone that has DST, the offset might change, no? – FObersteiner Jun 18 '22 at 15:26
  • @FObersteiner Sure. I don't aspire to give an universal solution (just an *example*, see the inline comment…). [A **naive** object does not contain enough information to unambiguously locate itself relative to other date/time objects.](https://docs.python.org/3.10/library/datetime.html#aware-and-naive-objects) – JosefZ Jun 18 '22 at 19:40
  • I understand, my comment is a bit "nit-picky". Nevertheless, in general I think it's important to empathize the "naive = local time" behavior (which is the root of the error in question!). From the amout of questions around this topic here on SO, I'd conclude that can be quite confusing. – FObersteiner Jun 19 '22 at 11:17