Passed
Push — main ( 4dce73...0d6f99 )
by Jochen
04:31
created

get_timezone_string()   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 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