| 1 |
|
| #include "moment.h" |
| 2 |
|
| #include "dt_core.h" |
| 3 |
|
| #include "dt_parse_iso.h" |
| 4 |
|
| |
| 5 |
|
| static int |
| 6 |
66 |
| parse_string_lenient(const char *str, size_t len, int64_t *sp, IV *np, IV *op) { |
| 7 |
|
| size_t n; |
| 8 |
|
| dt_t dt; |
| 9 |
|
| char c; |
| 10 |
|
| int sod, nanosecond, offset; |
| 11 |
|
| |
| 12 |
66 |
| n = dt_parse_iso_date(str, len, &dt); |
| 13 |
66 |
▶ | if (!n || n == len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n || n == len) |
66 |
0 |
66 |
| if (!n || n == len) |
0 |
66 |
66 |
|
| 14 |
0 |
| return 1; |
| 15 |
|
| |
| 16 |
66 |
| c = str[n++]; |
| 17 |
66 |
▶ | if (!(c == 'T' || c == 't' || c == ' ')) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(c == 'T' || c == 't' || c == ' ')) |
62 |
4 |
66 |
| if (!(c == 'T' || c == 't' || c == ' ')) |
46 |
16 |
62 |
| if (!(c == 'T' || c == 't' || c == ' ')) |
0 |
46 |
46 |
|
| 18 |
0 |
| return 1; |
| 19 |
|
| |
| 20 |
66 |
| str += n; |
| 21 |
66 |
| len -= n; |
| 22 |
|
| |
| 23 |
66 |
| n = dt_parse_iso_time(str, len, &sod, &nanosecond); |
| 24 |
66 |
▶ | if (!n || n == len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n || n == len) |
66 |
0 |
66 |
| if (!n || n == len) |
0 |
66 |
66 |
|
| 25 |
0 |
| return 1; |
| 26 |
|
| |
| 27 |
66 |
▶ | if (str[n] == ' ') |
Branch: 1 branch
| Branch | True | False | Total |
| if (str[n] == ' ') |
27 |
39 |
66 |
|
| 28 |
27 |
| n++; |
| 29 |
|
| |
| 30 |
66 |
| str += n; |
| 31 |
66 |
| len -= n; |
| 32 |
|
| |
| 33 |
66 |
| n = dt_parse_iso_zone_lenient(str, len, &offset); |
| 34 |
66 |
▶ | if (!n || n != len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n || n != len) |
66 |
0 |
66 |
| if (!n || n != len) |
0 |
66 |
66 |
|
| 35 |
0 |
| return 1; |
| 36 |
|
| |
| 37 |
66 |
| *sp = ((int64_t)dt_rdn(dt) - 719163) * 86400 + sod - offset * 60; |
| 38 |
66 |
| *np = nanosecond; |
| 39 |
66 |
| *op = offset; |
| 40 |
66 |
| return 0; |
| 41 |
|
| } |
| 42 |
|
| |
| 43 |
|
| static int |
| 44 |
711 |
| parse_string_strict(const char *str, size_t len, int64_t *sp, IV *np, IV *op) { |
| 45 |
|
| size_t n; |
| 46 |
|
| dt_t dt; |
| 47 |
|
| int sod, nanosecond, offset; |
| 48 |
|
| bool extended; |
| 49 |
|
| |
| 50 |
711 |
| n = dt_parse_iso_date(str, len, &dt); |
| 51 |
711 |
▶ | if (!n || n == len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n || n == len) |
711 |
0 |
711 |
| if (!n || n == len) |
0 |
711 |
711 |
|
| 52 |
0 |
| return 1; |
| 53 |
|
| |
| 54 |
|
| /* |
| 55 |
|
| * 0123456789 |
| 56 |
|
| * 2012-12-14 |
| 57 |
|
| */ |
| 58 |
711 |
| extended = str[4] == '-'; |
| 59 |
711 |
▶ | if (str[n++] != 'T') |
Branch: 1 branch
| Branch | True | False | Total |
| if (str[n++] != 'T') |
62 |
649 |
711 |
|
| 60 |
62 |
| return 1; |
| 61 |
|
| |
| 62 |
649 |
| str += n; |
| 63 |
649 |
| len -= n; |
| 64 |
|
| |
| 65 |
649 |
▶ | if (extended) |
Branch: 1 branch
| Branch | True | False | Total |
| if (extended) |
634 |
15 |
649 |
|
| 66 |
634 |
| n = dt_parse_iso_time_extended(str, len, &sod, &nanosecond); |
| 67 |
|
| else |
| 68 |
15 |
| n = dt_parse_iso_time_basic(str, len, &sod, &nanosecond); |
| 69 |
|
| |
| 70 |
649 |
▶ | if (!n || n == len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n || n == len) |
648 |
1 |
649 |
| if (!n || n == len) |
0 |
648 |
648 |
|
| 71 |
1 |
| return 1; |
| 72 |
|
| |
| 73 |
648 |
| str += n; |
| 74 |
648 |
| len -= n; |
| 75 |
|
| |
| 76 |
648 |
▶ | if (extended) |
Branch: 1 branch
| Branch | True | False | Total |
| if (extended) |
633 |
15 |
648 |
|
| 77 |
633 |
| n = dt_parse_iso_zone_extended(str, len, &offset); |
| 78 |
|
| else |
| 79 |
15 |
| n = dt_parse_iso_zone_basic(str, len, &offset); |
| 80 |
|
| |
| 81 |
648 |
▶ | if (!n || n != len) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!n || n != len) |
646 |
2 |
648 |
| if (!n || n != len) |
1 |
645 |
646 |
|
| 82 |
3 |
| return 1; |
| 83 |
|
| |
| 84 |
645 |
| *sp = ((int64_t)dt_rdn(dt) - 719163) * 86400 + sod - offset * 60; |
| 85 |
645 |
| *np = nanosecond; |
| 86 |
645 |
| *op = offset; |
| 87 |
645 |
| return 0; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
|
| moment_t |
| 91 |
777 |
| THX_moment_from_string(pTHX_ const char *str, STRLEN len, bool lenient) { |
| 92 |
|
| int ret; |
| 93 |
|
| int64_t seconds; |
| 94 |
|
| IV nanosecond, offset; |
| 95 |
|
| |
| 96 |
777 |
▶ | if (lenient) |
Branch: 1 branch
| Branch | True | False | Total |
| if (lenient) |
66 |
711 |
777 |
|
| 97 |
66 |
| ret = parse_string_lenient(str, len, &seconds, &nanosecond, &offset); |
| 98 |
|
| else |
| 99 |
711 |
| ret = parse_string_strict(str, len, &seconds, &nanosecond, &offset); |
| 100 |
|
| |
| 101 |
777 |
▶ | if (ret != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret != 0) |
66 |
711 |
777 |
|
| 102 |
66 |
| croak("Could not parse the given string"); |
| 103 |
|
| |
| 104 |
711 |
| return moment_from_epoch(seconds, nanosecond, offset); |
| 105 |
|
| } |
| 106 |
|
| |