Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_formatting_showcase_example.cpp
Go to the documentation of this file.
1
3
5
6#include <iostream>
7
8int main() {
9 using namespace time_shield;
10
11 const ts_t utc_ts = to_timestamp(2024, 6, 21, 12, 34, 56);
12 const ts_ms_t utc_ms = ts_ms(2024, 6, 21, 12, 34, 56, 789);
13 const tz_t utc_offset = 2 * SEC_PER_HOUR + 30 * SEC_PER_MIN;
14
15 // ISO8601 helpers.
16 std::cout << "ISO8601 local default: " << to_iso8601(utc_ts) << '\n';
17 std::cout << "ISO8601 UTC: " << to_iso8601_utc(utc_ts) << '\n';
18 std::cout << "ISO8601 date: " << to_iso8601_date(utc_ts) << '\n';
19 std::cout << "ISO8601 time: " << to_iso8601_time(utc_ts) << '\n';
20 std::cout << "ISO8601 time UTC: " << to_iso8601_time_utc(utc_ts) << '\n';
21 std::cout << "ISO8601 with offset: " << to_iso8601(utc_ts, utc_offset) << '\n';
22 std::cout << "ISO8601 ms local: " << to_iso8601_ms(utc_ms) << '\n';
23 std::cout << "ISO8601 ms UTC: " << to_iso8601_utc_ms(utc_ms) << '\n';
24 std::cout << "ISO8601 ms with offset: " << to_iso8601_ms(utc_ms, utc_offset) << '\n';
25
26 // Custom formatting helpers.
27 std::cout << "Custom UTC format: "
28 << to_string("%Y-%m-%d %H:%M:%S", utc_ts) << '\n';
29 std::cout << "Custom local format: "
30 << to_string("%Y-%m-%d %H:%M:%S %z", utc_ts, utc_offset) << '\n';
31 std::cout << "Custom UTC format (ms): "
32 << to_string_ms("%Y-%m-%d %H:%M:%S.%sss", utc_ms) << '\n';
33 std::cout << "Custom local fmt (ms): "
34 << to_string_ms("%Y-%m-%d %H:%M:%S.%sss %z", utc_ms, utc_offset) << '\n';
35
36 // Specialized output helpers.
37 std::cout << "MQL5 date/time: " << to_mql5_date_time(utc_ts) << '\n';
38 std::cout << "Filename-safe string: " << to_windows_filename(utc_ts) << '\n';
39 std::cout << "Filename-safe ms: " << to_windows_filename_ms(utc_ms) << '\n';
40 std::cout << "Human readable: " << to_human_readable(utc_ts) << '\n';
41 std::cout << "Human readable (ms): " << to_human_readable_ms(utc_ms) << '\n';
42
43 return 0;
44}
constexpr int64_t SEC_PER_HOUR
Seconds per hour.
constexpr int64_t SEC_PER_MIN
Seconds per minute.
TIME_SHIELD_CONSTEXPR ts_ms_t ts_ms(year_t year, int month, int day)
Alias for to_timestamp_ms.
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_iso8601_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string.
const std::string to_mql5_date_time(ts_t ts)
Converts a timestamp to a string in MQL5 date and time format.
const std::string to_iso8601_time(T ts)
Converts a timestamp to an ISO8601 time string.
const std::string to_windows_filename(ts_t ts)
Converts a timestamp in seconds to a Windows-compatible filename format.
const std::string to_iso8601_date(T ts)
Converts a timestamp to an ISO8601 date string.
std::string to_human_readable(ts_t ts)
Converts a timestamp in seconds to a human-readable format.
const std::string to_iso8601_utc_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string in UTC format.
const std::string to_iso8601(T ts)
Converts a timestamp to an ISO8601 string.
std::string to_human_readable_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a human-readable format.
const std::string to_iso8601_time_utc(T ts)
Converts a timestamp to an ISO8601 UTC time string.
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.
const std::string to_windows_filename_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a Windows-compatible filename format.
const std::string to_iso8601_utc(T ts)
Converts a timestamp to an ISO8601 string in UTC format.
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
Main namespace for the Time Shield library.
Header file for time formatting utilities.