Kike .Asekas
Kike .Asekas

Reputation: 83

wrong string to timestamp conversion in python

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())

Upvotes: 1

Views: 184

Answers (2)

Alex_6
Alex_6

Reputation: 319

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)

Upvotes: 1

user11553043
user11553043

Reputation:

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

Upvotes: 1

Related Questions