Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
Time Shield Library

Version: 1.0.1

Introduction

The Time Shield Library is a comprehensive C++ library designed for time manipulation, formatting, and conversion. It was built for practical and engineering tasks, especially in constrained or performance-critical environments.

Why Time Shield?

Unlike std::chrono or more academic libraries like HowardHinnant/date, Time Shield is designed to be simple, portable, and suitable for scenarios like logging, serialization, MQL5 usage, and date/time formatting.

  • Uses simple types (int64_t, double) like ts_t, fts_t for timestamps — easy to serialize and store.
  • Supports multiple time representations: Unix, floating-point seconds, ms/us precision, OLE Automation, Julian dates.
  • Includes utilities for rounding, ISO8601 formatting/parsing, time parts, and boundary calculations.
  • Header-only and cross-platform; most modules require only STL and <cstdint>.
  • MQL5-compatible — does not use exceptions, STL containers, or dynamic memory.
  • Modules requiring platform-specific APIs (like NtpClient) are isolated and optional.

Features

  • Validation of dates and times
  • Time and date formatting (standard and custom)
  • Time zone conversion functions
  • ISO8601 string parsing
  • Utilities for time manipulation and conversion

Examples

Here is a simple demonstration:

#include <time_shield.hpp>
int main() {
tsh::ts_t current_ts = tsh::timestamp();
tsh::ts_t future_ts = tsh::end_of_year(current_ts);
int days = tsh::get_days_difference(current_ts, future_ts);
std::cout << "Days until end of year: " << days << std::endl;
std::string formatted_time = tsh::to_iso8601_str(future_ts);
tsh::ts_ms_t ts_ms = 0;
if (!tsh::str_to_ts_ms(formatted_time, ts_ms)) {
std::cout << "Error occurred while parsing ISO8601 string" << std::endl;
return -1;
}
formatted_time = tsh::to_iso8601_utc_ms(ts_ms);
std::cout << "Future time: " << formatted_time << std::endl;
return 0;
}
TIME_SHIELD_CONSTEXPR ts_t end_of_year(ts_t ts=time_shield::ts())
Get the end-of-year timestamp.
constexpr const T get_days_difference(ts_t start, ts_t stop) noexcept
Get the number of days between two timestamps.
const std::string to_iso8601_utc_ms(ts_ms_t ts_ms)
Converts a timestamp in milliseconds to an ISO8601 string in UTC format.
bool str_to_ts_ms(const std::string &str, ts_ms_t &ts)
Convert an ISO8601 string to a millisecond timestamp (ts_ms_t).
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_t timestamp() noexcept
Get the current UTC timestamp in seconds.
int main()
Main header file for the Time Shield library.

Additional example files are located in the examples/ folder:

Installation

Time Shield is a header-only library. To use it in your C++ project:

  • Add the include/time_shield_cpp folder to your project's include paths.
  • Include the main header:
#include <time_shield.hpp>

No additional build steps or external dependencies are required.

For MQL5/MetaTrader:

  • Run install_mql5.bat to copy the necessary .mqh files to your MQL5 include directory.
  • Include the main file in your MQL5 script:
#include <TimeShield.mqh>

To build the C++ examples:

  • Run build-examples.bat to compile example programs with MSVC or your preferred toolchain.

Repository

Time Shield Library GitHub repository

License

This library is licensed under the MIT License. See the LICENSE file for more details.