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
6
7#include <iostream>
8
9#if defined(_WIN32)
13
14int main() {
15 using namespace time_shield;
16
17 const std::string iso = "2024-11-25T14:30:00+01:00";
18
21 if (parse_iso8601(iso, dt, tz)) {
22 std::cout << "Parsed date/time: "
23 << dt.year << '-' << dt.mon << '-' << dt.day << ' '
24 << dt.hour << ':' << dt.min << ':' << dt.sec;
25 if (dt.ms)
26 std::cout << '.' << dt.ms;
27 std::cout << ' ' << (tz.is_positive ? '+' : '-')
28 << tz.hour << ':' << tz.min << '\n';
29 } else {
30 std::cerr << "Failed to parse: " << iso << '\n';
31 }
32
33 ts_t s_ts;
34 if (str_to_ts(iso, s_ts))
35 std::cout << "ts: " << s_ts << '\n';
36
37 ts_ms_t ms_ts;
38 if (str_to_ts_ms(iso, ms_ts))
39 std::cout << "ts_ms: " << ms_ts << '\n';
40
41 fts_t f_ts;
42 if (str_to_fts(iso, f_ts))
43 std::cout << "fts: " << f_ts << '\n';
44
45 std::cout << "Using helpers ts(): " << ts(iso) << '\n';
46 std::cout << "Using helpers ts_ms(): " << ts_ms(iso) << '\n';
47 std::cout << "Using helpers fts(): " << fts(iso) << '\n';
48
49 Month mon = get_month_number<Month>("March");
50 std::cout << "Month number for March: " << static_cast<int>(mon) << '\n';
51
52 int sod;
53 if (sec_of_day("15:30:10", sod))
54 std::cout << "sec_of_day(\"15:30:10\"): " << sod << '\n';
55 std::cout << "sec_of_day(\"8:20\"): " << sec_of_day("8:20") << '\n';
56
57 std::cout << "Press Enter to exit..." << std::endl;
58 std::cin.get();
59 return 0;
60}
61#else
62int main() {
63 std::cout << "time_parser.hpp requires Windows for now_realtime_us()" << std::endl;
64 return 0;
65}
66#endif
Header for date and time structure and related functions.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
constexpr fts_t fts(T1 year, T2 month, T2 day, T2 hour=0, T2 min=0, T2 sec=0, T3 ms=0)
Alias for to_ftimestamp.
constexpr ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr const T sec_of_day(ts_t ts=ts()) noexcept
Get the second of the day.
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 parse_iso8601(const std::string &input, DateTimeStruct &dt, TimeZoneStruct &tz)
Parse a date and time string in ISO8601 format.
T get_month_number(const std::string &month)
Get the month number by name.
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).
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:45
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:46
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:48
Main namespace for the Time Shield library.
Structure to represent date and time.
int ms
Millisecond component of time (0-999)
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 time zone information.
int hour
Hour component of time (0-23)
int min
Minute component of time (0-59)
bool is_positive
True if the time zone offset is positive, false if negative.
Header file with functions for parsing dates and times in ISO8601 format and converting them to vario...
int main()
Header for time zone structure and related functions.