Blender V5.3
date_string.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <array>
11#include <ctime>
12#include <string>
13
14#include <fmt/format.h>
15
16#include "BLT_date_string.hh"
17
18#include "BLT_translation.hh"
19
20namespace blender::date_string {
21
26
27static const LocalePatterns &get_locale_patterns(const StringRef locale_iso)
28{
29 static constexpr std::array<LocalePatterns, 9> patterns = {{
30 {"", "{d:02} {b} {Y}"}, /* default */
31 {"en_US", "{d:02} {b} {Y}"}, /* English (US) */
32 {"ar_EG", "{d:02} {b} {Y}"}, /* Arabic (Egypt) */
33 {"zh_HANS", "{Y}年{m}月{d}日"}, /* Chinese (Simplified) */
34 {"zh_HANT", "{Y}年{m}月{d}日"}, /* Chinese (Traditional) */
35 {"hu_HU", "{Y}. {b} {d:02}"}, /* Hungarian */
36 {"ja_JP", "{Y}年{m}月{d}日"}, /* Japanese */
37 {"ko_KR", "{Y}년 {m}월 {d}일"}, /* Korean */
38 {"ur", "{d:02} {b} {Y}"}, /* Urdu */
39 }};
40
41 for (const LocalePatterns &pattern : patterns) {
42 if (pattern.locale == locale_iso) {
43 return pattern;
44 }
45 }
46
47 /* Fallback default pattern (index 0). */
48 return patterns[0];
49}
50
51std::tm localtime_safe(const time_t time)
52{
53 const std::tm *ltime = std::localtime(&time);
54 if (ltime) {
55 return *ltime;
56 }
57
58 /* If localtime fails, return the epoch time. */
59 const time_t zero = 0;
60 return *std::localtime(&zero);
61}
62
63std::string time(const std::tm &date_time, TimeFormat format)
64{
65 std::string_view time_format_str;
66 switch (format) {
67 case TimeFormat::H24:
68 time_format_str = "{H:02}:{M:02}";
69 break;
70 case TimeFormat::H12:
71 time_format_str = "{I}:{M:02} {p}";
72 }
73
74 return fmt::format(fmt::runtime(time_format_str),
75 fmt::arg("H", date_time.tm_hour),
76 fmt::arg("M", date_time.tm_min),
77 fmt::arg("S", date_time.tm_sec),
78 fmt::arg("I", (date_time.tm_hour % 12) == 0 ? 12 : (date_time.tm_hour % 12)),
79 fmt::arg("p",
80 (date_time.tm_hour < 12) ? CTX_IFACE_(BLT_I18NCONTEXT_TIME, "AM") :
82}
83
84std::string date(const std::tm &date_time, const StringRef locale_iso, DateFormat format)
85{
86 static constexpr std::array<StringRef, 12> months = {CTX_N_(BLT_I18NCONTEXT_TIME, "Jan"),
98
99 BLI_assert(date_time.tm_mon >= 0 && date_time.tm_mon < 12);
100 const int month_index = std::clamp(date_time.tm_mon, 0, 11);
101
102 std::string_view date_format_str;
103 switch (format) {
104 case DateFormat::Default: {
105 const LocalePatterns &pattern = get_locale_patterns(locale_iso);
106 date_format_str = pattern.date;
107 break;
108 }
110 date_format_str = "{d:02}/{m:02}/{Y}";
111 break;
113 date_format_str = "{d:02}.{m:02}.{Y}";
114 break;
116 date_format_str = "{d:02}-{m:02}-{Y}";
117 break;
119 date_format_str = "{m:02}/{d:02}/{Y}";
120 break;
122 date_format_str = "{Y}/{m:02}/{d:02}";
123 break;
125 date_format_str = "{Y}.{m:02}.{d:02}";
126 break;
128 date_format_str = "{Y}-{m:02}-{d:02}";
129 break;
130 }
131
132 return fmt::format(fmt::runtime(date_format_str),
133 fmt::arg("Y", date_time.tm_year + 1900),
134 fmt::arg("m", date_time.tm_mon + 1),
135 fmt::arg("b", CTX_IFACE_(BLT_I18NCONTEXT_TIME, months[month_index])),
136 fmt::arg("d", date_time.tm_mday));
137}
138
139std::string datetime(const std::tm &date_time,
140 const StringRef locale_iso,
141 DateFormat date_format,
142 TimeFormat time_format,
143 const std::tm *now,
144 const StringRef today,
145 const StringRef yesterday)
146{
147 bool is_today = false;
148 bool is_yesterday = false;
149 if (now && !today.is_empty() && !yesterday.is_empty()) {
150 is_today = (date_time.tm_yday == now->tm_yday && date_time.tm_year == now->tm_year);
151 std::tm yesterday_tm = *now;
152 yesterday_tm.tm_mday--;
153 mktime(&yesterday_tm);
154 is_yesterday = (date_time.tm_yday == yesterday_tm.tm_yday &&
155 date_time.tm_year == yesterday_tm.tm_year);
156 }
157
158 const std::string time_s = time(date_time, time_format);
159
160 if (is_today) {
161 return fmt::format("{} {}", today, time_s);
162 }
163 if (is_yesterday) {
164 return fmt::format("{} {}", yesterday, time_s);
165 }
166 const std::string date_s = date(date_time, locale_iso, date_format);
167 return fmt::format("{} {}", date_s, time_s);
168}
169
170} // namespace blender::date_string
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_TIME
#define CTX_IFACE_(context, msgid)
constexpr bool is_empty() const
static const LocalePatterns & get_locale_patterns(const StringRef locale_iso)
std::string time(const std::tm &date_time, TimeFormat format=TimeFormat::H24)
std::string datetime(const std::tm &date_time, StringRef locale_iso={}, DateFormat date_format=DateFormat::Default, TimeFormat time_format=TimeFormat::H24, const std::tm *now=nullptr, StringRef today={}, StringRef yesterday={})
std::tm localtime_safe(time_t time)
std::string date(const std::tm &date_time, StringRef locale_iso={}, DateFormat format=DateFormat::Default)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)