OpenCPN Partial API docs
Loading...
Searching...
No Matches
datetime.cpp
1/***************************************************************************
2 * Copyright (C) 2023 by David Register *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 **************************************************************************/
19
20#include "model/datetime.h"
21
22#include "ocpn_plugin.h"
23
24#if wxCHECK_VERSION(3, 1, 6)
25#include <wx/uilocale.h>
26#endif
27
28namespace ocpn {
29
30wxString getUsrDateTimeFormat() { return ::g_datetime_format; }
31
32// date/time in the desired time zone format.
33wxString toUsrDateTimeFormat(const wxDateTime date_time,
35 wxDateTime t(date_time);
36 wxString effective_time_zone = options.time_zone;
37 if (effective_time_zone == wxEmptyString) {
38 effective_time_zone = ::g_datetime_format;
39 }
40 if (effective_time_zone == wxEmptyString) {
41 effective_time_zone = "UTC";
42 }
43 // Define a map for custom format specifiers.
44 std::vector<std::pair<wxString, wxString>> formatMap = {
45#if wxCHECK_VERSION(3, 1, 6)
46 // Note: the GetInfo() method may return special unicode characters, such
47 // as
48 // narrow no-break space (U+202F).
49 {"$long_date_time",
50 wxUILocale::GetCurrent().GetInfo(wxLOCALE_LONG_DATE_FMT) + " " +
51 wxUILocale::GetCurrent().GetInfo(wxLOCALE_TIME_FMT)},
52 {"$long_date", wxUILocale::GetCurrent().GetInfo(wxLOCALE_LONG_DATE_FMT)},
53 {"$weekday_short_date_time",
54 "%a " + wxUILocale::GetCurrent().GetInfo(wxLOCALE_SHORT_DATE_FMT) + " " +
55 wxUILocale::GetCurrent().GetInfo(wxLOCALE_TIME_FMT)},
56 {"$weekday_short_date",
57 "%a " + wxUILocale::GetCurrent().GetInfo(wxLOCALE_SHORT_DATE_FMT)},
58 {"short_date_time",
59 wxUILocale::GetCurrent().GetInfo(wxLOCALE_SHORT_DATE_FMT) + " " +
60 wxUILocale::GetCurrent().GetInfo(wxLOCALE_TIME_FMT)},
61 {"$short_date",
62 wxUILocale::GetCurrent().GetInfo(wxLOCALE_SHORT_DATE_FMT)},
63 {"$hour_minutes_seconds",
64 wxUILocale::GetCurrent().GetInfo(wxLOCALE_TIME_FMT)},
65#else
66 {"$long_date_time", "%x %X"},
67 {"$long_date", "%x"}, // There is no descriptor for localized long date.
68 // Fallback to short date.
69 {"$weekday_short_date_time", "%a %x %X"},
70 {"$weekday_short_date", "%a %x"},
71 {"$short_date_time", "%x %X"},
72 {"$short_date", "%x"},
73 {"$hour_minutes_seconds", "%X"},
74#endif
75 {"$hour_minutes", "%H:%M"},
76 {"$24_hour_minutes_seconds", "%H:%M:%S"},
77 };
78 wxString format = options.format_string;
79 if (format == wxEmptyString) {
80 format = "$weekday_short_date_time";
81 }
82 // Replace custom specifiers with actual format strings
83 for (const auto& pair : formatMap) {
84 format.Replace(pair.first, pair.second);
85 }
86 wxString ret;
87 if (effective_time_zone == "Local Time") {
88 wxDateTime now = wxDateTime::Now();
89 if ((now == (now.ToGMT())) &&
90 t.IsDST()) // bug in wxWingets 3.0 for UTC meridien ?
91 t.Add(wxTimeSpan(1, 0, 0, 0));
92 // Get the abbreviated name of the timezone configured in the operating
93 // system. Formatting with the actual timezone (rather than "LOC") makes the
94 // labels unambiguous, even if the user changes the timezone settings in the
95 // operating system. For example "2021-10-31 01:30:00 EDT" is unambiguous,
96 // while "2021-10-31 01:30:00 LOC" is not.
97 wxString tzName = t.Format("%Z");
98 ret = t.Format(format) + " " + tzName;
99 } else if (effective_time_zone == "UTC") {
100 // Convert to UTC and format date/time.
101 ret = t.ToUTC().Format(format) + " " + _("UTC");
102 } else if (effective_time_zone == "LMT") {
103 // Local mean solar time at the current location.
104 t = t.ToUTC();
105 if (std::isnan(options.longitude)) {
106 t = wxInvalidDateTime;
107 } else {
108 t.Add(wxTimeSpan(0, 0, wxLongLong(options.longitude * 3600. / 15.)));
109 }
110 ret = t.Format(format) + " " + _("LMT");
111 } else {
112 // Fallback to UTC if the timezone is not recognized.
113 ret = t.ToUTC().Format(format) + " " + _("UTC");
114 }
115 return ret;
116}
117
118} // namespace ocpn
Standard, mostly strings utilities.
Definition datetime.cpp:28
wxString getUsrDateTimeFormat()
Return the date/time format to use when formatting date/time strings.
Definition datetime.cpp:30
PlugIn Object Definition/API.
Configuration options for date and time formatting.