~aleteoryx/muditaos

ref: dca37deb75987037d7770a61e8a8daf8225c9d03 muditaos/module-utils/time/time_locale.hpp -rw-r--r-- 5.3 KiB
dca37deb — Wiktor S. Ovalle Correa [EGD-5297] Fix Release build on linux 5 years ago
                                                                                
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once

#include <array>
#include <utf8/UTF8.hpp>
#include "i18n/i18n.hpp"
#include <log/log.hpp>

namespace utils
{
    namespace time
    {

        class Locale;

        extern Locale tlocale;

        class Locale
        {
            static const int num_days       = 7;
            static const int num_monts      = 12;
            static const int num_formatters = 4;
            // imo it would be nicer to have datetime locales in different json with thiny bit nicer and more effective
            // getters
            const std::array<std::string, num_days> daysShort = {
                "common_mo", "common_tu", "common_we", "common_th", "common_fr", "common_sa", "common_su"};

            const std::array<std::string, num_days> days = {"common_sunday",
                                                            "common_monday",
                                                            "common_tuesday",
                                                            "common_wednesday",
                                                            "common_thursday",
                                                            "common_friday",
                                                            "common_saturday"};

            const std::array<std::string, num_monts> months = {"common_january",
                                                               "common_february",
                                                               "common_march",
                                                               "common_april",
                                                               "common_may",
                                                               "common_june",
                                                               "common_july",
                                                               "common_august",
                                                               "common_september",
                                                               "common_october",
                                                               "common_november",
                                                               "common_december"};

            const std::array<std::string, num_formatters> time_formats{
                "locale_12hour_min",
                "locale_24hour_min",
                "locale_date_full",
                "locale_date_short",
            };

            const std::string ltoday     = "common_today";
            const std::string lyesterday = "common_yesterday";
            const std::string ltimezone  = "common_timezone";

          public:
            enum Day
            {
                Sun = 0,
                Mon,
                Tue,
                Wed,
                Thu,
                Fri,
                Sat,
            };

            enum Month
            {
                Jan = 0,
                Feb,
                Mar,
                Apr,
                May,
                Jun,
                Jul,
                Aug,
                Sept,
                Oct,
                Now,
                Dec
            };

            enum TimeFormat
            {
                FormatTime12H = 0,     // H:M in 12h format
                FormatTime24H,         // H:M in 24h format
                FormatLocaleDateFull,  // format locale specified format
                FormatLocaleDateShort, // format locale specified format
            };

            // this could return variant<bool, UTF8> -> on error -> false -> visit -> handle defaults
            static const UTF8 get_day(enum Day day)
            {
                if (day >= num_days) {
                    LOG_ERROR("Bad value: %d", day);
                    return "";
                }
                else {
                    return localize.get(tlocale.days[day]);
                }
            }

            static const UTF8 get_short_day(const uint32_t &day)
            {
                if (day >= num_days) {
                    LOG_ERROR("Bad value");
                    return "";
                }
                else {
                    return localize.get(tlocale.daysShort[day]);
                }
            }

            static const UTF8 get_month(enum Month mon)
            {
                if (mon >= num_monts) {
                    LOG_ERROR("Bad value %d", mon);
                    return "";
                }
                else {
                    return localize.get(tlocale.months[mon]);
                }
            }

            static const UTF8 yesterday()
            {
                return localize.get(tlocale.lyesterday);
            }

            static const UTF8 today()
            {
                return localize.get(tlocale.ltoday);
            }

            static const std::string format(enum TimeFormat what)
            {
                return localize.get(tlocale.time_formats[what]);
            }

            static const UTF8 getAM()
            {
                return localize.get("common_AM");
            }

            static const UTF8 getPM()
            {
                return localize.get("common_PM");
            }
        };

    }; // namespace time
};     // namespace utils