commonlib
utc_time.h
Go to the documentation of this file.
1 
19 #pragma once
20 
21 #include "synchronization.h"
22 
23 #include <string>
24 #include <ctime>
25 
26 BEGIN_NAMESPACE_SYNCHRONIZATION
27 
28 class UTCTime final
29 {
30 public:
31 
44  UTCTime(int32_t hours, int32_t minutes, double seconds, int32_t day, int32_t month, int32_t year) noexcept;
45 
55  UTCTime(int64_t unixTime, double secondsFraction) noexcept;
56 
65  explicit UTCTime(double dateTime) noexcept;
66 
71  int32_t getHours() const;
72 
77  int32_t getMinutes() const;
78 
83  int32_t getSeconds() const;
84 
89  double getSecondsFraction() const;
90 
95  int32_t getDay() const;
96 
101  int32_t getMonth() const;
102 
107  int32_t getYear() const;
108 
113  double getDateTimeValue() const;
114 
119  int64_t getUnixTimeValue() const;
120 
125  static double RelativeSecondsBetween(const UTCTime& start, const UTCTime& end);
126 
133  static UTCTime IncrementSeconds(const UTCTime& time, double secondsIncrement);
134 
140  static constexpr double DateTimeAtUnixStart = 25569.0;
141 
142 private:
143  int32_t hours, minutes, seconds;
144  double secondsFraction;
145  int32_t day, month, year;
146 
147  double dateTime;
148  int64_t unixTime;
149 
150  void calculateUnixAndDateTime(const std::time_t& time);
151  static std::time_t TmToTimeT(std::tm& tm);
152  static std::tm TimeTToTm(std::time_t& time);
153  void setTime(std::time_t& time);
154 };
155 
156 END_NAMESPACE_SYNCHRONIZATION
static constexpr double DateTimeAtUnixStart
UTC days since 30-Dec-1899 UTC, fractional part represents ratio of day.
Definition: utc_time.h:140
int32_t getYear() const
Years of Common Era [1970, INF)
int32_t getSeconds() const
Seconds of minute with fractional part [0, 59].
UTCTime(int32_t hours, int32_t minutes, double seconds, int32_t day, int32_t month, int32_t year) noexcept
Constructs UTCTime from human readable time and date, parameters over their limits are handled and co...
static double RelativeSecondsBetween(const UTCTime &start, const UTCTime &end)
Calculates relative seconds between two UTC times.
static UTCTime IncrementSeconds(const UTCTime &time, double secondsIncrement)
Increments UTC time by positive or negative amount of seconds, the maximum size of an increment is eq...
double getDateTimeValue() const
UTC days since 30-Dec-1899 UTC, fractional part represents ratio of day.
int32_t getDay() const
Day of month [1, 31].
double getSecondsFraction() const
Fractional part of a second [0.0, 1.0)
int64_t getUnixTimeValue() const
Integer number of seconds since start of Unix time on 01-Jan-1970 UTC.
int32_t getMonth() const
Month of year [1, 12].
int32_t getMinutes() const
Minutes of hour [0, 59].
Definition: utc_time.h:28
int32_t getHours() const
Hours of day [0, 23].