Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
time_formatting_example.cpp
Go to the documentation of this file.
1
6
7#include <iostream>
8#include <chrono>
9
10#if defined(_WIN32)
12
13int main() {
14 using namespace time_shield;
15
16 auto now = std::chrono::system_clock::now();
17 const ts_t now_sec = static_cast<ts_t>(std::chrono::system_clock::to_time_t(now));
18 const ts_ms_t now_ms = static_cast<ts_ms_t>(
19 std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
20
21 std::cout << "ISO8601: " << to_iso8601(now_sec) << '\n';
22 std::cout << "ISO8601 with ms: " << to_iso8601_ms(now_ms) << '\n';
23 std::cout << "Custom format: "
24 << to_string("%Y-%m-%d %H:%M:%S", now_sec) << '\n';
25 std::cout << "Windows filename: " << to_windows_filename(now_sec) << '\n';
26 std::cout << "Windows filename (ms): " << to_windows_filename_ms(now_ms) << '\n';
27 std::cout << "MQL5 date/time: " << to_mql5_date_time(now_sec) << '\n';
28 std::cout << "Human readable: " << to_human_readable(now_sec) << '\n';
29 std::cout << "Human readable (ms): " << to_human_readable_ms(now_ms) << '\n';
30
31 std::cout << "Press Enter to exit..." << std::endl;
32 std::cin.get();
33 return 0;
34}
35#else
36int main() {
37 std::cout << "time_formatting.hpp requires Windows." << std::endl;
38 return 0;
39}
40#endif
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_windows_filename(ts_t ts)
Converts a timestamp in seconds to a Windows-compatible filename format.
std::string to_human_readable(ts_t ts)
Converts a timestamp in seconds to a human-readable 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_windows_filename_ms(ts_ms_t ts)
Converts a timestamp in milliseconds to a Windows-compatible filename format.
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
const ts_ms_t now() noexcept
Get the current UTC timestamp in milliseconds.
Main namespace for the Time Shield library.
Header file for time formatting utilities.