1

I tried to convert the string "01/03/2019 0:10:00" to a timestamp and the result I got was 1551395400 which is incorrect. This timestamp is equivalent to 28/02/2019 11:00:00.

I don´t understand why I am getting this issue, yesterday on the same computer the conversion was correct.

The code:

date ="01/03/2019 0:10:00"
time.mktime(datetime.datetime.strptime(date, "%d/%m/%Y %H:%M:%S).timetuple())
6
  • 1
    When I run that code the output I get is 1551399000.0 which comes back as the correct time. Sounds like you might not be considering timezones? Commented Jul 26, 2019 at 15:04
  • Did your local time zone change, or did you change some setting on your computer related to time zones? Commented Jul 26, 2019 at 15:08
  • I don't change nothing. Commented Jul 26, 2019 at 15:09
  • I realize when i insert data in a database and I get an error of unique constraint Commented Jul 26, 2019 at 15:10
  • You example has a missing double quote, so it can't be exactly what you ran. It might be informative to get a screen capture of exactly what you are running and the output. Here's mine (which differs from greg_data by 5 hours because of timezone differences). >>> date ="01/03/2019 0:10:00" >>> time.mktime(datetime.datetime.strptime(date, "%d/%m/%Y %H:%M:%S").timetuple()) 1551417000.0 Commented Jul 26, 2019 at 15:21

2 Answers 2

1

The hour has one zero, but is described as %H?

Sign up to request clarification or add additional context in comments.

1 Comment

Changing it to 00: has no effect, so I don't think this is the cause.
1

I think you have a timezone problem. Try this :

import pytz, datetime
local = pytz.timezone ("Europe/Paris") # Put your timezone
date ="01/03/2019 0:10:00"
dt = datetime.datetime.strptime(date, "%d/%m/%Y %H:%M:%S")
local_dt = local.localize(dt, is_dst=None)
utc_dt = local_dt.astimezone(pytz.utc)

Then you can call :

datetime.datetime.timestamp(utc_dt)

3 Comments

So is. Thank you very much.
But now I have another problem. python says that datetime.datetime has no attribute timestamp
Well it's weird. Make sure you've successfully imported the datetime module. Check this website out : programiz.com/python-programming/datetime/timestamp-datetime for some information on the conversion of a datetime object to a timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.