File: time_utils.py

package info (click to toggle)
giara 1.1.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,416 kB
  • sloc: python: 5,258; sh: 93; xml: 38; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 968 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from gi.repository import GLib
from datetime import datetime
from dateutil import tz


def utc_datetime_to_now(in_time: datetime) -> datetime:
    return in_time.astimezone(tz.tzlocal())


def utc_timestamp_to_now(timestamp: str) -> datetime:
    return utc_datetime_to_now(
        datetime.fromtimestamp(timestamp, tz.tzutc())
    )


def humanize(dt: datetime) -> str:
    tzl = tz.tzlocal()
    timezone_seconds = tzl.utcoffset(dt).seconds
    timezone_str = '{0}{1}:{2}'.format(
        '+' if timezone_seconds >= 0 else '',
        format(int(timezone_seconds/3600), '02'),
        format(int(
            (timezone_seconds - (int(timezone_seconds/3600)*3600))/60
        ), '02')
    )
    return GLib.DateTime(
        GLib.TimeZone(timezone_str),
        dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second
    ).to_local().format('%-e %b %Y\n%X')


def humanize_utc_timestamp(timestamp: str) -> str:
    return humanize(utc_timestamp_to_now(timestamp))