Time zone offset

I can’t seem to figure out inside a channel how to find the local Timezone or timezone offset. Can anyone else share how to accomplish converting a Utc time stamp to local time?

I'm pretty sure that the framework method Datetime.FromTimestamp() takes a UTC timestamp and mixes it with local timezone info to return a datetime object.

You can then use python's built-in datetime methods to extract .date(), .time() or other info as necessary and they should all take local timezone into consideration.

I had hoped the same thing, but it seems to preserve the UTC timezone. 

Here is the code I have so far ( in the code for now I am assuming a 6 hour offset) 

  unixtimestarted = Datetime.TimestampFromDatetime(Datetime.ParseDate(airingData['startTime']))
  timezoneoffset = 6 * 60 * 60
  displayeddate = str(Datetime.FromTimestamp(Datetime.TimestampFromDatetime(Datetime.ParseDate(airingData['startTime'])) -timezoneoffset))

Why are you using Datetime.ParseDate() prior to Datetime.FromTimestamp()?

I wouldn't be surprised if that's throwing a wrench into the timezone handling.

The original date I am getting is a string that looks like 2014-07-16T23:30Z which I would need to convert to a Datetime object first right? 

Ah. I was envisioning the wrong type of timestamp.

Try:

startdate = Datetime.ParseDate(airingData['startTime'])
timestarted = startdate.time()
datestarted = startdate.date()

It still does not give a local timestamp:

here is my source data:

'startTime': '2014-07-15T20:00Z'

Here is the output of the code:

2014-07-14 20:02:48,876 (109f07000) :  INFO (__init__:948) - ***TabloTV: startdate: datetime.datetime(2014, 7, 15, 20, 0, tzinfo=tzutc())

2014-07-14 20:02:48,876 (109f07000) :  INFO (__init__:948) - ***TabloTV: time: datetime.time(20, 0)
2014-07-14 20:02:48,876 (109f07000) :  INFO (__init__:948) - ***TabloTV: date: datetime.date(2014, 7, 15)
 
This should be 3 PM local time 

I guess I was wrong about the Framework taking local timezones into account.

Here's a suggestion which _should_ avoid requiring any hard-coded TZ info.

local = Datetime.ParseDate('2014-07-16T23:30Z') - (Datetime.UTCNow() - Datetime.Now())

I guess I was wrong about the Framework taking local timezones into account.

Here's a suggestion which _should_ avoid requiring any hard-coded TZ info.

local = Datetime.ParseDate('2014-07-16T23:30Z') - (Datetime.UTCNow() - Datetime.Now())

Not sure here, and haven't tested, but will above work for timezones on both sides on GMT?

/T

And maybe better with :

https://docs.python.org/2/library/datetime.html#tzinfo-objects

tzinfo.fromutc(selfdt)

/T

Not sure here, and haven't tested, but will above work for timezones on both sides on GMT?

/T

I think so.

And maybe better with :

https://docs.python.org/2/library/datetime.html#tzinfo-objects

tzinfo.fromutc(selfdt)

/T

or that.

I think so.

LOL, and good enough for me...

(If it ain't broken...don't fix it)

/T

I'll try those examples later tonight, but I think I have tried basically the same thing. I found part of the issue, Datetime.Now() returns a naive object and has no tzinfo so Datetime.Now and Datetime.UTCnow() both return the same time. Any ideas how to get an Aware Datetime object? 

See: https://docs.python.org/2/library/datetime.html

Ok, In case anyone else ever has this issue, This appears to work:

   datetime = Datetime.Now()

    timezoneoffset = int((datetime - datetime.utcnow()).total_seconds())

Thanks for posting your findings.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.