summaryrefslogtreecommitdiff
path: root/raphodo/timeutils.py
blob: fca1f8f756f67cf91dd51065e6660d25f850d853 (plain)
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (C) 2015-2017 Damon Lynch <damonlynch@gmail.com>

# This file is part of Rapid Photo Downloader.
#
# Rapid Photo Downloader is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Rapid Photo Downloader is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Rapid Photo Downloader.  If not,
# see <http://www.gnu.org/licenses/>.

# A few utility functions relating to time conversion and internationalization

import locale
from datetime import datetime
import re

from arrow.arrow import Arrow
from gettext import gettext as _


def twelve_hour_clock() -> bool:
    """
    Determine if a twelve hour clock is being used in the current locale

    :return: True if so, else False
    """

    return bool(locale.nl_langinfo(locale.T_FMT_AMPM))


replacement_fmts = [
    ('.%S', ''),
    (':%S', ''),
    (',%S', ''),
    (':%OS', ''),
    ('ཀསར་ཆ%S', ''),
    (' %S초', ''),
    ('%S秒', ''),
    ('%r', '%I:%M'),
    ('%t', '%H:%M'),
    ('%T', '%H:%M')
]

dt_am = datetime(2015, 11, 3, 1)
dt_pm = datetime(2015, 11, 3, 13)


def locale_time(t: datetime) -> str:
    """
    Attempt to localize the time without displaying seconds

    locale.nl_langinfo(locale.T_FMT) for this locale is %r, %T or %t,
    then just return the time, dropping the ':dd' in 'dd:dd:dd' if
    that's the format the time is in.

    Otherwise, then use the approach here:
    Adapted from http://stackoverflow.com/questions/2507726/how-to-display
    -locale-sensitive-time-format-without-seconds-in-python
    :param t: time in datetime format
    :return: time in format like "12:08 AM", or locale equivalent
    """

    t_fmt = locale.nl_langinfo(locale.T_FMT_AMPM) or locale.nl_langinfo(locale.T_FMT)

    if t_fmt in ('%r', '%t', '%T'):
        s = t.strftime('%X').strip()
        return re.sub(r"(\d\d):(\d\d):\d\d", r"\1:\2", s)

    for fmt in replacement_fmts:
        new_t_fmt = t_fmt.replace(*fmt)
        if new_t_fmt != t_fmt:
            return t.strftime(new_t_fmt)

    return t.strftime(t_fmt)


def strip_zero(t: str, strip_zero) -> str:
    if not strip_zero:
        return t.strip()
    return t.lstrip('0').strip()


def strip_am(t: str) -> str:
    if not locale.nl_langinfo(locale.T_FMT_AMPM):
        return t.strip()
    return t.replace(dt_am.strftime('%p'), '').strip()


def strip_pm(t: str) -> str:
    if not locale.nl_langinfo(locale.T_FMT_AMPM):
        return t.strip()
    return t.replace(dt_pm.strftime('%p'), '').strip()


def make_long_date_format(arrowtime: Arrow) -> str:
    # Translators: for example Nov 3 or Dec 31
    long_format = _('%(month)s %(numeric_day)s') % {
        'month': arrowtime.datetime.strftime('%b'),
        'numeric_day': arrowtime.format('D')
    }
    # Translators: for example Nov 15 2015
    return _('%(date)s %(year)s') % dict(date=long_format, year=arrowtime.year)