1

I tested the following code :

date_string = "16:30"
date = datetime.strptime(date_string, "%H:%M")
timestamp = datetime.timestamp(date)
print(timestamp)

But I got the error :

OSError: [Errno 22] Invalid argument

enter image description here

What should I do to get the timestamp of the time?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
Khadija
  • 72
  • 1
  • 8
  • I can't reproduce the error. Please add the full traceback. – Barmar Mar 30 '22 at 15:56
  • Does your code include *from datetime import datetime* ? If it does, I can't reproduce this on macOS – DarkKnight Mar 30 '22 at 15:57
  • Please [don’t post images of code or error messages.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557) – tripleee Jun 17 '22 at 19:13
  • related: [Why is Datetime's .timestamp() method returning OSError: Errno 22 Invalid argument?](https://stackoverflow.com/q/59199985/10197418), [An error occurs in Windows - OSError: Errno 22 Invalid argument - datetime](https://stackoverflow.com/q/67898530/10197418) – FObersteiner Nov 17 '22 at 08:20

3 Answers3

5

Your code works if you make the datetime object aware, i.e. set a time zone:

from datetime import datetime, timezone

date_string = "16:30"
date = datetime.strptime(date_string, "%H:%M").replace(tzinfo=timezone.utc)

print(date)
# 1900-01-01 16:30:00+00:00

print(date.timestamp())
# -2208929400.0

(Tested on Windows 10, Python 3.9.12)

A bit of background, from the docs:

datetime.timestamp() ... Naive datetime instances are assumed to represent local time and this method relies on the platform C mktime() function to perform the conversion.

So, if you call the .timestamp() method on a naive datetime object, local time (i.e. the UTC offset at given date/time) must be determined. If your platform's mktime doesn't support this for a given Unix time, you see OSError: [Errno 22]. In contrast, if you set the time zone (i.e. specify the UTC offset), that call to mktime is not necessary and the error is avoided.

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

The error is that timestamp() doesn't have arguments (and it is exactly what the stack trace is telling you: Invalid argument.

I think you want date.timestamp(), so a method from your date variable (date is a datetime.datetime instance, and not the class datetime.date).

But I think you are importing datetime as from datetime import datetime. Do not do it. it will just confuse you and your editor, so you may get invalid suggestions from your IDE. And also do not use variable name date if you use datetime, but also in general, never use so generic variable names. It just confuse everything. Prefer a better variable name, e.g. a this_date or my_date (so nobody will confuse with datetime.date, especially if in future you need that clas). But better to set a better name.

Giacomo Catenazzi
  • 8,519
  • 2
  • 24
  • 32
0

there is no problem with the code. Datetime Module takes the default year (1900-01-01).

Time Module Supports from 1970-01-01 onwards. If you run below you got to know that.

from time import gmtime, strftime
print(strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime(0)))

In your code you didn't mention the date. so it is taking default date (1900-01-01). For more info Click Here