Time Shield Library
C++ library for working with time
Toggle main menu visibility
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
7
int
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
}
time_shield::DateTime
Represents a moment in time with optional fixed UTC offset.
Definition
DateTime.hpp:37
time_shield::DateTime::start_of_day
DateTime start_of_day() const
Start of local day.
Definition
DateTime.hpp:542
time_shield::DateTime::iso_week_date
IsoWeekDateStruct iso_week_date() const
Local ISO week date.
Definition
DateTime.hpp:417
time_shield::DateTime::try_parse_iso8601
static bool try_parse_iso8601(const std::string &str, DateTime &out) noexcept
Try to parse ISO8601 string to DateTime.
Definition
DateTime.hpp:167
time_shield::DateTime::to_iso8601
std::string to_iso8601() const
Format to ISO8601 string with stored offset.
Definition
DateTime.hpp:248
time_shield::DateTime::format
std::string format(const std::string &fmt) const
Format using custom pattern.
Definition
DateTime.hpp:258
time_shield::DateTime::to_iso8601_utc
std::string to_iso8601_utc() const
Format to ISO8601 string in UTC.
Definition
DateTime.hpp:253
time_shield::DateTime::unix_ms
ts_ms_t unix_ms() const noexcept
Access UTC milliseconds.
Definition
DateTime.hpp:283
time_shield::DateTime::add_days
DateTime add_days(int64_t days) const noexcept
Add days to UTC instant.
Definition
DateTime.hpp:517
main
int main()
Demonstrates DateTime parsing, formatting, arithmetic, and ISO week-date usage.
Definition
date_time_example.cpp:7
time_shield::format_iso_week_date
std::string format_iso_week_date(const IsoWeekDateStruct &iso_date, bool extended=true, bool include_weekday=true)
Format ISO week date to string.
Definition
iso_week_conversions.hpp:134
time_shield
Main namespace for the Time Shield library.
time_shield::IsoWeekDateStruct
Structure to represent an ISO week date.
Definition
iso_week_struct.hpp:17
time_shield.hpp
Main header file for the Time Shield library.
examples
date_time_example.cpp
Generated by
1.17.0