Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_conversions_example.cpp
Go to the documentation of this file.
1
7
8#include <iostream>
9
10#if defined(_WIN32)
12
13int main() {
14 using namespace time_shield;
15
16 double sec = 123.456;
17 std::cout << "ns_of_sec: " << ns_of_sec(sec) << '\n';
18 std::cout << "us_of_sec: " << us_of_sec(sec) << '\n';
19 std::cout << "ms_of_sec: " << ms_of_sec(sec) << '\n';
20
21 auto ms = sec_to_ms(sec);
22 std::cout << "sec_to_ms: " << ms << '\n';
23 std::cout << "ms_to_sec: " << ms_to_sec(ms) << '\n';
24 std::cout << "ms_to_fsec: " << ms_to_fsec(ms) << '\n';
25
26 std::cout << "min_to_sec(2.5): " << min_to_sec(2.5) << '\n';
27 std::cout << "sec_to_min(150): " << sec_to_min(150) << '\n';
28 std::cout << "hour_to_ms(1.5): " << hour_to_ms(1.5) << '\n';
29 std::cout << "hour24_to_12(15): " << hour24_to_12(15) << '\n';
30
31 ts_t ts_val = to_ts(2024, Month::JUN, 21, 14, 30, 0);
32 ts_ms_t ts_ms_val = to_ts_ms(2024, Month::JUN, 21, 14, 30, 0, 250);
33
35 std::cout << "to_dt_ms: " << dt.year << '-' << dt.mon << '-' << dt.day << ' '
36 << dt.hour << ':' << dt.min << ':' << dt.sec << '.' << dt.ms << '\n';
37
38 ts_t back_to_ts = dt_to_timestamp(dt);
39 std::cout << "dt_to_timestamp: " << back_to_ts << '\n';
40
41 std::cout << "start_of_day: " << start_of_day(ts_val) << '\n';
42 std::cout << "end_of_day_ms: " << end_of_day_ms(ts_ms_val) << '\n';
43 std::cout << "next_day_ms: " << next_day_ms(ts_ms_val) << '\n';
44
45 std::cout << "day_of_year: " << day_of_year(ts_val) << '\n';
46 std::cout << "month_of_year: " << static_cast<int>(month_of_year(ts_val)) << '\n';
47 std::cout << "day_of_month: " << day_of_month(ts_val) << '\n';
48 std::cout << "start_of_year_date(2024): " << start_of_year_date(2024) << '\n';
49 std::cout << "day_of_week_date(2024, JUN, 21): "
50 << static_cast<int>(day_of_week_date(2024, Month::JUN, 21)) << '\n';
51
52 std::cout << "Press Enter to exit..." << std::endl;
53 std::cin.get();
54 return 0;
55}
56#else
57int main() {
58 std::cout << "time_conversions.hpp requires Windows." << std::endl;
59 return 0;
60}
61#endif
62
constexpr T ns_of_sec(fts_t ts) noexcept
Get the nanosecond part of the second from a floating-point timestamp.
TIME_SHIELD_CONSTEXPR T day_of_month(ts_t ts=time_shield::ts())
Get the day of the month.
TIME_SHIELD_CONSTEXPR T month_of_year(ts_t ts) noexcept
Get the month of the year.
constexpr T1 min_to_sec(T2 ts) noexcept
Converts a timestamp from minutes to seconds.
constexpr ts_ms_t end_of_day_ms(ts_ms_t ts_ms=time_shield::ts_ms()) noexcept
Get the timestamp at the end of the day in milliseconds.
constexpr T1 hour_to_ms(T2 ts) noexcept
Converts a timestamp from hours to milliseconds.
TIME_SHIELD_CONSTEXPR T hour24_to_12(T hour) noexcept
Converts a 24-hour format hour to a 12-hour format.
TIME_SHIELD_CONSTEXPR ts_t to_ts(year_t year, int month, int day)
Alias for to_timestamp.
T day_of_year(ts_t ts=time_shield::ts())
Get the day of the year.
TIME_SHIELD_CONSTEXPR ts_t dt_to_timestamp(const T &date_time)
Converts a date-time structure to a timestamp.
constexpr T1 ms_to_sec(T2 ts_ms) noexcept
Converts a timestamp from milliseconds to seconds.
T to_dt_ms(ts_ms_t ts)
Alias for to_date_time_ms function.
constexpr ts_t start_of_day(ts_t ts=time_shield::ts()) noexcept
Get the start of the day timestamp.
TIME_SHIELD_CONSTEXPR ts_t start_of_year_date(T year)
Get the timestamp of the start of the year.
constexpr ts_ms_t next_day_ms(ts_ms_t ts_ms, T days=1) noexcept
Calculate the timestamp for a specified number of days in the future (milliseconds).
constexpr T1 sec_to_min(T2 ts) noexcept
Converts a timestamp from seconds to minutes.
constexpr T1 day_of_week_date(T2 year, T3 month, T4 day)
Get the day of the week.
constexpr T us_of_sec(fts_t ts) noexcept
Get the microsecond part of the second from a floating-point timestamp.
constexpr T1 sec_to_ms(T2 ts) noexcept
Converts a timestamp from seconds to milliseconds.
constexpr T ms_of_sec(fts_t ts) noexcept
Get the millisecond part of the second from a floating-point timestamp.
constexpr ts_ms_t to_ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
constexpr fts_t ms_to_fsec(T ts_ms) noexcept
Converts a timestamp from milliseconds to floating-point seconds.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:46
int64_t ts_ms_t
Unix timestamp in milliseconds since epoch.
Definition types.hpp:47
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)
Header file for time conversion functions.