Time Shield Library
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.
static bool try_parse_iso_week_date(const std::string &str, IsoWeekDateStruct &iso) noexcept
Try to parse ISO week-date string.
Definition DateTime.hpp:224
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
bool parse_iso_week_date(const char *input, std::size_t length, IsoWeekDateStruct &iso_date) noexcept
Parse ISO week date string buffer.
const std::string to_string(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp to string with custom format.
const std::string to_string_ms(const std::string &format_str, T timestamp, tz_t utc_offset=0)
Convert timestamp in milliseconds to string with custom format.
bool parse_iso8601(const char *input, std::size_t length, DateTimeStruct &dt, TimeZoneStruct &tz) noexcept
Parse ISO8601 character buffer into DateTimeStruct and TimeZoneStruct.
bool str_to_ts_ms(const std::string &str, ts_ms_t &ts)
Convert an ISO8601 string to a millisecond timestamp (ts_ms_t).
bool try_parse_format(const char *data, std::size_t length, const char *format, std::size_t format_length, DateTimeStruct &out_dt, TimeZoneStruct &out_tz) noexcept
Parse input using formatter-compatible custom pattern.
bool try_parse_format_ts_ms(const char *data, std::size_t length, const char *format, std::size_t format_length, ts_ms_t &out_ts) noexcept
Parse input using formatter-compatible custom pattern and convert to UTC milliseconds.
bool try_parse_format_ts(const char *data, std::size_t length, const char *format, std::size_t format_length, ts_t &out_ts) noexcept
Parse input using formatter-compatible custom pattern and convert to UTC seconds.
bool str_to_ts(const std::string &str, ts_t &ts)
Convert an ISO8601 string to a timestamp (ts_t).
bool str_to_fts(const std::string &str, fts_t &ts)
Convert an ISO8601 string to a floating-point timestamp (fts_t).
TIME_SHIELD_CONSTEXPR ts_t to_timestamp(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0)
Converts a date and time to a timestamp.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:49
int32_t tz_t
Time zone offset in minutes from UTC (e.g., +180 = UTC+3).
Definition types.hpp:61
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:50
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:52
Main namespace for the Time Shield library.
Structure to represent date and time.
int hour
Hour component of time (0-23).
int64_t year
Year component of the date.
int day
Day component of the date (1-31).
int min
Minute component of time (0-59).
int mon
Month component of the date (1-12).
int sec
Second component of time (0-59).
Structure to represent an ISO week date.
int64_t year
ISO week-numbering year component.
int32_t week
ISO week number component (1-52/53).
int32_t weekday
ISO weekday component (1=Monday .. 7=Sunday).
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()