PLCnext API Documentation 24.0.0.71
DateTime.hpp
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8#include "Arp/System/Core/PrimitiveTypes.hpp"
9#include <ctime>
10#include <iostream>
11
12#ifndef ARP_INSIDE_ARP_H
13 #error Never include 'DateTime.hpp' directly, just include 'Arp.h'
14#endif
15
16namespace Arp
17{
18
21
26enum class DateTimeKind
27{
29 Unspecified = 0,
31 Utc = 1,
33 // Local = 2
34};
35
46{
47private: // typedefs
49
50public: // construction/destruction
52 DateTime(void);
58 ARP_DEPRECATED("Use overload with DateTimeKind parameter instead.")
59 explicit DateTime(int64 ticks);
99 ARP_DEPRECATED("Use overload with DateTimeKind parameter instead.")
100 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond);
133 DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind);
136 DateTime(const DateTime& arg) = default;
140 DateTime& operator=(const DateTime& arg) = default;
142 ~DateTime(void) = default;
143
144public: // operators
148 bool operator==(const DateTime& arg)const;
152 bool operator!=(const DateTime& arg)const;
156 bool operator>(const DateTime& arg)const;
160 bool operator<(const DateTime& arg)const;
164 bool operator>=(const DateTime& arg)const;
168 bool operator<=(const DateTime& arg)const;
169
170public: // static operations
173 static DateTime GetUtcNow(void);
177 ARP_DEPRECATED("Use GetUtcNow instead")
178 static DateTime Now(void);
182 static DateTime FromUnixTime(time_t unixTime);
188 static DateTime FromUnixMicrosecondTicks(uint64 microseconds);
196 static DateTime FromOpcUaTime(int64 opcUaTime);
200 static DateTime FromIso8601String(const String& value);
215 static bool IsLeapYear(int year);
216
217public: // setter/getter operations
220 int64 GetTicks(void)const;
224 DateTimeKind GetKind(void)const;
228 int GetYear(void)const;
232 int GetMonth(void)const;
236 int GetDay(void)const;
240 int GetHour(void)const;
244 int GetMinute(void)const;
248 int GetSecond(void)const;
252 int GetMillisecond(void)const;
256 int GetMicrosecond(void)const;
261 bool IsEmpty(void)const;
262
263public: // operations
266 time_t ToUnixTime(void)const;
277 uint64 ToBinary(void)const;
280 int64 ToOpcUaTime(void)const;
284
285public: // global stream operator for logging and XML conversion
286 friend std::ostream& operator<<(std::ostream& os, const DateTime& dateTime);
287 friend std::istream& operator>>(std::istream& is, DateTime& dateTime);
288
289private: // methods
290 int GetDatePart(int part)const;
291
292private: // static methods
293 static int64 DateToTicks(int year, int month, int day);
294 static int64 TimeToTicks(int hour, int minute, int second);
295 static int64 TimeToTicks(int hour, int minute, int second, int millisecond, int microsecond);
296
297private: // fields
298 // The data is stored as an unsigned 64-bit integer
299 // Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
300 // 12/31/9999 23:59:59.9999999
301 // Bits 63-64: A four-state value that describes the DateTimeKind value of the date time.
302 uint64 data;
303
304private: // static fields
305 static const int kindShift = 62;
306 static const uint64 kindMask = 3ULL << kindShift;
307 static const uint64 ticksMask = ~kindMask;
308
309 // Number of 100ns ticks per time unit
310 static const int64 ticksPerMicrosecond = 10;
311 static const int64 ticksPerMillisecond = ticksPerMicrosecond * 1000;
312 static const int64 ticksPerSecond = ticksPerMillisecond * 1000;
313 static const int64 ticksPerMinute = ticksPerSecond * 60;
314 static const int64 ticksPerHour = ticksPerMinute * 60;
315 static const int64 ticksPerDay = ticksPerHour * 24;
316
317 // Number of days in a non-leap year
318 static const int daysPerYear = 365;
319 // Number of days in 4 years
320 static const int daysPer4Years = daysPerYear * 4 + 1;
321 // Number of days in 100 years
322 static const int daysPer100Years = daysPer4Years * 25 - 1;
323 // Number of days in 400 years
324 static const int daysPer400Years = daysPer100Years * 4 + 1;
325
326 // Number of days from 1/1/0001 to 12/31/1969
327 static const int daysTo1970 = daysPer400Years * 4 + daysPer100Years * 3 + daysPer4Years * 17 + daysPerYear;
328 // Number of days from 1/1/0001 to 12/31/1600
329 static const int daysTo1601 = daysPer400Years * 4;
330 // Number of days from 1/1/0001 to 12/31/9999
331 static const int daysTo10000 = daysPer400Years * 25 - 366;
332
333 static const int daysToMonth365[];
334 static const int daysToMonth366[];
335
336 static const int datePartYear = 0;
337 static const int datePartMonth = 2;
338 static const int datePartDayOfYear = 1;
339 static const int datePartDay = 3;
340
341 // Ticks related
342 static const int64 unix0Ticks = daysTo1970 * ticksPerDay; // 1970.01.01T 00:00:00
343 static const int64 opcua0Ticks = daysTo1601 * ticksPerDay; // 1601.01.01T 00:00:00
344
345public: // static fields
348 static const int64 MinTicks = 0;
351 static const int64 MaxTicks = daysTo10000 * ticksPerDay - 1;
352};
353
355// inline methods of class DateTime
356inline DateTime::DateTime(void)
357 : data(0)
358{
359}
360
361inline bool DateTime::operator==(const DateTime& arg) const
362{
363 return this->GetTicks() == arg.GetTicks();
364}
365
366inline bool DateTime::operator!=(const DateTime& arg) const
367{
368 return this->GetTicks() != arg.GetTicks();
369}
370
371inline bool DateTime::operator>(const DateTime& arg) const
372{
373 return this->GetTicks() > arg.GetTicks();
374}
375
376inline bool DateTime::operator<(const DateTime& arg) const
377{
378 return this->GetTicks() < arg.GetTicks();
379}
380
381inline bool DateTime::operator>=(const DateTime& arg) const
382{
383 return this->GetTicks() >= arg.GetTicks();
384}
385
386inline bool DateTime::operator<=(const DateTime& arg) const
387{
388 return this->GetTicks() <= arg.GetTicks();
389}
390
392{
393 return (int64)(this->data & ticksMask);
394}
395
397{
398 return (DateTimeKind)(this->data >> kindShift);
399}
400
402{
403 return DateTime(unix0Ticks + (uint64)microseconds * ticksPerMicrosecond, DateTimeKind::Utc);
404}
405
407inline std::ostream& operator<<(std::ostream& os, const DateTime& dateTime)
408{
409 os << dateTime.ToIso8601String();
410 return os;
411}
412
414inline std::istream& operator>>(std::istream& is, DateTime& dateTime)
415{
416 std::string s;
417 is >> s;
418 dateTime = DateTime::FromIso8601String(s);
419 return is;
420}
421
423
424} // end of namespace Arp
The class contains date and time informations.
Definition: DateTime.hpp:46
uint64 ToBinary(void) const
Converts this instance to a binary representation.
static DateTime FromIso8601String(const String &value)
Creates a DateTime from the as argument passed ISO8601 string.
int64 ToUnixTimeMicroseconds(void) const
Converts this instance to unix time (UTC) in microseconds.
static const int64 MaxTicks
The maximal tick count.
Definition: DateTime.hpp:351
int GetDay(void) const
Gets the day of the month component of the date represented by this instance.
int GetMillisecond(void) const
Gets the millisecond component of the date represented by this instance.
static DateTime FromOpcUaTime(int64 opcUaTime)
Creates a DateTime from the argument passed in OpcUA time.
static const int64 MinTicks
The minimal tick count.
Definition: DateTime.hpp:348
static DateTime FromUnixTimeMicroseconds(int64 microseconds)
Creates a DateTime from the as argument passed unix time in microseconds.
String ToIso8601String(void) const
Converts this instance to an ISO8601 string .
int GetYear(void) const
Gets the year component of the date represented by this instance.
static DateTime GetUtcNow(void)
Gets the current time as DateTime, expressed as the UTC time.
int64 ToOpcUaTime(void) const
Converts this instance to OPC time (UTC).
int GetMinute(void) const
Gets the minute component of the date represented by this instance.
int GetHour(void) const
Gets the hour component of the date represented by this instance.
static DateTime Now(void)
Deprecated! Gets the current time as DateTime, expressed as the UTC time.
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:414
static bool IsLeapYear(int year)
Returns an indication whether the specified year is a leap year.
time_t ToUnixTime(void) const
Converts this instance to unix time (UTC).
static DateTime FromUnixTime(time_t unixTime)
Creates a DateTime from the as argument passed unix time.
uint64 ToUnixMicrosecondTicks(void) const
Deprecated! Converts this instance to unix time (UTC) in microseconds.
static DateTime FromBinary(uint64 data)
Creates a DateTime from the as argument passed binary representation.
int GetSecond(void) const
Gets the second component of the date represented by this instance.
bool IsEmpty(void) const
Checks if this instance represents a valid date.
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:407
int GetMonth(void) const
Gets the month component of the date represented by this instance.
int GetMicrosecond(void) const
Gets the microsecond component of the date represented by this instance.
bool operator!=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:366
std::uint64_t uint64
The Arp unsigned integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:40
int64 GetTicks(void) const
Gets the number of ticks that represent the date and time of this instance.
Definition: DateTime.hpp:391
bool operator<(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:376
bool operator==(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:361
DateTimeKind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC),...
Definition: DateTime.hpp:27
bool operator>(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:371
bool operator>=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:381
std::int64_t int64
The Arp integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:42
bool operator<=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:386
DateTimeKind GetKind(void) const
Gets a value that indicates whether the time represented by this instance is based on local time,...
Definition: DateTime.hpp:396
static DateTime FromUnixMicrosecondTicks(uint64 microseconds)
Deprecated! Creates a DateTime from the as argument passed unix time in microseconds.
Definition: DateTime.hpp:401
DateTime(void)
Constructs an zero'ed DateTime instance.
Definition: DateTime.hpp:356
@ Utc
Time is in UTC.
Root namespace for the PLCnext API
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38
Namespace of the C++ standard library