PLCnext API Documentation  20.6.0.30321
DateTime.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/PrimitiveTypes.hpp"
8 #include <ctime>
9 
10 #ifndef ARP_INSIDE_ARP_H
11  #error Never include 'DateTime.hpp' directly, just include 'Arp.h'
12 #endif
13 
14 namespace Arp
15 {
16 
19 
24 enum class DateTimeKind
25 {
27  Unspecified = 0,
29  Utc = 1,
31  // Local = 2
32 };
33 
43 class DateTime
44 {
45 private: // typedefs
46  using String = BasicString<char8>;
47 
48 public: // construction/destruction
50  DateTime(void);
54  explicit DateTime(int64 ticks, DateTimeKind kind = DateTimeKind::Unspecified);
87  DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind = DateTimeKind::Unspecified);
90  DateTime(const DateTime& arg) = default;
94  DateTime& operator=(const DateTime& arg) = default;
96  ~DateTime(void) = default;
97 
98 public: // operators
102  bool operator==(const DateTime& arg)const;
106  bool operator!=(const DateTime& arg)const;
110  bool operator>(const DateTime& arg)const;
114  bool operator<(const DateTime& arg)const;
118  bool operator>=(const DateTime& arg)const;
122  bool operator<=(const DateTime& arg)const;
123 
124 public: // static operations
127  static DateTime Now(void);
131  static DateTime FromUnixTime(time_t unixTime);
135  ARP_DEPRECATED("Use FromUnixTimeMicroseconds instead")
136  static DateTime FromUnixMicrosecondTicks(uint64 microseconds);
140  static DateTime FromUnixTimeMicroseconds(int64 microseconds);
144  static DateTime FromOpcUaTime(int64 opcUaTime);
148  static DateTime FromIso8601String(const String& value);
152  static DateTime FromBinary(uint64 data);
163  static bool IsLeapYear(int year);
164 
165 public: // setter/getter operations
168  int64 GetTicks(void)const;
172  DateTimeKind GetKind(void)const;
176  int GetYear(void)const;
180  int GetMonth(void)const;
184  int GetDay(void)const;
188  int GetHour(void)const;
192  int GetMinute(void)const;
196  int GetSecond(void)const;
200  int GetMillisecond(void)const;
204  int GetMicrosecond(void)const;
205 
206 public: // operations
209  time_t ToUnixTime(void)const;
212  ARP_DEPRECATED("Use ToUnixTimeMicroseconds instead")
213  uint64 ToUnixMicrosecondTicks(void)const;
216  int64 ToUnixTimeMicroseconds(void)const;
219  uint64 ToBinary(void)const;
222  int64 ToOpcUaTime(void)const;
225  String ToIso8601String(void)const;
226 
227 private: // methods
228  int GetDatePart(int part)const;
229 
230 private: // static methods
231  static int64 DateToTicks(int year, int month, int day);
232  static int64 TimeToTicks(int hour, int minute, int second);
233  static int64 TimeToTicks(int hour, int minute, int second, int millisecond, int microsecond);
234 
235 private: // fields
236  // The data is stored as an unsigned 64-bit integer
237  // Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
238  // 12/31/9999 23:59:59.9999999
239  // Bits 63-64: A four-state value that describes the DateTimeKind value of the date time.
240  uint64 data;
241 
242 private: // static fields
243  static const int kindShift = 62;
244  static const uint64 kindMask = 3ULL << kindShift;
245  static const uint64 ticksMask = ~kindMask;
246 
247  // Number of 100ns ticks per time unit
248  static const int64 ticksPerMicrosecond = 10;
249  static const int64 ticksPerMillisecond = ticksPerMicrosecond * 1000;
250  static const int64 ticksPerSecond = ticksPerMillisecond * 1000;
251  static const int64 ticksPerMinute = ticksPerSecond * 60;
252  static const int64 ticksPerHour = ticksPerMinute * 60;
253  static const int64 ticksPerDay = ticksPerHour * 24;
254 
255  // Number of days in a non-leap year
256  static const int daysPerYear = 365;
257  // Number of days in 4 years
258  static const int daysPer4Years = daysPerYear * 4 + 1;
259  // Number of days in 100 years
260  static const int daysPer100Years = daysPer4Years * 25 - 1;
261  // Number of days in 400 years
262  static const int daysPer400Years = daysPer100Years * 4 + 1;
263 
264  // Number of days from 1/1/0001 to 12/31/1969
265  static const int daysTo1970 = daysPer400Years * 4 + daysPer100Years * 3 + daysPer4Years * 17 + daysPerYear;
266  // Number of days from 1/1/0001 to 12/31/1600
267  static const int daysTo1601 = daysPer400Years * 4;
268  // Number of days from 1/1/0001 to 12/31/9999
269  static const int daysTo10000 = daysPer400Years * 25 - 366;
270 
271  static const int daysToMonth365[];
272  static const int daysToMonth366[];
273 
274  static const int datePartYear = 0;
275  static const int datePartMonth = 2;
276  static const int datePartDayOfYear = 1;
277  static const int datePartDay = 3;
278 
279  // Ticks related
280  static const int64 unix0Ticks = daysTo1970 * ticksPerDay; // 1970.01.01T 00:00:00
281  static const int64 opcua0Ticks = daysTo1601 * ticksPerDay; // 1601.01.01T 00:00:00
282 
283 public: // static fields
286  static const int64 MinTicks = 0;
289  static const int64 MaxTicks = daysTo10000 * ticksPerDay - 1;
290 };
291 
293 // inline methods of class DateTime
294 inline DateTime::DateTime(void)
295  : data(0)
296 {
297 }
298 
299 inline bool DateTime::operator==(const DateTime& arg) const
300 {
301  return this->GetTicks() == arg.GetTicks();
302 }
303 
304 inline bool DateTime::operator!=(const DateTime& arg) const
305 {
306  return this->GetTicks() != arg.GetTicks();
307 }
308 
309 inline bool DateTime::operator>(const DateTime& arg) const
310 {
311  return this->GetTicks() > arg.GetTicks();
312 }
313 
314 inline bool DateTime::operator<(const DateTime& arg) const
315 {
316  return this->GetTicks() < arg.GetTicks();
317 }
318 
319 inline bool DateTime::operator>=(const DateTime& arg) const
320 {
321  return this->GetTicks() >= arg.GetTicks();
322 }
323 
324 inline bool DateTime::operator<=(const DateTime& arg) const
325 {
326  return this->GetTicks() <= arg.GetTicks();
327 }
328 
329 inline int64 DateTime::GetTicks() const
330 {
331  return (int64)(this->data & ticksMask);
332 }
333 
335 {
336  return (DateTimeKind)(this->data >> kindShift);
337 }
338 
340 {
341  return DateTime(unix0Ticks + (uint64)microseconds * ticksPerMicrosecond, DateTimeKind::Utc);
342 }
343 
345 
346 } // end of namespace Arp
bool operator<(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:314
bool operator>=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:319
The class contains date and time informations.
Definition: DateTime.hpp:43
bool operator==(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:299
static DateTime FromUnixMicrosecondTicks(uint64 microseconds)
Creates a DateTime from the as argument passed unix time in microseconds.
Definition: DateTime.hpp:339
Kind of time in not specified
DateTimeKind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC)...
Definition: DateTime.hpp:24
bool operator>(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:309
bool operator!=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:304
bool operator<=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:324
Time is in UTC.
Root namespace for the PLCnext API
std::uint64_t uint64
The Arp unsigned integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:39
int64 GetTicks(void) const
Gets the number of ticks that represent the date and time of this instance.
Definition: DateTime.hpp:329
DateTimeKind GetKind(void) const
Gets a value that indicates whether the time represented by this instance is based on local time...
Definition: DateTime.hpp:334
DateTime(void)
Constructs an zero&#39;ed DateTime instance.
Definition: DateTime.hpp:294
std::int64_t int64
The Arp integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:41