# Installing pytz library

**URL:** <https://forum.inductiveautomation.com/t/installing-pytz-library/94123>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [August 2, 2024, 3:56pm UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123 "2024-08-02T15:56:45Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 2, 2024, 6:24pm UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/21 "2024-08-02T18:24:17Z")

</div>

Then you cannot rely on Ignition to automatically handle this for you. You should still always record and transfer timestamps in UTC.

Provide your users with some timezone session custom property, or pass the appropriate timezone id's as part of your machine information, but keep track of what timezone you need at any point in your GUI. Then use the techniques in this post to **display** timestamps in any desired timezone (instead of `session.props.timezoneId`):

> [@Ignition Perspective Page to show the client System date and time](https://forum.inductiveautomation.com/t/ignition-perspective-page-to-show-the-client-system-date-and-time/73265/8):
>
> Sure. I recommend [DateTimeFormatter.ofPattern()](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/time/format/DateTimeFormatter.html#ofPattern(java.lang.String)). But I would move the complexity to a project script module with a DateTimeFormatter instance with your preferred pattern saved as a top level variable. Like so, perhaps: from java.time import ZoneId from java.time.format import DateTimeFormatter defaultFormatter = DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm") def zonedFormat(ts, zoneid, format=None): formatter = defaultFormatter if format is None else DateTimeFormatter.ofPattern(format) r…

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 2, 2024, 6:26pm UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/22 "2024-08-02T18:26:35Z")

</div>

Thank you Phil for your patience, the link is very useful.. tomorrow I will study it with calm and I will update this topic.

Have a nice day, and thanks again

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 5, 2024, 5:41am UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/23 "2024-08-05T05:41:33Z")

</div>

Hello everyone, at the moment I'm using @pturmel function to return the ZoneId datetime and it works very good for my needs.

I modify the return in this way so to have a datetime datatype:

```python
return ts.toInstant().atZone(ZoneId.of(zoneid)).format(formatter)

```

```python
return system.date.parse(ts.toInstant().atZone(ZoneId.of(zoneid)).format(formatter), "yyyy-MM-dd HH:mm:ss")

```

is this ok or is better to manage in another way?

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 5, 2024, 5:52am UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/24 "2024-08-05T05:52:34Z")

</div>

The first one should return a string, not a datetime.

Where are you using this and what are you trying to achieve with it ?

What exactly is the input to this ? A unix timestamp ?  
If that's the case, `system.date.fromMillis()` will convert it into a datetime object.

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 5, 2024, 5:56am UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/25 "2024-08-05T05:56:22Z")

</div>

Yes the first one is the "original" that return the string, I modify the second to have a datetime.

> [@pascal.fragnoud](#):
>
> this and what are you trying to achieve with it ?

I need to have a datetime conversion accordly to the timezone.

> [@pascal.fragnoud](#):
>
> What exactly is the input to this ? A unix timestamp ?

In this case the input is `system.date.now()`

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 5, 2024, 6:02am UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/26 "2024-08-05T06:02:43Z")

</div>

I think you're missing an important point in Phil's advice: keep the datetime as UTC and only convert where you need to display it.  
Pass the original datetime returned by your database, which should be UTC if you listened to Phil, do the things you need to do with it, and then, as a last operation before it's displayed, convert it to a string, using whatever timezone should be used for this particular date.

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 5, 2024, 6:24am UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/27 "2024-08-05T06:24:12Z")

</div>

At the moment I need to write in the DB the values with the timezone not in UTC because I have to continue with the same standard as is still working in this moment.

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [August 5, 2024, 6:32am UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/28 "2024-08-05T06:32:59Z")

</div>

The same advice applies, except you'll use the `zonedFormat` function Phil provided when you need to display the date. You'll just have to keep track of what zone the date is in, which pretty much means "retrieve it from the db when pulling the data, and pass it along whenever you're passing the datetime"

---

<div class="post-metadata">

**Author:** ![Aiasa21](https://avatars.discourse-cdn.com/v4/letter/a/aeb1de/32.png) [@Aiasa21](https://forum.inductiveautomation.com/u/Aiasa21)\
**Post date:** [August 5, 2024, 6:55pm UTC](https://forum.inductiveautomation.com/t/installing-pytz-library/94123/29 "2024-08-05T18:55:39Z")

</div>

Thanks for the expanation, I'll work on the `zonedFormat` function, thank you for the help

[Previous page](https://forum.inductiveautomation.com/t/installing-pytz-library/94123.md?page=1)
