-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringFormatter.cpp
276 lines (232 loc) · 9.09 KB
/
StringFormatter.cpp
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*------------------------------------------------------------------------------
Copyright 2021 Garmin Ltd. or its subsidiaries.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------*/
/**
@file
Contains functionality to format date/time, depth, and location.
Copyright 2018-2020 by Garmin Ltd. or its subsidiaries.
*/
#define DBG_MODULE "ACDB"
#define DBG_TAG "StringFormatter"
#include <cmath>
#include "DBG_pub.h"
#include "Acdb/SettingsManager.hpp"
#include "Acdb/StringFormatter.hpp"
#include "Acdb/StringUtil.hpp"
#include "Acdb/TextHandle.hpp"
#include "Acdb/TextTranslator.hpp"
#include "NavDateTimeExtensions.hpp"
#include "UTL_pub_lib_cnvt.h"
#define DATE_TIME_STR_MAX_LEN 64
static const char* DEGREE_SYMBOL = "\xC2\xB0";
namespace Acdb {
//----------------------------------------------------------------
//!
//! @public
//! @brief Constructor
//!
//----------------------------------------------------------------
StringFormatter::StringFormatter() {} // end of StringFormatter
//----------------------------------------------------------------
//!
//! @public
//! @brief print a position
//! @detail formats a position using the user's chosen coordinate format
//!
//----------------------------------------------------------------
std::string StringFormatter::FormatPosition(const scposn_type& aPosition) const {
std::string result;
double latitude = aPosition.lat * UTL_SEMI_TO_DEG;
double longitude = aPosition.lon * UTL_SEMI_TO_DEG;
std::string latDir = TextTranslator::GetInstance().Find(
static_cast<int>(latitude >= 0.0f ? TextHandle::NorthAbbr : TextHandle::SouthAbbr));
std::string lonDir = TextTranslator::GetInstance().Find(
static_cast<int>(longitude >= 0.0f ? TextHandle::EastAbbr : TextHandle::WestAbbr));
switch (SettingsManager::GetInstance().GetCoordinateFormat()) {
case ACDB_COORD_DEG_MIN:
result =
String::Format("%s%s, %s%s", GetDegreesMinutesString(latitude).c_str(), latDir.c_str(),
GetDegreesMinutesString(longitude).c_str(), lonDir.c_str());
break;
case ACDB_COORD_DEG_MIN_SEC:
result = String::Format("%s%s, %s%s", GetDegreesMinutesSecondsString(latitude).c_str(),
latDir.c_str(), GetDegreesMinutesSecondsString(longitude).c_str(),
lonDir.c_str());
break;
case ACDB_COORD_DEC_DEG:
default:
result = String::Format("%0.4f%s%s, %0.4f%s%s", std::abs(latitude), DEGREE_SYMBOL,
latDir.c_str(), std::abs(longitude), DEGREE_SYMBOL, lonDir.c_str());
break;
}
return result;
} // end of FormatPosition
//----------------------------------------------------------------
//!
//! @public
//! @brief print a depth
//! @detail formats a depth value using the user's chosen depth unit
//!
//----------------------------------------------------------------
std::string StringFormatter::FormatDepthValue(const double aMeters) const {
if (aMeters < 0) {
return std::string{};
}
double value;
std::string unitString;
switch (SettingsManager::GetInstance().GetDistanceUnit()) {
case ACDB_FEET:
value = aMeters * UTL_MT_TO_FT;
unitString = TextTranslator::GetInstance().Find(static_cast<int>(TextHandle::FeetUnit));
break;
case ACDB_METER:
default:
value = aMeters;
unitString = TextTranslator::GetInstance().Find(static_cast<int>(TextHandle::MetersUnit));
}
return String::Format("%.2f %s", value, unitString.c_str());
} // end of FormatDepthValue
//----------------------------------------------------------------
//!
//! @public
//! @brief print a date
//! @detail Formats a Unix timestamp as a date string.
//!
//----------------------------------------------------------------
std::string StringFormatter::FormatDate(const uint64_t aUnixTimestamp) const {
return GetDateString(
NavDateTimeExtensions::EpochToNavDateTime(Acdb::EpochType::UNIX_EPOCH, aUnixTimestamp));
} // end of FormatDate
//----------------------------------------------------------------
//!
//! @public
//! @brief print a date and time
//! @detail Formats an ISO 8601 string (yyyy-mm-ddThh:mm:ssZ) as
//! a local date string.
//!
//----------------------------------------------------------------
std::string StringFormatter::FormatDate(const std::string& aIso8601DateTimeStr) const {
std::string result;
NavDateTime navDateTime;
bool success = navDateTime.FromString(aIso8601DateTimeStr, YYYYMMDDTHHMMSSZ_FORMAT);
if (!success) {
// Fallback -- try to convert with milliseconds.
success = navDateTime.FromString(aIso8601DateTimeStr, YYYYMMDDTHHMMSSMMMZ_FORMAT);
}
if (success) {
result = GetDateString(navDateTime);
}
if (result.empty()) {
DBG_E("Failed to convert date/time from ISO8601 string -- %s", aIso8601DateTimeStr.c_str());
}
return result;
} // end of FormatDate
//----------------------------------------------------------------
//!
//! @public
//! @brief accessor
//!
//! @returns reference to the single instance of the string
//! formatter
//!
//----------------------------------------------------------------
/*static*/ StringFormatter& StringFormatter::GetInstance() {
static StringFormatter instance;
return instance;
} // end of GetInstance
//----------------------------------------------------------------
//!
//! @private
//! @detail Convert NavDateTime to date string
//!
//----------------------------------------------------------------
std::string StringFormatter::GetDateString(const Navionics::NavDateTime& aNavDateTime) const {
DateStringType dateStringType;
DateDelimiterToken delimiter;
std::string result;
switch (SettingsManager::GetInstance().GetDateFormat()) {
case ACDB_DATE_MONTH_ABBR: {
unsigned int month;
unsigned int day;
unsigned int year;
aNavDateTime.GetDate(day, month, year);
std::string monthStr =
TextTranslator::GetInstance().Find(static_cast<int>(TextHandle::MonthJan) + month - 1);
result = String::Format("%u-%s-%u", day, monthStr.c_str(), year);
break;
}
case ACDB_DATE_MDY_SLASH:
dateStringType = DateStringType::MMDDYYYY_FORMAT;
delimiter = DateDelimiterToken::DATE_DELIMITER_SLASH;
break;
case ACDB_DATE_DMY_SLASH:
dateStringType = DateStringType::DDMMYYYY_FORMAT;
delimiter = DateDelimiterToken::DATE_DELIMITER_SLASH;
break;
case ACDB_DATE_MDY_DASH:
dateStringType = DateStringType::MMDDYYYY_FORMAT;
delimiter = DateDelimiterToken::DATE_DELIMITER_DASH;
break;
case ACDB_DATE_DMY_DASH:
default:
dateStringType = DateStringType::DDMMYYYY_FORMAT;
delimiter = DateDelimiterToken::DATE_DELIMITER_DASH;
break;
}
if (result.empty()) {
aNavDateTime.ToString(result, dateStringType, delimiter);
}
return result;
} // end of GetDateString
//----------------------------------------------------------------
//!
//! @private
//! @detail Convert decimal degrees to degrees/minutes string
//!
//----------------------------------------------------------------
std::string StringFormatter::GetDegreesMinutesString(const double aDegrees) const {
double absDegrees = abs(aDegrees);
uint32_t degrees = static_cast<uint32_t>(absDegrees);
double minutes = (absDegrees - degrees) * 60;
// Check whether minutes will round up to 60.
if (minutes > 59.9995) {
minutes = 0;
degrees++;
}
// If updating minutes precision, must make a corresponding change in the rounding check above.
return String::Format("%02i%s%06.3f'", degrees, DEGREE_SYMBOL, minutes);
} // end of GetDegreesMinutesString
//----------------------------------------------------------------
//!
//! @private
//! @detail Convert decimal degrees to degrees/minutes/seconds string
//!
//----------------------------------------------------------------
std::string StringFormatter::GetDegreesMinutesSecondsString(const double aDegrees) const {
double absDegrees = abs(aDegrees);
uint32_t degrees = static_cast<uint32_t>(absDegrees);
uint32_t minutes = static_cast<uint32_t>((absDegrees - degrees) * 60);
double seconds = ((absDegrees - degrees) * 3600) - (60 * minutes);
// Check whether minutes or seconds will round up to 60.
if (seconds > 59.95) {
seconds = 0;
minutes++;
}
if (minutes == 60) {
minutes = 0;
degrees++;
}
// If updating the seconds precision, must make a corresponding change to the rounding check
// above.
return String::Format("%02i%s%02i'%04.1f\"", degrees, DEGREE_SYMBOL, minutes, seconds);
} // end of GetDegreesMinutesSecondsString
} // end of namespace Acdb