PLCnext API Documentation 23.0.2.9
DateTime.hpp
1
2//
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
15namespace Arp
16{
17
20
25enum class DateTimeKind
26{
28 Unspecified = 0,
30 Utc = 1,
32 // Local = 2
33};
34
45{
46private: // typedefs
48
49public: // construction/destruction
51 DateTime(void);
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
99public: // 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
125public: // static operations
128 static DateTime Now(void);
132 static DateTime FromUnixTime(time_t unixTime);
137 ARP_DEPRECATED("Use FromUnixTimeMicroseconds instead")
138 static DateTime FromUnixMicrosecondTicks(uint64 microseconds);
146 static DateTime FromOpcUaTime(int64 opcUaTime);
150 static DateTime FromIso8601String(const String& value);
165 static bool IsLeapYear(int year);
166
167public: // setter/getter operations
170 int64 GetTicks(void)const;
174 DateTimeKind GetKind(void)const;
178 int GetYear(void)const;
182 int GetMonth(void)const;
186 int GetDay(void)const;
190 int GetHour(void)const;
194 int GetMinute(void)const;
198 int GetSecond(void)const;
202 int GetMillisecond(void)const;
206 int GetMicrosecond(void)const;
207
208public: // operations
211 time_t ToUnixTime(void)const;
222 uint64 ToBinary(void)const;
225 int64 ToOpcUaTime(void)const;
229
230public: // global stream operator for logging and XML conversion
231 friend std::ostream& operator<<(std::ostream& os, const DateTime& dateTime);
232 friend std::istream& operator>>(std::istream& is, DateTime& dateTime);
233
234private: // methods
235 int GetDatePart(int part)const;
236
237private: // static methods
238 static int64 DateToTicks(int year, int month, int day);
239 static int64 TimeToTicks(int hour, int minute, int second);
240 static int64 TimeToTicks(int hour, int minute, int second, int millisecond, int microsecond);
241
242private: // fields
243 // The data is stored as an unsigned 64-bit integer
244 // Bits 01-62: The value of 100-nanosecond ticks where 0 represents 1/1/0001 12:00am, up until the value
245 // 12/31/9999 23:59:59.9999999
246 // Bits 63-64: A four-state value that describes the DateTimeKind value of the date time.
247 uint64 data;
248
249private: // static fields
250 static const int kindShift = 62;
251 static const uint64 kindMask = 3ULL << kindShift;
252 static const uint64 ticksMask = ~kindMask;
253
254 // Number of 100ns ticks per time unit
255 static const int64 ticksPerMicrosecond = 10;
256 static const int64 ticksPerMillisecond = ticksPerMicrosecond * 1000;
257 static const int64 ticksPerSecond = ticksPerMillisecond * 1000;
258 static const int64 ticksPerMinute = ticksPerSecond * 60;
259 static const int64 ticksPerHour = ticksPerMinute * 60;
260 static const int64 ticksPerDay = ticksPerHour * 24;
261
262 // Number of days in a non-leap year
263 static const int daysPerYear = 365;
264 // Number of days in 4 years
265 static const int daysPer4Years = daysPerYear * 4 + 1;
266 // Number of days in 100 years
267 static const int daysPer100Years = daysPer4Years * 25 - 1;
268 // Number of days in 400 years
269 static const int daysPer400Years = daysPer100Years * 4 + 1;
270
271 // Number of days from 1/1/0001 to 12/31/1969
272 static const int daysTo1970 = daysPer400Years * 4 + daysPer100Years * 3 + daysPer4Years * 17 + daysPerYear;
273 // Number of days from 1/1/0001 to 12/31/1600
274 static const int daysTo1601 = daysPer400Years * 4;
275 // Number of days from 1/1/0001 to 12/31/9999
276 static const int daysTo10000 = daysPer400Years * 25 - 366;
277
278 static const int daysToMonth365[];
279 static const int daysToMonth366[];
280
281 static const int datePartYear = 0;
282 static const int datePartMonth = 2;
283 static const int datePartDayOfYear = 1;
284 static const int datePartDay = 3;
285
286 // Ticks related
287 static const int64 unix0Ticks = daysTo1970 * ticksPerDay; // 1970.01.01T 00:00:00
288 static const int64 opcua0Ticks = daysTo1601 * ticksPerDay; // 1601.01.01T 00:00:00
289
290public: // static fields
293 static const int64 MinTicks = 0;
296 static const int64 MaxTicks = daysTo10000 * ticksPerDay - 1;
297};
298
300// inline methods of class DateTime
301inline DateTime::DateTime(void)
302 : data(0)
303{
304}
305
306inline bool DateTime::operator==(const DateTime& arg) const
307{
308 return this->GetTicks() == arg.GetTicks();
309}
310
311inline bool DateTime::operator!=(const DateTime& arg) const
312{
313 return this->GetTicks() != arg.GetTicks();
314}
315
316inline bool DateTime::operator>(const DateTime& arg) const
317{
318 return this->GetTicks() > arg.GetTicks();
319}
320
321inline bool DateTime::operator<(const DateTime& arg) const
322{
323 return this->GetTicks() < arg.GetTicks();
324}
325
326inline bool DateTime::operator>=(const DateTime& arg) const
327{
328 return this->GetTicks() >= arg.GetTicks();
329}
330
331inline bool DateTime::operator<=(const DateTime& arg) const
332{
333 return this->GetTicks() <= arg.GetTicks();
334}
335
337{
338 return (int64)(this->data & ticksMask);
339}
340
342{
343 return (DateTimeKind)(this->data >> kindShift);
344}
345
347{
348 return DateTime(unix0Ticks + (uint64)microseconds * ticksPerMicrosecond, DateTimeKind::Utc);
349}
350
352inline std::ostream& operator<<(std::ostream& os, const DateTime& dateTime)
353{
354 os << dateTime.ToIso8601String();
355 return os;
356}
357
359inline std::istream& operator>>(std::istream& is, DateTime& dateTime)
360{
361 std::string s;
362 is >> s;
363 dateTime = DateTime::FromIso8601String(s);
364 return is;
365}
366
368
369} // end of namespace Arp
The class contains date and time informations.
Definition: DateTime.hpp:45
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:296
int GetDay(void) const
Gets the day of the month component of the date represented by this instance.
DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, int microsecond, DateTimeKind kind=DateTimeKind::Unspecified)
Initializes a new instance of the T:System.DateTime structure to the specified year,...
~DateTime(void)=default
Destructs this instance and frees all resources.
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.
DateTime & operator=(const DateTime &arg)=default
The default assignment operator.
static const int64 MinTicks
The minimal tick count.
Definition: DateTime.hpp:293
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.
DateTime(int64 ticks, DateTimeKind kind=DateTimeKind::Unspecified)
Constructs an DateTime instance.
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)
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:359
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
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.
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:352
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.
DateTime(const DateTime &arg)=default
The default copy constructor.
bool operator!=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:311
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:336
bool operator<(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:321
bool operator==(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:306
DateTimeKind
Specifies whether a DateTime object represents a local time, a Coordinated Universal Time (UTC),...
Definition: DateTime.hpp:26
bool operator>(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:316
bool operator>=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:326
std::int64_t int64
The Arp integer type of 8 byte size.
Definition: PrimitiveTypes.hpp:41
bool operator<=(const DateTime &arg) const
Compares the ticks of this instance to arg .
Definition: DateTime.hpp:331
DateTimeKind GetKind(void) const
Gets a value that indicates whether the time represented by this instance is based on local time,...
Definition: DateTime.hpp:341
static DateTime FromUnixMicrosecondTicks(uint64 microseconds)
Creates a DateTime from the as argument passed unix time in microseconds.
Definition: DateTime.hpp:346
DateTime(void)
Constructs an zero'ed DateTime instance.
Definition: DateTime.hpp:301
@ Utc
Time is in UTC.
@ Unspecified
Kind of time in not specified
Root namespace for the PLCnext API
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
The class implements an adapter for enums to define the string literals of the enum entries.
Definition: EnumStrings.hxx:38
Namespace of the C++ standard library