Converting timestamp to a different time zone

Posted on
Mon Apr 19, 2021 10:33 am
ryanbuckner offline
Posts: 450
Joined: Oct 08, 2011

Converting timestamp to a different time zone

Ok I worked on this for an hour and couldn't get it right. What's the best way to convert your (Zulu?, UTC?) timestamp to local EST ?

Posted on
Mon Apr 19, 2021 10:35 am
FlyingDiver online
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Converting timestamp to a different time zone

Which state, specifically, are you trying to convert?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 19, 2021 10:42 am
FlyingDiver online
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Converting timestamp to a different time zone

This article covers it pretty well: https://stackabuse.com/converting-strin ... in-python/

Depends on how good your Python skills are. :)

I think it's a three step process:

1. Parse the string to a datetime object using datetime.datetime.strptime()
2. Force that to UTC using pytz
3. Convert that to your preferred time zone astimezone()

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 19, 2021 11:15 am
ryanbuckner offline
Posts: 450
Joined: Oct 08, 2011

Converting timestamp to a different time zone

FlyingDiver wrote:
Which state, specifically, are you trying to convert?



The last time the data was updated. Assuming that's updateTime state or internalDataTimeUTC

Posted on
Mon Apr 19, 2021 11:24 am
FlyingDiver online
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Converting timestamp to a different time zone

I'll try to get a chance to work up a script this afternoon, but no guarantees. Read that article I linked and see if it helps any.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 19, 2021 11:53 am
ryanbuckner offline
Posts: 450
Joined: Oct 08, 2011

Converting timestamp to a different time zone

ok I think I got something working with that article: Since it requires Python3 I'm not sure how to get the device state UTC timestamp as an input tho.

Code: Select all
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import datetime
import pytz

timestring = '2021-04-19T00:37:13'
date_time_obj = datetime.datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S')
timezone = pytz.timezone('America/New_York')
timezone_date_time_obj = timezone.localize(date_time_obj)

print(timezone_date_time_obj.strftime("%m/%d %I:%M:%S %p"))

>> 04/19 12:37:13 AM

Posted on
Mon Apr 19, 2021 12:07 pm
FlyingDiver online
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Converting timestamp to a different time zone

This can be done in Python2, I just don't have time to do it right now.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 19, 2021 12:32 pm
ryanbuckner offline
Posts: 450
Joined: Oct 08, 2011

Converting timestamp to a different time zone

FlyingDiver wrote:
This can be done in Python2, I just don't have time to do it right now.


This worked in 2.7. I just need to make sure my formatting is right. But I think it's all good:

Code: Select all
import datetime
import pytz

timestring = indigo.devices[105421725].states["internalDataTimeUTC"]

date_time_obj = datetime.datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S')
timezone = pytz.timezone('America/New_York')
timezone_date_time_obj = timezone.localize(date_time_obj)

indigo.variable.updateValue(360278443, value=timezone_date_time_obj.strftime("%m/%d %I:%M:%S %p"))


Posted on
Mon Apr 19, 2021 12:40 pm
ryanbuckner offline
Posts: 450
Joined: Oct 08, 2011

Converting timestamp to a different time zone

The timezone adjustment is adding 4 hours to the time, and therefore I'm getting time 4 hours in the future. So I've done something wrong.

Posted on
Mon Apr 19, 2021 12:47 pm
FlyingDiver online
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Converting timestamp to a different time zone

Unfortunately, that's just coercing the timezone to be America/New_York, not converting it from UTC.

Code: Select all
import datetime
import pytz

timestring = '2021-04-19T00:37:13'

date_time_obj = datetime.datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S')
utc_time = pytz.timezone('UTC').localize(date_time_obj)
ny_time = utc_time.astimezone(pytz.timezone('America/New_York'))

print utc_time.strftime("%m/%d %I:%M:%S %p")
print ny_time.strftime("%m/%d %I:%M:%S %p")


04/19 12:37:13 AM
04/18 08:37:13 PM


joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 19, 2021 12:52 pm
FlyingDiver online
User avatar
Posts: 5321
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Converting timestamp to a different time zone

I split this to a new topic, and moved it to the Python scripting forum. It's not really specific to that plugin.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Mon Apr 19, 2021 1:00 pm
ryanbuckner offline
Posts: 450
Joined: Oct 08, 2011

Re: Converting timestamp to a different time zone

FlyingDiver wrote:
I split this to a new topic, and moved it to the Python scripting forum. It's not really specific to that plugin.


Thanks!

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests