|
| 1 | +/** |
| 2 | + * Copyright (c) 2025, 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 | + |
| 30 | +#ifndef lnav_ptime_spec_hh |
| 31 | +#define lnav_ptime_spec_hh |
| 32 | + |
| 33 | +#include <sys/types.h> |
| 34 | + |
| 35 | +#include "base/time_util.hh" |
| 36 | + |
| 37 | +inline bool |
| 38 | +ptime_YmdTHM(struct exttm* dst, const char* str, off_t& off_inout, ssize_t len) |
| 39 | +{ |
| 40 | + static constexpr auto WIDTH = 16; |
| 41 | + |
| 42 | + if (off_inout + WIDTH > len) { |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + auto sep_count = size_t{0}; |
| 47 | + |
| 48 | + auto Y_hundreds |
| 49 | + = (str[off_inout + 0] - '0') * 10 + (str[off_inout + 1] - '0') * 1; |
| 50 | + auto Y_ones |
| 51 | + = (str[off_inout + 2] - '0') * 10 + (str[off_inout + 3] - '0') * 1; |
| 52 | + |
| 53 | + sep_count += str[off_inout + 4] == '-'; |
| 54 | + |
| 55 | + auto m = (str[off_inout + 5] - '0') * 10 + (str[off_inout + 6] - '0') * 1; |
| 56 | + |
| 57 | + sep_count += str[off_inout + 7] == '-'; |
| 58 | + |
| 59 | + auto d = (str[off_inout + 8] - '0') * 10 + (str[off_inout + 9] - '0') * 1; |
| 60 | + |
| 61 | + sep_count += str[off_inout + 10] == 'T'; |
| 62 | + |
| 63 | + auto H = (str[off_inout + 11] - '0') * 10 + (str[off_inout + 12] - '0') * 1; |
| 64 | + |
| 65 | + sep_count += str[off_inout + 13] == ':'; |
| 66 | + |
| 67 | + auto M = (str[off_inout + 14] - '0') * 10 + (str[off_inout + 15] - '0') * 1; |
| 68 | + |
| 69 | + auto Y = (Y_hundreds * 100 + Y_ones) - 1900; |
| 70 | + if (sep_count != 4 || Y < 0 || Y > 1100 || m < 1 || m > 12 || d < 0 |
| 71 | + || d > 31 || H < 0 || H > 23 || M < 0 || M > 59) |
| 72 | + { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + dst->et_tm.tm_year = Y; |
| 77 | + dst->et_tm.tm_mon = m - 1; |
| 78 | + dst->et_tm.tm_mday = d; |
| 79 | + dst->et_tm.tm_hour = H; |
| 80 | + dst->et_tm.tm_min = M; |
| 81 | + |
| 82 | + dst->et_flags |= ETF_YEAR_SET | ETF_MONTH_SET | ETF_DAY_SET | ETF_HOUR_SET |
| 83 | + | ETF_MINUTE_SET; |
| 84 | + |
| 85 | + off_inout += WIDTH; |
| 86 | + |
| 87 | + return true; |
| 88 | +} |
| 89 | + |
| 90 | +#endif |
0 commit comments