Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions MQL4/Include/TimeShield.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@
#property link "https://github.com/NewYaroslav/time-shield-cpp"
#property strict

const int TSHIELD_MS_PER_SEC = 1000; ///< Milliseconds per second.
const int TSHIELD_US_PER_SEC = 1000000; ///< Microseconds per second.
const int TSHIELD_SEC_PER_MIN = 60; ///< Seconds per minute.
const int TSHIELD_SEC_PER_HOUR = 3600; ///< Seconds per hour.
const int TSHIELD_SEC_PER_DAY = 86400; ///< Seconds per day.

/// \class TimeShield
/// \brief Provides static time helpers for MQL4.
class TimeShield {
public:
enum Constants {
MS_PER_SEC = 1000, ///< Milliseconds per second.
US_PER_SEC = 1000000, ///< Microseconds per second.
SEC_PER_MIN = 60, ///< Seconds per minute.
SEC_PER_HOUR = 3600, ///< Seconds per hour.
SEC_PER_DAY = 86400 ///< Seconds per day.
};

/// \brief Get raw terminal tick counter in milliseconds.
/// \return Wrapped 32-bit terminal tick counter.
static uint tick_count_ms() {
Expand Down Expand Up @@ -77,7 +75,7 @@ public:
while((next_ts = ts()) == start_ts) {
}

offset = next_ts * MS_PER_SEC - (long)monotonic_ms();
offset = next_ts * TSHIELD_MS_PER_SEC - (long)monotonic_ms();
initialized = true;
}

Expand All @@ -100,14 +98,14 @@ public:
/// \param timestamp Timestamp in seconds.
/// \return Second of day.
static int sec_of_day(const long timestamp) {
return (int)(timestamp % SEC_PER_DAY);
return (int)(timestamp % TSHIELD_SEC_PER_DAY);
}

/// \brief Get second of day from milliseconds timestamp.
/// \param timestamp_ms Timestamp in milliseconds.
/// \return Second of day.
static int sec_of_day_ms(const long timestamp_ms) {
return sec_of_day(timestamp_ms / MS_PER_SEC);
return sec_of_day(timestamp_ms / TSHIELD_MS_PER_SEC);
}

/// \brief Get second of day from hours, minutes and seconds.
Expand All @@ -116,7 +114,7 @@ public:
/// \param second Second value.
/// \return Second of day.
static int sec_of_day(const int hour, const int minute, const int second) {
return hour * SEC_PER_HOUR + minute * SEC_PER_MIN + second;
return hour * TSHIELD_SEC_PER_HOUR + minute * TSHIELD_SEC_PER_MIN + second;
}

/// \brief Check time-of-day components.
Expand All @@ -132,15 +130,15 @@ public:

/// \brief Convert string with time of day to second of day.
/// \param value Time string in HH, HH:MM, or HH:MM:SS format.
/// \return Second of day or SEC_PER_DAY on parse failure.
/// \return Second of day or TSHIELD_SEC_PER_DAY on parse failure.
static int sec_of_day(const string value) {
string parts[];
const ushort separator = StringGetCharacter(":", 0);
const int count = StringSplit(value, separator, parts);

if(count < 1 || count > 3) {
ArrayFree(parts);
return SEC_PER_DAY;
return TSHIELD_SEC_PER_DAY;
}

int hour = 0;
Expand All @@ -158,7 +156,7 @@ public:
ArrayFree(parts);

if(!is_valid_time(hour, minute, second)) {
return SEC_PER_DAY;
return TSHIELD_SEC_PER_DAY;
}

return sec_of_day(hour, minute, second);
Expand Down
63 changes: 48 additions & 15 deletions MQL5/Include/TimeShield.mqh
Original file line number Diff line number Diff line change
@@ -1,38 +1,71 @@
//+------------------------------------------------------------------+
//| TimeShield.mqh |
//| MQL5 Facade for TimeShield C++ Library |
//| Copyright 2025, NewYaroslav |
//| https://github.com/NewYaroslav/time-shield-cpp |
//+------------------------------------------------------------------+
#ifndef __TIME_SHIELD_FACADE_MQH__
#define __TIME_SHIELD_FACADE_MQH__
#ifndef TIME_SHIELD_MQL5_MQH_INCLUDED
#define TIME_SHIELD_MQL5_MQH_INCLUDED

/// \file TimeShield.mqh
/// \brief MQL5-style facade header for the Time Shield library.
///
/// This is a thin wrapper over the internal `time_shield.mqh` header, designed
/// to provide a user-friendly PascalCase interface for MQL5 users.
/// Prefer `#include <TimeShield.mqh>` when using the library in MQL5.
/// \ingroup mql5
/// \brief Main MQL5 header file for the Time Shield library.

#property copyright "Copyright 2025, NewYaroslav"
#property link "https://github.com/NewYaroslav/time-shield-cpp"
#property strict

/// \defgroup mql5 MQL5 TimeShield API
/// \brief Components of the Time Shield library adapted for MetaTrader 5.
/// @{
///
/// This group includes all functions, structures, and utilities that are part of the MQL5-compatible version
/// of the Time Shield library.
/// This group includes all functions, structures, and utilities that are part
/// of the MQL5-compatible version of the Time Shield library.
///
/// Example usage:
/// \code
/// #include <TimeShield.mqh>
/// time_shield::DateStruct d = time_shield::create_date_struct(2025, 6, 21);
/// \endcode
///
/// @}

#include <time_shield.mqh>
// Constants used in time calculations
#include <time_shield/constants.mqh>

// Enumerations used in time representations
#include <time_shield/enums.mqh>

// Structures representing time components
#include <time_shield/time_struct.mqh>

// Structures representing date components
#include <time_shield/date_struct.mqh>

// Structure representing a time zone
#include <time_shield/time_zone_struct.mqh>

// Structure representing date and time combinations
#include <time_shield/date_time_struct.mqh>

// Value-type wrapper for date-time with offset
#include <time_shield/DateTime.mqh>

// Functions for validation of time-related values
#include <time_shield/validation.mqh>

// Utility functions for time manipulation
#include <time_shield/time_utils.mqh>

// Functions for converting between different time representations
#include <time_shield/time_conversions.mqh>

// Functions for converting between time zones
#include <time_shield/time_zone_conversions.mqh>

// Functions for formatting time in various standard formats
#include <time_shield/time_formatting.mqh>

// Functions for parsing time in various standard formats
#include <time_shield/time_parser.mqh>

// Functions for initializing the library
#include <time_shield/initialization.mqh>

#endif // __TIME_SHIELD_FACADE_MQH__
#endif // TIME_SHIELD_MQL5_MQH_INCLUDED
64 changes: 0 additions & 64 deletions MQL5/Include/time_shield.mqh

This file was deleted.

22 changes: 15 additions & 7 deletions MQL5/Include/time_shield/DateTime.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ namespace time_shield {
/// \param offset Fixed UTC offset in seconds.
/// \return Constructed DateTime.
static DateTime from_unix_ms(const long utc_ms, const int offset = 0) {
return DateTime(utc_ms, offset);
DateTime result(utc_ms, offset);
return result;
}

/// \brief Construct instance for current UTC time.
/// \param offset Fixed UTC offset in seconds.
/// \return DateTime set to now.
static DateTime now_utc(const int offset = 0) {
return DateTime(ts_ms(), offset);
DateTime result(ts_ms(), offset);
return result;
}

/// \brief Try to build from calendar components interpreted in provided offset.
Expand Down Expand Up @@ -73,7 +75,8 @@ namespace time_shield {
return false;

const long local_ms = to_timestamp_ms(year, month, day, hour, min, sec, ms);
out = DateTime(local_ms - offset_to_ms(offset), offset);
DateTime result(local_ms - offset_to_ms(offset), offset);
out = result;
return true;
}

Expand All @@ -94,7 +97,8 @@ namespace time_shield {
return false;

const long local_ms = dt_to_timestamp_ms(local_dt);
out = DateTime(local_ms - offset_to_ms(offset), offset);
DateTime result(local_ms - offset_to_ms(offset), offset);
out = result;
return true;
}

Expand All @@ -108,8 +112,9 @@ namespace time_shield {
if (!parse_iso8601(str, dt, tz))
return false;

out = DateTime(dt_to_timestamp_ms(dt) - offset_to_ms(time_zone_struct_to_offset(tz)),
time_zone_struct_to_offset(tz));
const int offset = time_zone_struct_to_offset(tz);
DateTime result(dt_to_timestamp_ms(dt) - offset_to_ms(offset), offset);
out = result;
return true;
}

Expand All @@ -123,7 +128,10 @@ namespace time_shield {
int utc_offset() const { return m_offset; }

/// \brief Return copy with new offset preserving instant.
DateTime with_offset(const int new_offset) const { return DateTime(m_utc_ms, new_offset); }
DateTime with_offset(const int new_offset) const {
DateTime result(m_utc_ms, new_offset);
return result;
}

/// \brief Return copy with zero offset.
DateTime to_utc() const { return with_offset(0); }
Expand Down
2 changes: 1 addition & 1 deletion MQL5/Include/time_shield/time_parser.mqh
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ namespace time_shield {
dt.mon =(int)StringToInteger(parts[1]);
dt.day =(int)StringToInteger(parts[2]);

if(!is_valid_date(dt))
if(!is_valid_date(dt.year, dt.mon, dt.day))
return false;

if (StringLen(time_part)>0) {
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ library and report platform capabilities:

C++ and MQL5 public headers place their declarations inside the `time_shield`
namespace. Use `time_shield::` or `using namespace time_shield;` to access that
API. MQL4 uses the `TimeShield` class with static methods and constants because
the MQL4 language does not provide namespaces.
API. MQL4 uses the `TimeShield` class for static methods and `TSHIELD_`
constants because the MQL4 language does not provide namespaces.

> Some functions depend on platform APIs and may be limited (for example,
> obtaining realtime via `QueryPerformanceCounter` on Windows).
Expand Down Expand Up @@ -189,6 +189,7 @@ Use `install_mql5.bat` to install the MQL5 files. For MQL4, copy

long now_ms = TimeShield::now();
int second_day = TimeShield::sec_of_day(TimeCurrent());
int one_day = TSHIELD_SEC_PER_DAY;
```

### Integration Notes
Expand Down
Loading