PLCnext API Documentation  21.0.0.35466
DateTime.hpp
1 //
3 // Copyright PHOENIX CONTACT Electronics GmbH
4 //
6 #pragma once
7 #include "Arp/System/Core/PrimitiveTypes.hpp"
8 #include <ctime>
9 #include <iostream>
10 
11 #ifndef ARP_INSIDE_ARP_H
12  #error Never include 'DateTime.hpp' directly, just include 'Arp.h'
13 #endif
14 
15 namespace Arp
16 {
17 
20 
25 enum class DateTimeKind
26 {
28  Unspecified = 0,
30  Utc = 1,
32  // Local = 2
33 };
34 
44 class DateTime
45 {
46 private: // typedefs
47  using String = BasicString<char8>;
48 
49 public: // construction/destruction
51  DateTime(void);
55  explicit DateTime(int64 ticks, DateTimeKind kind = DateTimeKind::Unspecified);
88  DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind = DateTimeKind::Unspecified);
91  DateTime(const DateTime& arg) = default;
95  DateTime& operator=(const DateTime& arg) = default;
97  ~DateTime(void) = default;
98 
99 public: // operators
103  bool operator==(const DateTime& arg)const;
107  bool operator!=(const DateTime& arg)const;
111  bool operator>(const DateTime& arg)const;
115  bool operator<(const DateTime& arg)const;
119  bool operator>=(const DateTime& arg)const;
123  bool operator<=(const DateTime& arg)const;
124 
125 public: // static operations
128  static DateTime Now(void);
132  static DateTime FromUnixTime(time_t unixTime);
136  ARP_DEPRECATED("Use FromUnixTimeMicroseconds instead")
137  static DateTime FromUnixMicrosecondTicks(uint64 microseconds);
141  static DateTime FromUnixTimeMicroseconds(int64 microseconds);
145  static DateTime FromOpcUaTime(int64 opcUaTime);
149  static DateTime FromIso8601String(const String& value);
153  static DateTime FromBinary(uint64 data);
164  static bool IsLeapYear(int year);
165 
166 public: // setter/getter operations
169  int64 GetTicks(void)const;
173  DateTimeKind GetKind(void)const;
177  int GetYear(void)const;
181  int GetMonth(void)const;
185  int GetDay(void)const;
189  int GetHour(void)const;
193  int GetMinute(void)const;
197  int GetSecond(void)const;
201  int GetMillisecond(void)const;
205  int GetMicrosecond(void)const;
206 
207 public: // operations
210  time_t ToUnixTime(void)const;
213  ARP_DEPRECATED("Use ToUnixTimeMicroseconds instead")
214  uint64 ToUnixMicrosecondTicks(void)const;
217  int64 ToUnixTimeMicroseconds(void)const;
220  uint64 ToBinary(void)const;
223  int64 ToOpcUaTime(void)const;
226  String ToIso8601String(void)const;
227 
228 public: // global stream operator for logging and XML conversion
229  friend std::ostream& operator<<(std::ostream& os, const DateTime& dateTime);
230  friend std::istream& operator>>(std::istream& is, DateTime& dateTime);
231 
232 private: // methods
233  int GetDatePart(int part)const;
234 
235 private: // static methods
236  static int64 DateToTicks(int year, int month, int day);
237  static int64 TimeToTicks(int hour, int minute, int second);
238  static int64 TimeToTicks(int hour, int minute, int second, int millisecond, int microsecond);
239 
240 private: // fields
241  // The data is stored as an unsigned 64-bit integer
242  // Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
243  // 12/31/9999 23:59:59.9999999
244  // Bits 63-64: A four-state value that describes the DateTimeKind value of the date time.
245  uint64 data;
246 
247 private: // static fields
248  static const int kindShift = 62;
249  static const uint64 kindMask = 3ULL << kindShift;
250  static const uint64 ticksMask = ~kindMask;
251 
252  // Number of 100ns ticks per time unit
253  static const int64 ticksPerMicrosecond = 10;
254  static const int64 ticksPerMillisecond = ticksPerMicrosecond * 1000;
255  static const int64 ticksPerSecond = ticksPerMillisecond * 1000;
256  static const int64 ticksPerMinute = ticksPerSecond * 60;
257  static const int64 ticksPerHour = ticksPerMinute * 60;
258  static const int64 ticksPerDay = ticksPerHour * 24;
259 
260  // Number of days in a non-leap year
261  static const int daysPerYear = 365;
262  // Number of days in 4 years
263  static const int daysPer4Years = daysPerYear * 4 + 1;
264  // Number of days in 100 years
265  static const int daysPer100Years = daysPer4Years * 25 - 1;
266  // Number of days in 400 years
267  static const int daysPer400Years = daysPer100Years * 4 + 1;
268 
269  // Number of days from 1/1/0001 to 12/31/1969
270  static const int daysTo1970 = daysPer400Years * 4 + daysPer100Years * 3 + daysPer4Years * 17 + daysPerYear;
271  // Number of days from 1/1/0001 to 12/31/1600
272  static const int daysTo1601 = daysPer400Years * 4;
273  // Number of days from 1/1/0001 to 12/31/9999
274  static const int daysTo10000 = daysPer400Years * 25 - 366;
275 
276  static const int daysToMonth365[];
277  static const int daysToMonth366[];
278 
279  static const int datePartYear = 0;
280  static const int datePartMonth = 2;
281  static const int datePartDayOfYear = 1;
282  static const int datePartDay = 3;
283 
284  // Ticks related
285  static const int64 unix0Ticks = daysTo1970 * ticksPerDay; // 1970.01.01T 00:00:00
286  static const int64 opcua0Ticks = daysTo1601 * ticksPerDay; // 1601.01.01T 00:00:00
287 
288 public: // static fields
291  static const int64 MinTicks = 0;
294  static const int64 MaxTicks = daysTo10000 * ticksPerDay - 1;
295 };
296 
298 // inline methods of class DateTime
299 inline DateTime::DateTime(void)
300  : data(0)
301 {
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 bool DateTime::operator<=(const DateTime& arg) const
330 {
331  return this->GetTicks() <= arg.GetTicks();
332 }
333 
334 inline int64 DateTime::GetTicks() const
335 {
336  return (int64)(this->data & ticksMask);
337 }
338 
340 {
341  return (DateTimeKind)(this->data >> kindShift);
342 }
343 
345 {
346  return DateTime(unix0Ticks + (uint64)microseconds * ticksPerMicrosecond, DateTimeKind::Utc);
347 }
348 
350 inline std::ostream& operator<<(std::ostream& os, const DateTime& dateTime)
351 {
352  os << dateTime.ToIso8601String();
353  return os;
354 }
355 
357 inline std::istream& operator>>(std::istream& is, DateTime& dateTime)
358 {
359  std::string s;
360  is >> s;
361  dateTime = DateTime::FromIso8601String(s);
362  return is;
363 }
364 
366 
367 } // end of namespace Arp
bool operator<(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:319
std::istream & operator>>(std::istream &is, BasicString< CharType, Alloc > &right)
Streams the instream is into the right string.
Definition: BasicString.hxx:1595
bool operator>=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:324
static DateTime FromIso8601String(const String &value)
Creates a DateTime from the as argument passed ISO8601 string.
friend std::ostream & operator<<(std::ostream &os, const DateTime &dateTime)
The ostream operator is used for logging and string formatting and formats the DateTime value in ISO-...
Definition: DateTime.hpp:350
bool operator!=(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on inequality.
Definition: BasicString.hxx:1788
friend std::istream & operator>>(std::istream &is, DateTime &dateTime)
The istream operator is used for string parsing and expects the ISO-8601 format.
Definition: DateTime.hpp:357
std::ostream & operator<<(std::ostream &os, const BasicString< CharType, Alloc > &right)
Streams the right string to the outstream os .
Definition: BasicString.hxx:1584
The class contains date and time informations.
Definition: DateTime.hpp:44
bool operator>(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string.
Definition: BasicString.hxx:1860
bool operator<(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string.
Definition: BasicString.hxx:1824
bool operator==(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:304
static DateTime FromUnixMicrosecondTicks(uint64 microseconds)
Creates a DateTime from the as argument passed unix time in microseconds.
Definition: DateTime.hpp:344
Kind of time in not specified
DateTimeKind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC)...
Definition: DateTime.hpp:25
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:309
bool operator<=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:329
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
bool operator==(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on equality.
Definition: BasicString.hxx:1752
int64 GetTicks(void) const
Gets the number of ticks that represent the date and time of this instance.
Definition: DateTime.hpp:334
bool operator>=(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string.
Definition: BasicString.hxx:1932
DateTimeKind GetKind(void) const
Gets a value that indicates whether the time represented by this instance is based on local time...
Definition: DateTime.hpp:339
String ToIso8601String(void) const
Converts this instance to an ISO8601 string .
DateTime(void)
Constructs an zero&#39;ed DateTime instance.
Definition: DateTime.hpp:299
std::int64_t int64
The Arp integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:41
bool operator<=(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string.
Definition: BasicString.hxx:1896