10#ifdef ORIGINAL_COMPILER_GCC
52 UNIT_FACTOR_BASE * 1000,
53 UNIT_FACTOR_BASE * 1000 * 1000,
54 UNIT_FACTOR_BASE * 1000 * 1000 * 1000,
55 UNIT_FACTOR_BASE * 1000 * 1000 * 1000 * 60,
56 UNIT_FACTOR_BASE * 1000 * 1000 * 1000 * 60 * 60,
57 UNIT_FACTOR_BASE * 1000 * 1000 * 1000 * 60 * 60 * 24,
150#ifdef ORIGINAL_COMPILER_GCC
158 explicit duration(
const timespec& ts);
211 std::
string toString(
bool enter) const override;
213#ifdef ORIGINAL_COMPILER_GCC
219 explicit operator timespec()
const;
221 timespec toTimespec()
const;
399#ifdef ORIGINAL_COMPILER_GCC
405 explicit point(
const timespec& ts);
439 std::string
toString(
bool enter)
const override;
441#ifdef ORIGINAL_COMPILER_GCC
447 explicit operator timespec()
const;
449 timespec toTimespec()
const;
547 enum class calendar :
integer {
556 enum class weekdays :
integer {
567 static constexpr integer DAYS_OF_MONTH[] {
568 31, 28, 31, 30, 31, 30,
569 31, 31, 30, 31, 30, 31
577 static constexpr bool isValidYear(
integer year);
584 static constexpr bool isValidMonth(
integer month);
591 static constexpr bool isValidDay(
integer day);
609 static constexpr auto SATURDAY = weekdays::SATURDAY;
611 static constexpr auto SUNDAY = weekdays::SUNDAY;
613 static constexpr auto MONDAY = weekdays::MONDAY;
615 static constexpr auto TUESDAY = weekdays::TUESDAY;
619 static constexpr auto THURSDAY = weekdays::THURSDAY;
621 static constexpr auto FRIDAY = weekdays::FRIDAY;
720 [[nodiscard]] weekdays
weekday()
const;
825 explicit operator point()
const;
827 point toPoint()
const;
853 std::
string toString(
bool enter) const override;
889 static constexpr auto MONTH =
UTCTime::calendar::MONTH;
891 static constexpr auto YEAR =
UTCTime::calendar::YEAR;
938 time::UTCTime
operator+(
const time::UTCTime& t,
const time::duration& d);
940 time::UTCTime
operator+(
const time::duration& d,
const time::UTCTime& t);
942 time::UTCTime
operator-(
const time::UTCTime& t,
const time::duration& d);
944 time::duration
operator-(
const time::UTCTime& lhs,
const time::UTCTime& rhs);
1020 return time::duration(std::llround(val * time::FACTOR_NANOSECOND), time::NANOSECOND);
1029 return time::duration(std::llround(val * time::FACTOR_MICROSECOND), time::NANOSECOND);
1038 return time::duration(std::llround(val * time::FACTOR_MILLISECOND), time::NANOSECOND);
1047 return time::duration(std::llround(val * time::FACTOR_SECOND), time::NANOSECOND);
1056 return time::duration(std::llround(val * time::FACTOR_MINUTE), time::NANOSECOND);
1065 return time::duration(std::llround(val * time::FACTOR_HOUR), time::NANOSECOND);
1074 return time::duration(std::llround(val * time::FACTOR_DAY), time::NANOSECOND);
1103 throw valueError(
"Invalid time unit specified for duration construction");
1107#ifdef ORIGINAL_COMPILER_GCC
1113 this->operator=(std::move(other));
1121 this->nano_seconds_ = other.nano_seconds_;
1122 other.nano_seconds_ = 0;
1152 throw valueError(
"Invalid time unit specified for duration value conversion");
1159 if (this->nano_seconds_ == other.nano_seconds_)
1161 return this->nano_seconds_ > other.nano_seconds_ ? 1 : -1;
1170 return "time::duration";
1174 std::stringstream ss;
1175 ss <<
"(" << this->className() <<
" " << this->nano_seconds_ <<
"ns)";
1181#ifdef ORIGINAL_COMPILER_GCC
1182inline original::time::duration::operator timespec()
const
1187 return timespec{seconds,
static_cast<long>(nanoseconds)};
1190inline timespec original::time::duration::toTimespec()
const
1192 return static_cast<timespec
>(*this);
1198 this->nano_seconds_ += 1;
1205 this->nano_seconds_ += 1;
1211 this->nano_seconds_ -= 1;
1218 this->nano_seconds_ -= 1;
1224 this->nano_seconds_ += other.nano_seconds_;
1230 this->nano_seconds_ -= other.nano_seconds_;
1236 this->nano_seconds_ *= factor;
1242 this->nano_seconds_ /= factor;
1248 this->nano_seconds_ /= other.nano_seconds_;
1259 return static_cast<floating>(this->nano_seconds_) /
static_cast<floating>(other.nano_seconds_);
1300 res.nano_seconds_ = -res.nano_seconds_;
1347 val = val < 0 ? -val : val;
1353#if ORIGINAL_COMPILER_GCC || (ORIGINAL_COMPILER_CLANG && ORIGINAL_PLATFORM_LINUX)
1355 clock_gettime(CLOCK_REALTIME, &ts);
1357#elif ORIGINAL_PLATFORM_APPLE
1359 gettimeofday(&tv,
nullptr);
1369 : nano_since_epoch_(val,
unit) {}
1372 : nano_since_epoch_(std::move(d)) {}
1374#ifdef ORIGINAL_COMPILER_GCC
1376 : nano_since_epoch_(ts) {}
1381 return this->nano_since_epoch_.value(
unit);
1386 return this->nano_since_epoch_.compareTo(other.nano_since_epoch_);
1391 return this->nano_since_epoch_.toHash();
1395 return "time::point";
1400 std::stringstream ss;
1401 ss <<
"(" << this->className() <<
" " << this->nano_since_epoch_.nano_seconds_ <<
")";
1407#ifdef ORIGINAL_COMPILER_GCC
1408inline original::time::point::operator timespec()
const
1410 return static_cast<timespec
>(this->nano_since_epoch_);
1413inline timespec original::time::point::toTimespec()
const
1415 return static_cast<timespec
>(*this);
1421 ++this->nano_since_epoch_;
1428 ++this->nano_since_epoch_;
1434 --this->nano_since_epoch_;
1441 --this->nano_since_epoch_;
1447 this->nano_since_epoch_ += d;
1453 this->nano_since_epoch_ -= d;
1466 return lhs.nano_since_epoch_ - rhs.nano_since_epoch_;
1482original::time::UTCTime::isValidYear(
const integer year) {
1487original::time::UTCTime::isValidMonth(
const integer month) {
1488 return 1 <= month && month <= 12;
1492original::time::UTCTime::isValidDay(
const integer day) {
1493 return 1 <= day && day <= 31;
1499 this->month_ = month;
1502 this->minute_ = minute;
1503 this->second_ = second;
1514#if ORIGINAL_COMPILER_GCC
1515 integer t = std::time(
nullptr);
1517#if ORIGINAL_PLATFORM_WINDOWS
1518 localtime_s(&local_tm, &t);
1520 localtime_r(&t, &local_tm);
1524#if ORIGINAL_PLATFORM_WINDOWS
1525 gmtime_s(&utc_tm, &t);
1527 gmtime_r(&t, &utc_tm);
1530 offset_seconds =
static_cast<integer>(difftime(mktime(&local_tm), mktime(&utc_tm)));
1532 TIME_ZONE_INFORMATION tz_info;
1533 DWORD result = GetTimeZoneInformation(&tz_info);
1534 if (result != TIME_ZONE_ID_INVALID) {
1536 if (result == TIME_ZONE_ID_DAYLIGHT) {
1551 if (!isValidYear(year))
1553 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
1558 if (!isValidYear(year) || !isValidMonth(month))
1561 if (month == 2 && isLeapYear(year)){
1562 return DAYS_OF_MONTH[month - 1] + 1;
1564 return DAYS_OF_MONTH[month - 1];
1567constexpr original::time::UTCTime::weekdays
1569 if (!isValidYMD(year, month, day))
1570 throw valueError(
"Invalid date for weekday calculation: " +
1571 formatString(year) +
"-" + formatString(month) +
"-" + formatString(day));
1573 integer corrected_year, corrected_month;
1574 if (month == 1 || month == 2){
1575 corrected_year = year - 1;
1576 corrected_month = MONTHS_YEAR + month;
1578 corrected_year = year;
1579 corrected_month = month;
1581 const integer century = corrected_year / YEARS_CENTURY;
1582 const integer years_in_century = corrected_year % YEARS_CENTURY;
1584 return static_cast<weekdays
>((
1586 13 * (corrected_month + 1) / 5 +
1588 years_in_century / 4 +
1596 if (!isValidYear(year) || !isValidMonth(month) || !isValidDay(day))
1599 return day <= daysOfMonth(year, month);
1604 return 0 <= hour && hour <= 23 &&
1605 0 <= minute && minute <= 59 &&
1606 0 <= second && second <= 59;
1612 return isValidYMD(year, month, day) && isValidHMS(hour, minute, second);
1616 return isLeapYear(this->year_);
1619inline original::time::UTCTime::weekdays
1621 return weekday(this->year_, this->month_, this->day_);
1628 :
UTCTime(year, month, day, 0, 0, 0) {}
1632 if (!isValid(year, month, day, hour, minute, second))
1633 throw valueError(
"Invalid UTC time parameters: " +
1634 formatString(year) +
"-" + formatString(month) +
"-" + formatString(day) +
" " +
1635 formatString(hour) +
":" + formatString(minute) +
":" + formatString(second));
1637 this->
set(year, month, day, hour, minute, second);
1657 if (day < days_in_year){
1661 day -= days_in_year;
1667 const integer dim = daysOfMonth(year, month);
1678 this->
set(year, month, day, hour, minute, second);
1682 this->operator=(std::move(other));
1690 this->
set(other.year_, other.month_, other.day_,
1691 other.hour_, other.minute_, other.second_);
1698 return UTCTime{this->year_, this->month_, this->day_};
1705 return this->second_;
1707 return this->minute_;
1713 throw valueError(
"Invalid time unit for UTCTime value access");
1721 return this->month_;
1725 throw valueError(
"Invalid calendar component for UTCTime value access");
1729inline original::time::UTCTime::operator
point()
const {
1736 for (
integer month = 1; month < this->month_; ++month) {
1737 total_days += daysOfMonth(this->year_, month);
1740 total_days += this->day_ - 1;
1745 total_seconds += this->second_;
1752 return static_cast<point>(*this);
1757 if (this->year_ != other.year_)
1758 return this->year_ > other.year_ ? 1 : -1;
1759 if (this->month_ != other.month_)
1760 return this->month_ > other.month_ ? 1 : -1;
1761 if (this->day_ != other.day_)
1762 return this->day_ > other.day_ ? 1 : -1;
1763 if (this->hour_ != other.hour_)
1764 return this->hour_ > other.hour_ ? 1 : -1;
1765 if (this->minute_ != other.minute_)
1766 return this->minute_ > other.minute_ ? 1 : -1;
1767 if (this->second_ != other.second_)
1768 return this->second_ > other.second_ ? 1 : -1;
1776 this-> hour_, this->minute_, this->second_);
1781 return "time::UTCTime";
1785 std::stringstream ss;
1786 ss <<
"(" << this->className() <<
" "
1787 << this->year_ <<
"-"
1788 << std::setw(2) << std::setfill(
'0') << this->month_ <<
"-"
1789 << std::setw(2) << std::setfill(
'0') << this->day_ <<
" "
1790 << std::setw(2) << std::setfill(
'0') << this->hour_ <<
":"
1791 << std::setw(2) << std::setfill(
'0') << this->minute_ <<
":"
1792 << std::setw(2) << std::setfill(
'0') << this->second_ <<
")";
Base class for comparable objects.
Definition comparable.h:31
Forward declaration of hash class template.
Definition hash.h:92
static u_integer hashFunc(const T &t) noexcept
Default hash function fallback.
Forward declaration of hashable interface template.
Definition hash.h:240
Base class providing polymorphic string conversion capabilities.
Definition printable.h:39
Abstract base class for unique element containers.
Definition set.h:44
Represents a UTC calendar date and time.
Definition zeit.h:534
static UTCTime localNow()
Returns the current local time as UTCTime.
Definition zeit.h:1545
static constexpr bool isValidYMD(integer year, integer month, integer day)
Checks if year-month-day is valid.
Definition zeit.h:1595
static integer localZonedOffset()
Returns the system's local timezone offset in hours from UTC.
Definition zeit.h:1512
std::string toString(bool enter) const override
Converts UTCTime to string representation (YYYY-MM-DD HH:MM:SS)
Definition zeit.h:1784
static constexpr auto FRIDAY
Constant for Friday.
Definition zeit.h:621
static constexpr bool isValidHMS(integer hour, integer minute, integer second)
Checks if hour-minute-second is valid.
Definition zeit.h:1603
static constexpr auto SATURDAY
Constant for Saturday.
Definition zeit.h:609
static constexpr integer YEARS_CENTURY
Years in a century.
Definition zeit.h:628
static constexpr integer MONTHS_YEAR
Months in a year.
Definition zeit.h:626
friend UTCTime operator+(const UTCTime &t, const duration &d)
Adds duration to UTCTime.
bool isLeapYear() const
Checks if this year is a leap year.
Definition zeit.h:1615
static constexpr integer daysOfMonth(integer year, integer month)
Gets days in specified month.
Definition zeit.h:1557
static constexpr auto MONDAY
Constant for Monday.
Definition zeit.h:613
integer compareTo(const UTCTime &other) const override
Compares this UTCTime to another.
Definition zeit.h:1756
static constexpr auto TUESDAY
Constant for Tuesday.
Definition zeit.h:615
static constexpr integer DAYS_WEEK
Days in a week.
Definition zeit.h:624
static constexpr auto WEDNESDAY
Constant for Wednesday.
Definition zeit.h:617
UTCTime & operator=(const UTCTime &other)=default
Default copy assignment.
static const UTCTime EPOCH
Epoch time constant (1970-01-01 00:00:00)
Definition zeit.h:631
weekdays weekday() const
Gets weekday for this date.
Definition zeit.h:1620
UTCTime(const UTCTime &other)=default
Default copy constructor.
UTCTime date() const
Returns a new UTCTime object containing only the calendar date.
Definition zeit.h:1696
std::string className() const override
Gets the class name.
Definition zeit.h:1780
u_integer toHash() const noexcept override
Computes hash value for this UTCTime.
Definition zeit.h:1773
static constexpr auto THURSDAY
Constant for Thursday.
Definition zeit.h:619
friend UTCTime operator-(const UTCTime &t, const duration &d)
Subtracts duration from UTCTime.
integer value(unit unit) const
Gets time component value.
Definition zeit.h:1702
static constexpr bool isValid(integer year, integer month, integer day, integer hour, integer minute, integer second)
Checks if full date-time is valid.
Definition zeit.h:1610
UTCTime()
Constructs a UTCTime object representing the epoch (1970-01-01 00:00:00 UTC)
Definition zeit.h:1624
static constexpr auto SUNDAY
Constant for Sunday.
Definition zeit.h:611
static UTCTime now()
Gets current UTC time.
Definition zeit.h:1507
Represents a time duration with nanosecond precision.
Definition zeit.h:134
duration & operator*=(time_val_type factor)
Multiplies duration by a factor.
Definition zeit.h:1235
friend duration operator*(time_val_type factor, const duration &d)
Multiplication operator (factor * duration)
duration(const duration &other)=default
Default copy constructor.
duration & operator+=(const duration &other)
Adds another duration to this one.
Definition zeit.h:1223
u_integer toHash() const noexcept override
Computes hash value for this duration.
Definition zeit.h:1165
friend duration operator/(const duration &lhs, const duration &rhs)
Division operator (duration / duration)
time_val_type value(unit unit=MILLISECOND) const
Gets the duration value in specified units.
Definition zeit.h:1127
std::string className() const override
Gets the class name.
Definition zeit.h:1169
friend duration operator/(const duration &d, time_val_type factor)
Division operator (duration / factor)
duration(time_val_type val=0, unit unit=MILLISECOND)
Constructs a duration with given value and unit.
Definition zeit.h:1079
floating div(time_val_type factor, unit unit=MILLISECOND) const
Floating-point division by a factor.
Definition zeit.h:1253
duration & operator++()
Prefix increment (adds 1 nanosecond)
Definition zeit.h:1197
std::string toString(bool enter) const override
Converts duration to string representation.
Definition zeit.h:1173
friend duration operator-(const duration &d)
Negation operator.
integer compareTo(const duration &other) const override
Compares this duration to another.
Definition zeit.h:1158
static const duration ZERO
Zero duration constant.
Definition zeit.h:141
friend duration operator*(const duration &d, time_val_type factor)
Multiplication operator (duration * factor)
friend duration operator+(const duration &lhs, const duration &rhs)
Addition operator.
friend duration abs(const duration &d)
Absolute value of duration.
duration & operator/=(time_val_type factor)
Divides duration by a factor.
Definition zeit.h:1241
duration & operator-=(const duration &other)
Subtracts another duration from this one.
Definition zeit.h:1229
duration & operator--()
Prefix decrement (subtracts 1 nanosecond)
Definition zeit.h:1210
duration & operator=(const duration &other)=default
Default copy assignment.
friend duration operator-(const duration &lhs, const duration &rhs)
Subtraction operator.
Represents a point in time with nanosecond precision.
Definition zeit.h:374
static point now()
Gets current time point.
Definition zeit.h:1352
time_val_type value(unit unit=MILLISECOND) const noexcept
Gets time value in specified units.
Definition zeit.h:1380
std::string className() const override
Gets the class name.
Definition zeit.h:1394
friend point operator-(const point &p, const duration &d)
Subtracts duration from time point.
point & operator--()
Prefix decrement (subtracts 1 nanosecond)
Definition zeit.h:1433
point & operator++()
Prefix increment (adds 1 nanosecond)
Definition zeit.h:1420
point(time_val_type val=0, unit unit=MILLISECOND)
Constructs time point from value and unit.
Definition zeit.h:1368
point & operator-=(const duration &d)
Subtracts duration from time point.
Definition zeit.h:1452
std::string toString(bool enter) const override
Converts time point to string representation.
Definition zeit.h:1399
friend point operator+(const point &p, const duration &d)
Adds duration to time point.
point & operator+=(const duration &d)
Adds duration to time point.
Definition zeit.h:1446
u_integer toHash() const noexcept override
Computes hash value for this time point.
Definition zeit.h:1390
integer compareTo(const point &other) const override
Compares this time point to another.
Definition zeit.h:1385
friend duration operator-(const point &lhs, const point &rhs)
Computes duration between two time points.
Namespace-like class containing time-related utilities.
Definition zeit.h:40
static constexpr time_val_type FACTOR_MILLISECOND
Conversion factor for milliseconds.
Definition zeit.h:95
static constexpr auto NANOSECOND
Constant for nanosecond unit.
Definition zeit.h:76
static constexpr integer EPOCH_DAY
Epoch day (Unix epoch)
Definition zeit.h:115
static constexpr auto DAY
Constant for day unit.
Definition zeit.h:88
static constexpr auto HOUR
Constant for hour unit.
Definition zeit.h:86
static constexpr auto YEAR
Constant for year calendar component.
Definition zeit.h:891
static constexpr integer EPOCH_MONTH
Epoch month (Unix epoch)
Definition zeit.h:113
static constexpr auto MILLISECOND
Constant for millisecond unit.
Definition zeit.h:80
static constexpr auto MINUTE
Constant for minute unit.
Definition zeit.h:84
static constexpr time_val_type FACTOR_NANOSECOND
Conversion factor for nanoseconds.
Definition zeit.h:91
unit
Time units supported by the library.
Definition zeit.h:65
@ MILLISECOND
Milliseconds (1e-3 seconds)
@ MINUTE
Minutes (60 seconds)
@ MICROSECOND
Microseconds (1e-6 seconds)
@ DAY
Days (86400 seconds)
@ HOUR
Hours (3600 seconds)
@ NANOSECOND
Nanoseconds (1e-9 seconds)
static constexpr auto SECOND
Constant for second unit.
Definition zeit.h:82
static constexpr time_val_type FACTOR_DAY
Conversion factor for days.
Definition zeit.h:103
static constexpr integer DAYS_COMMON_YEAR
Days in a common year.
Definition zeit.h:108
static constexpr auto MICROSECOND
Constant for microsecond unit.
Definition zeit.h:78
static constexpr time_val_type FACTOR_MINUTE
Conversion factor for minutes.
Definition zeit.h:99
integer time_val_type
Type used for storing time values.
Definition zeit.h:43
static constexpr integer DAYS_LEAP_YEAR
Days in a leap year.
Definition zeit.h:106
static constexpr time_val_type FACTOR_SECOND
Conversion factor for seconds.
Definition zeit.h:97
static constexpr time_val_type FACTOR_HOUR
Conversion factor for hours.
Definition zeit.h:101
static constexpr integer EPOCH_YEAR
Epoch year (Unix epoch)
Definition zeit.h:111
static constexpr time_val_type FACTOR_MICROSECOND
Conversion factor for microseconds.
Definition zeit.h:93
static constexpr auto MONTH
Constant for month calendar component.
Definition zeit.h:889
Exception for invalid parameter values.
Definition error.h:150
Interface for objects that can be compared.
Platform-independent type definitions and compiler/platform detection.
Custom exception classes and callback validation utilities.
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:263
double floating
Double-precision floating-point type.
Definition config.h:283
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:254
std::uint64_t ul_integer
64-bit unsigned integer type
Definition config.h:274
Provides a generic hashing utility and interface for hashable types.
User-defined literals for time durations.
Main namespace for the project Original.
Definition algorithms.h:21
auto operator+(const iterator< T > &it, integer steps) -> iterator< T > *
Adds a number of steps to the iterator's current position and returns a new iterator.
auto operator-(const iterator< T > &it, integer steps) -> iterator< T > *
Subtracts a number of steps from the iterator's current position and returns a new iterator.
TYPE abs(TYPE a)
Returns the absolute value of a given number.
time::duration operator*(const time::duration &d, time::time_val_type factor)
Definition zeit.h:1319
time::duration operator/(const time::duration &d, time::time_val_type factor)
Definition zeit.h:1331
Interface for polymorphic string formatting and output.