|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, Timothy Stack |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * * Neither the name of Timothy Stack nor the names of its contributors |
| 15 | + * may be used to endorse or promote products derived from this software |
| 16 | + * without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY |
| 19 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY |
| 22 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * |
| 29 | + * @file lnav.tz.cc |
| 30 | + */ |
| 31 | + |
| 32 | +#include "lnav.tz.hh" |
| 33 | + |
| 34 | +#include "attr_line.hh" |
| 35 | +#include "config.h" |
| 36 | +#include "itertools.similar.hh" |
| 37 | +#include "lnav_log.hh" |
| 38 | + |
| 39 | +namespace lnav { |
| 40 | + |
| 41 | +static attr_line_t& |
| 42 | +symbol_reducer(const std::string& elem, attr_line_t& accum) |
| 43 | +{ |
| 44 | + return accum.append("\n ").append(lnav::roles::symbol(elem)); |
| 45 | +} |
| 46 | + |
| 47 | +Result<const date::time_zone*, lnav::console::user_message> |
| 48 | +locate_zone(string_fragment tz_name) |
| 49 | +{ |
| 50 | + try { |
| 51 | + return Ok(date::locate_zone(tz_name.to_string_view())); |
| 52 | + } catch (const std::runtime_error& e) { |
| 53 | + attr_line_t note; |
| 54 | + |
| 55 | + try { |
| 56 | + note = (date::get_tzdb().zones |
| 57 | + | lnav::itertools::map(&date::time_zone::name) |
| 58 | + | lnav::itertools::similar_to(tz_name.to_string()) |
| 59 | + | lnav::itertools::fold(symbol_reducer, attr_line_t{})) |
| 60 | + .add_header("did you mean one of the following?"); |
| 61 | + } catch (const std::runtime_error& e) { |
| 62 | + log_error("unable to get timezones: %s", e.what()); |
| 63 | + } |
| 64 | + auto um = lnav::console::user_message::error( |
| 65 | + attr_line_t().append_quoted(tz_name).append( |
| 66 | + " is not a valid timezone")) |
| 67 | + .with_reason(e.what()) |
| 68 | + .with_note(note) |
| 69 | + .move(); |
| 70 | + return Err(um); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace lnav |
0 commit comments