Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
date_time_example.cpp
Go to the documentation of this file.
1#include <time_shield.hpp>
2
3#include <iostream>
4#include <string>
5
7int main() {
8 using namespace time_shield;
9
10 const std::string input = "2025-12-16T10:20:30.123+02:30";
11 DateTime dt;
12 if (!DateTime::try_parse_iso8601(input, dt)) {
13 std::cout << "Failed to parse input" << std::endl;
14 return 1;
15 }
16
17 std::cout << "unix_ms: " << dt.unix_ms() << '\n';
18 std::cout << "iso8601 utc: " << dt.to_iso8601_utc() << '\n';
19 std::cout << "formatted: " << dt.format("%F %T") << '\n';
20
21 const DateTime tomorrow = dt.add_days(1);
22 std::cout << "tomorrow: " << tomorrow.to_iso8601() << '\n';
23 std::cout << "start_of_day: " << dt.start_of_day().to_iso8601() << '\n';
24
25 const IsoWeekDateStruct iso_week = dt.iso_week_date();
26 std::cout << "ISO week-date: " << format_iso_week_date(iso_week) << '\n';
27
28 return 0;
29}
Represents a moment in time with optional fixed UTC offset.
Definition DateTime.hpp:36
DateTime start_of_day() const
Start of local day.
Definition DateTime.hpp:473
IsoWeekDateStruct iso_week_date() const
Local ISO week date.
Definition DateTime.hpp:377
static bool try_parse_iso8601(const std::string &str, DateTime &out) noexcept
Try to parse ISO8601 string to DateTime.
Definition DateTime.hpp:168
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
Definition DateTime.hpp:243
std::string format(const std::string &fmt) const
Format using custom pattern.
Definition DateTime.hpp:253
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
Definition DateTime.hpp:248
ts_ms_t unix_ms() const noexcept
Access UTC milliseconds.
Definition DateTime.hpp:278
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
Definition DateTime.hpp:448
int main()
Demonstrates DateTime parsing, formatting, arithmetic, and ISO week-date usage.
std::string format_iso_week_date(const IsoWeekDateStruct &iso_date, bool extended=true, bool include_weekday=true)
Format ISO week date to string.
Main namespace for the Time Shield library.
Structure to represent an ISO week date.
Main header file for the Time Shield library.