Total Complexity | 3 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | """ |
||
2 | byceps.util.datetime.timezone |
||
3 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||
4 | |||
5 | Timezone helpers |
||
6 | |||
7 | :Copyright: 2006-2021 Jochen Kupperschmidt |
||
8 | :License: Revised BSD (see `LICENSE` file for details) |
||
9 | """ |
||
10 | |||
11 | 1 | from datetime import datetime |
|
12 | |||
13 | 1 | from flask import current_app |
|
14 | 1 | import pendulum |
|
15 | |||
16 | |||
17 | |||
18 | 1 | def local_tz_to_utc(dt: datetime): |
|
19 | """Convert date/time object from configured default local time to UTC.""" |
||
20 | 1 | tz_str = get_timezone_string() |
|
21 | |||
22 | 1 | return (pendulum.instance(dt) |
|
23 | .set(tz=tz_str) |
||
24 | .in_tz(pendulum.UTC) |
||
25 | # Keep SQLAlchemy from converting it to another zone. |
||
26 | .replace(tzinfo=None)) |
||
27 | |||
28 | |||
29 | 1 | def utc_to_local_tz(dt: datetime) -> datetime: |
|
30 | """Convert naive date/time object from UTC to configured time zone.""" |
||
31 | 1 | tz_str = get_timezone_string() |
|
32 | 1 | return pendulum.instance(dt).in_tz(tz_str) |
|
33 | |||
34 | |||
35 | 1 | def get_timezone_string() -> str: |
|
36 | """Return the configured default timezone as a string.""" |
||
37 | return current_app.config['TIMEZONE'] |
||
38 |