Time Shield Library 2.0.0
C++ library for working with time
Loading...
Searching...
No Matches
time_parser_example.cpp
Go to the documentation of this file.
1
3
8
9#include <iostream>
10
11int main() {
12 using namespace time_shield;
13
14 // Parse ISO8601 into calendar fields and an explicit fixed offset.
15 const std::string iso8601 = "2024-11-25T14:30:00-05:30";
16 DateTimeStruct iso_dt{};
17 TimeZoneStruct iso_tz{};
18 if (parse_iso8601(iso8601, iso_dt, iso_tz)) {
19 std::cout << "ISO fields: "
20 << iso_dt.year << '-' << iso_dt.mon << '-' << iso_dt.day << ' '
21 << iso_dt.hour << ':' << iso_dt.min << ':' << iso_dt.sec << ' '
22 << to_string(iso_tz) << '\n';
23 }
24
25 // Offset-aware strings are converted to the corresponding UTC instant.
26 ts_t utc_ts = 0;
27 if (str_to_ts(iso8601, utc_ts)) {
28 std::cout << "UTC seconds: " << utc_ts << '\n';
29 }
30
31 // Plain strings without a timezone token are interpreted as UTC.
32 ts_ms_t utc_ms = 0;
33 if (str_to_ts_ms("2024-11-25T20:00:00.250", utc_ms)) {
34 std::cout << "UTC milliseconds: " << utc_ms << '\n';
35 }
36
37 // Floating timestamps keep the fractional part in seconds.
38 fts_t utc_fts = 0.0;
39 if (str_to_fts("2024-11-25T20:00:00.250Z", utc_fts)) {
40 std::cout << "UTC floating seconds: " << utc_fts << '\n';
41 }
42
43 // Formatter and parser use the same custom grammar for round-trips.
44 const tz_t utc_offset = -(5 * SEC_PER_HOUR + 30 * SEC_PER_MIN);
45 const std::string custom = to_string("%Y-%m-%d %H:%M:%S %z", utc_ts, utc_offset);
46 std::cout << "Custom formatted: " << custom << '\n';
47
48 ts_t reparsed_ts = 0;
49 if (try_parse_format_ts(custom, "%Y-%m-%d %H:%M:%S %z", reparsed_ts)) {
50 std::cout << "Reparsed seconds: " << reparsed_ts << '\n';
51 }
52
53 const std::string custom_ms = to_string_ms("%Y-%m-%d %H:%M:%S.%sss %z", utc_ms, utc_offset);
54 std::cout << "Custom formatted with ms: " << custom_ms << '\n';
55
56 ts_ms_t reparsed_ms = 0;
57 if (try_parse_format_ts_ms(custom_ms, "%Y-%m-%d %H:%M:%S.%sss %z", reparsed_ms)) {
58 std::cout << "Parsed custom milliseconds: " << reparsed_ms << '\n';
59 }
60
61 // ISO week-date parsing accepts permissive mixed forms.
62 IsoWeekDateStruct iso_week{};
63 if (parse_iso_week_date("2025-W512", iso_week)) {
64 std::cout << "ISO week mixed form: "
65 << iso_week.year << "-W" << iso_week.week << '-' << iso_week.weekday << '\n';
66 }
67
68 // Lowercase week marker is accepted and missing weekday defaults to Monday.
69 if (DateTime::try_parse_iso_week_date("2025w51", iso_week)) {
70 std::cout << "ISO week lowercase/default weekday: "
71 << iso_week.year << "-W" << iso_week.week << '-' << iso_week.weekday << '\n';
72 }
73
74 // Custom ISO week tokens round-trip through the formatter-compatible parser.
75 const ts_t iso_week_ts = to_timestamp(2025, 12, 16);
76 const std::string iso_week_custom = to_string("%G-%V-%u", iso_week_ts);
77 std::cout << "ISO week custom formatted: " << iso_week_custom << '\n';
78
79 DateTimeStruct iso_week_dt{};
80 TimeZoneStruct iso_week_tz{};
81 if (try_parse_format(iso_week_custom, "%G-%V-%u", iso_week_dt, iso_week_tz)) {
82 std::cout << "ISO week reparsed calendar date: "
83 << iso_week_dt.year << '-' << iso_week_dt.mon << '-' << iso_week_dt.day << '\n';
84 }
85
86 if (try_parse_format("2025-W51", "%G-W%V", iso_week_dt, iso_week_tz)) {
87 std::cout << "ISO week without weekday resolves to: "
88 << iso_week_dt.year << '-' << iso_week_dt.mon << '-' << iso_week_dt.day << '\n';
89 }
90
91 // Failed parsing simply returns false.
92 const bool mismatch = try_parse_format_ts("2024/11/25", "%Y-%m-%d", reparsed_ts);
93 std::cout << "Format mismatch: " << (mismatch ? "unexpected success" : "false") << '\n';
94
95 // ISO week-based year/week tokens do not mix with Gregorian date tokens.
96 const bool conflicting_tokens = try_parse_format(
97 "2025-12-16 2025-51-2",
98 "%Y-%m-%d %G-%V-%u",
99 iso_week_dt,
100 iso_week_tz);
101 std::cout << "Mixed Gregorian/ISO-week tokens: "
102 << (conflicting_tokens ? "unexpected success" : "false") << '\n';
103
104 return 0;
105}
Value-type wrapper for timestamps with fixed UTC offset.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:48
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition types.hpp:60
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:49
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:51
Main namespace for the Time Shield library.
Structure to represent date and time.
Structure to represent an ISO week date.
Structure to represent time zone information.
Header file for fast parsing with formatter-compatible custom patterns.
Header file for time formatting utilities.
Header file with functions for parsing dates and times in ISO8601 format and converting them to vario...
int main()