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

byceps.util.datetime.timezone   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 3

3 Functions

Rating   Name   Duplication   Size   Complexity  
A utc_to_local_tz() 0 4 1
A local_tz_to_utc() 0 9 1
A get_timezone_string() 0 3 1
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