|
37 | 37 | #include <date/ptz.h> |
38 | 38 |
|
39 | 39 | #include "config.h" |
| 40 | +#include "itertools.similar.hh" |
40 | 41 | #include "lnav_log.hh" |
41 | 42 |
|
42 | 43 | namespace lnav { |
@@ -87,8 +88,10 @@ strftime_rfc3339(char* buffer, |
87 | 88 | } |
88 | 89 |
|
89 | 90 | ssize_t |
90 | | -strftime_rfc3339( |
91 | | - char* buffer, size_t buffer_size, std::chrono::microseconds micros, char sep) |
| 91 | +strftime_rfc3339(char* buffer, |
| 92 | + size_t buffer_size, |
| 93 | + std::chrono::microseconds micros, |
| 94 | + char sep) |
92 | 95 | { |
93 | 96 | struct tm gmtm; |
94 | 97 | auto secs = std::chrono::duration_cast<std::chrono::seconds>(micros); |
@@ -228,6 +231,39 @@ local_time_to_info(date::local_seconds secs) |
228 | 231 | return TZ_POSIX_UTC.value().get_info(secs); |
229 | 232 | } |
230 | 233 |
|
| 234 | +inline attr_line_t& |
| 235 | +symbol_reducer(const std::string& elem, attr_line_t& accum) |
| 236 | +{ |
| 237 | + return accum.append("\n ").append(lnav::roles::symbol(elem)); |
| 238 | +} |
| 239 | + |
| 240 | +Result<const date::time_zone*, lnav::console::user_message> |
| 241 | +locate_zone(string_fragment tz_name) |
| 242 | +{ |
| 243 | + try { |
| 244 | + return Ok(date::locate_zone(tz_name.to_string_view())); |
| 245 | + } catch (const std::runtime_error& e) { |
| 246 | + attr_line_t note; |
| 247 | + |
| 248 | + try { |
| 249 | + note = (date::get_tzdb().zones |
| 250 | + | lnav::itertools::map(&date::time_zone::name) |
| 251 | + | lnav::itertools::similar_to(tz_name.to_string()) |
| 252 | + | lnav::itertools::fold(symbol_reducer, attr_line_t{})) |
| 253 | + .add_header("did you mean one of the following?"); |
| 254 | + } catch (const std::runtime_error& e) { |
| 255 | + log_error("unable to get timezones: %s", e.what()); |
| 256 | + } |
| 257 | + auto um = lnav::console::user_message::error( |
| 258 | + attr_line_t().append_quoted(tz_name).append( |
| 259 | + " is not a valid timezone")) |
| 260 | + .with_reason(e.what()) |
| 261 | + .with_note(note) |
| 262 | + .move(); |
| 263 | + return Err(um); |
| 264 | + } |
| 265 | +} |
| 266 | + |
231 | 267 | } // namespace lnav |
232 | 268 |
|
233 | 269 | static time_t BAD_DATE = -1; |
@@ -283,8 +319,7 @@ tm2sec(const struct tm* t) |
283 | 319 | if (secs < 0) { |
284 | 320 | return BAD_DATE; |
285 | 321 | } /* must have overflowed */ |
286 | | - else |
287 | | - { |
| 322 | + else { |
288 | 323 | #ifdef HAVE_STRUCT_TM_TM_ZONE |
289 | 324 | if (t->tm_zone) { |
290 | 325 | secs -= t->tm_gmtoff; |
|
0 commit comments