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
// Copyright (c) 2017-2021, 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 = 5;
// 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_12hour_min_short",
"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 class TimeFormat
{
FormatTime12H = 0, // H:M in 12h format
FormatTime12HShort, // H:M in 12h format, am/pm excluded
FormatTime24H, // H:M in 24h format
FormatLocaleDateFull, // format locale specified format
FormatLocaleDateShort, // format locale specified format
};
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 "";
}
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(TimeFormat what)
{
return localize.get(tlocale.time_formats[static_cast<unsigned>(what)]);
}
static const UTF8 getAM()
{
return localize.get("common_AM");
}
static const UTF8 getPM()
{
return localize.get("common_PM");
}
};
}; // namespace time
}; // namespace utils