2

I have the following timestamp 1550588656 which translates to 2019-02-19 15:04:16+00:00 in UTC time convention.

I want to convert it to my country's time convention (UTC or GMT -3 in this time of the year) so it should translate to 2019-02-19 12:04:16+00:00

I have read on other SO questions that first I have to convert the timestamp to an UTC aware Datetime object and then localize it, I'm doing it like this

# string format time
naive_datetime = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')

# string parse time
naive_datetime = datetime.strptime(naive_datetime, "%Y-%m-%d %H:%M:%S")

# make naive Datetime object UTC aware
utc_datetime = naive_datetime.replace(tzinfo=pytz.UTC)

So now it's not a naive Datetime object, from here I should be able to localize it to my country's timezone. In Python that is pytz.timezone('America/Santiago')

So it should go something like this

cltime = pytz.timezone('America/Santiago')
local_datetime = utc_datetime.astimezone(cltime)

But I'm getting 2019-02-19 09:04:16-03:00 (UTC or GTM -6 ) as a result and I don't know why.

Can someone explain? My intuition tells me it's probably a simple thing I'm not looking at, but I've spent some minutes in it and I haven't been able to tell yet.

3
  • When I do the same thing I get datetime.datetime(2019, 2, 19, 12, 4, 16, tzinfo=<DstTzInfo 'America/Santiago' -03-1 day, 21:00:00 DST>). Is this really your code? Commented Feb 19, 2019 at 15:43
  • It is, here's a picture Commented Feb 19, 2019 at 15:51
  • In your second paragraph, it translates to 2019-02-19 12:04:16-03:00. You gave the offset as +00:00 incorrectly. Commented Feb 19, 2019 at 19:15

2 Answers 2

2

If you look at the documentation for fromtimestamp:

Return the local date and time corresponding to the POSIX timestamp

So the problem is that it is already doing a conversion from UTC to the local time, and you're doing it a second time.

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

3 Comments

Well, that was it. I guess that's what I get from mindlessly copying and pasting from other answers. The weird thing is, if you print the timezone info of that resulting Datetime (right after the fromtimestamp call) it says None which, apparently, it's not the case. Thanks anyway, you solved it!
@mfdebian tzinfo=None means "I don't know", which means you need to look at the documentation to figure out the truth. Traditionally datetime doesn't offer its own timezone objects since they're subject to change, so the support is kind of poor.
Thanks for the clarification Mark! (Also, nice profile picture).
0

First of all you have epoc time (UTC timestamp). You need to convert it into datetime object (native) which is followed by converting native time to aware time and than finally convert it to your local time.

  1. Convert your timestamp to native datetime object

    native_datetime = datetime.fromtimestamp(1550588656)

  2. convert native datetime object to aware time (add timezone info, will add timezone info to native timezone UTC for current)

    import pytz
    utc_time = native_datetime.replace(tzinfo=pytz.timezone("UTC"))
    
  3. localising aware datetime to your local datetime

    local_time = utc_time.astimezone(pytz.timezone("America/Santiago"))

    You can replace "America/Santiago" with your local time zone

I think this would help you to solve your problem. Thanks!

Comments

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.