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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
// Copyright (c) 2017-2023, 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
{
public:
static constexpr int num_days = 7;
private:
static const int num_monts = 12;
static const int num_formatters = 11;
// 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_sun", "common_mon", "common_tue", "common_wed", "common_thu", "common_fri", "common_sat"};
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_12hour_min_short",
"locale_24hour_min",
"locale_date_DD_MM_YYYY",
"locale_date_MM_DD_YYYY",
"locale_date_DD_MM_YY",
"locale_date_MM_DD_YY",
"locale_date_DD_MM",
"locale_date_MM_DD",
"locale_date_Day_DD_Mon",
"locale_date_Day_Mon_DD"};
const std::string ltoday = "common_today";
const std::string lyesterday = "common_yesterday";
const std::string ltimezone = "common_timezone";
public:
static constexpr int max_hour_24H_mode = 23;
static constexpr int max_hour_12H_mode = 12;
static constexpr int min_hour_24H_mode = 0;
static constexpr int min_hour_12H_mode = 1;
static constexpr int max_minutes = 59;
static constexpr int max_years = 2038;
static constexpr int min_years = 1970;
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 class TimeFormat
{
FormatTime12H, // H:M in 12h format
FormatTime12HShort, // H:M in 12h format, am/pm excluded
FormatTime24H, // H:M in 24h format
FormatLocaleDate_DD_MM_YYYY,
FormatLocaleDate_MM_DD_YYYY,
FormatLocaleDate_DD_MM_YY,
FormatLocaleDate_MM_DD_YY,
FormatLocaleDate_DD_MM,
FormatLocaleDate_MM_DD,
FormatDate_Day_DD_Month,
FormatDate_Day_Month_DD,
};
static constexpr TimeFormat defaultTimeFormat = TimeFormat::FormatTime24H;
static constexpr std::string_view time_format_12_H = "12 H";
static constexpr std::string_view time_format_24_H = "24 H";
static std::string_view get_time_format(TimeFormat timeFormat)
{
switch (timeFormat) {
case TimeFormat::FormatTime12H:
return time_format_12_H;
case TimeFormat::FormatTime24H:
return time_format_24_H;
default:
return time_format_24_H;
}
}
enum class DateFormat
{
DD_MM_YYYY = 0,
MM_DD_YYYY,
};
static constexpr DateFormat defaultDateFormat = DateFormat::DD_MM_YYYY;
static constexpr std::string_view dd_mm_yyyy = "DD/MM/YYYY";
static constexpr std::string_view mm_dd_yyyy = "MM/DD/YYYY";
static std::string_view get_date_format(DateFormat dateFormat)
{
switch (dateFormat) {
case DateFormat::DD_MM_YYYY:
return dd_mm_yyyy;
case DateFormat::MM_DD_YYYY:
return mm_dd_yyyy;
default:
return dd_mm_yyyy;
}
}
// 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 "";
}
return translate(tlocale.days[day]);
}
static const UTF8 get_day(const uint32_t &day)
{
if (day >= num_days) {
return "";
}
return translate(tlocale.days[day]);
}
static const UTF8 get_short_day(const uint32_t &day)
{
if (day >= num_days) {
LOG_ERROR("Bad value");
return "";
}
return translate(tlocale.daysShort[day]);
}
static const UTF8 get_month(enum Month mon)
{
if (mon >= num_monts) {
LOG_ERROR("Bad value %d", mon);
return "";
}
return translate(tlocale.months[mon]);
}
static const UTF8 yesterday()
{
return translate(tlocale.lyesterday);
}
static const UTF8 today()
{
return translate(tlocale.ltoday);
}
static const std::string format(TimeFormat what)
{
return translate(tlocale.time_formats[static_cast<unsigned>(what)]);
}
static const UTF8 getAM()
{
return translate("common_AM");
}
static const UTF8 getPM()
{
return translate("common_PM");
}
};
}; // namespace time
}; // namespace utils