UniversalDateTime Manual

About

maxon::UniversalDateTime is a MAXON API class used to create an object capable to represent Universal date-time (UTC+0000) in a convenient and effective form provided with numerous helpful methods to manage date & time representation.

It's highly recommended to use this class to store any date and time and conversion to maxon::LocalDateTime for a localized output should occur as late as possible to prevent time representation issues. The internal representation is an unsigned 64-bit length integer with the start time set at 01/01/1970-00:00 UTC+0000 and a time resolution of 1 second.

Creation and initialization

A maxon::UniversalDateTime can be created on the stack and initialized using:

// This example allocates a few maxon::UniversalDateTime objects showing different initialization methods.
// allocate a UniversalDateTime object on the stack initialized at the current time
// define a unix-compliant timestamp equal to 01.07.2016 09:00:00
const UInt64 aTimeStamp(1467363600);
// allocate and initialize a second UniversalDateTime object using a unix time stamp
// define a Julian day equal to 01.07.2016 09:00:00
// http://www.onlineconversion.com/julian_date.htm
const Float64 aJulianDay = 2457570.875;
// allocate and initialize a third UniversalDateTime object using a Julian date
// assign a UniversalDateTime given the year, month, day, hours, minutes, seconds
Definition: datetime.h:231
static UniversalDateTime FromJulianDay(JULIANDATE variant, Float64 j)
static Result< UniversalDateTime > FromValues(Int32 year, UChar month, UChar day, UChar hour, UChar minute, UChar second)
static UniversalDateTime FromUnixTimestamp(UInt64 timestamp)
static UniversalDateTime GetNow()
maxon::UInt64 UInt64
Definition: ge_sys_math.h:59
maxon::Float64 Float64
Definition: ge_sys_math.h:63
STANDARD
Create standard material.
Definition: lib_substance.h:0
#define iferr_return
Definition: resultbase.h:1524

Comparison

A maxon::UniversalDateTime can be compared by using standard operators:

  • maxon::UniversalDateTime::operator==(): compares two UniversalDateTime used in expression checking values for equality;
  • maxon::UniversalDateTime::operator<(): compares two UniversalDateTime used in expression checking the first being smaller than the second.
// This example shows how to compare two UniversalDateTime objects
// allocate and initialize a UniversalDateTime
const UInt64 timestamp = 1467363600;
// clone into a new UniversalDateTime
const maxon::UniversalDateTime udtCloned(udt);
// allocate and initialize a UniversalDateTime with a slightly different unix timestamp
// compare the initial with the clone
if (udt == udtCloned)
{
// the two instances are equal
// do something here
DiagnosticOutput("udt and udtCloned are equal.");
}
// compare the initial with the clone
if (udtPast < udt)
{
// the first instance is smaller than the second
// do something here
DiagnosticOutput("udtPast refers to a previous date than udt.");
}
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
Note
From the above operators all the remaining operators (!=, >, >=, <=) are derived and can thus be used as usual.

Conversion

A maxon::UniversalDateTime can be converted to maxon::LocalDateTime using:

// This example shows how to convert a UniversalDateTime to a LocalDateTime / RemoteDateTime
// allocate a UniversalDateTime and initialize to the current time stamp
// convert, in the current timezone, the UniversalDateTime to a LocalDateTime
const maxon::LocalDateTime localNow_currentTZ = udtNow.ConvertToLocalDateTime();
// allocate and initialize a TimeValue
maxon::TimeValue timeoffset;
timeoffset.SetHours(2.0);
// allocate and initialize a maxon::DST
const maxon::DST dstFlag(maxon::DST::SET);
// convert the UniversalDateTime to a RemoteDateTime using the specified offset and the DST information
const maxon::RemoteDateTime remoteNow_offsetDST = udtNow.ConvertToLocalDateTime(timeoffset, dstFlag);
Definition: datetime.h:91
The TimeValue class encapsulates a timer value.
Definition: timevalue.h:33
void SetHours(Float64 hours)
Definition: timevalue.h:189
LocalDateTime ConvertToLocalDateTime() const
DST
Daylight Saving Time information.
Definition: datetime.h:42
@ SET
When a date-time object gets converted the Daylight Saving Time is added to the target time.

Similarly the following methods return maxon::String representations of the maxon::UniversalDateTime via:

Note
maxon::UniversalDateTime::FormatTime() uses the same formatting specified in C++ strftime() function.
// This example shows how to return a properly formatted string representing the data stored in UniversalDateTime.
// allocate and initialize a UniversalDateTime object
const UInt64 timestamp = 1467363600;
// extract useful strings to represent the given timestamp
const maxon::String dayOfTheTS = udt.FormatTime("%A the %j-th day of year %Y");
const maxon::String weekOfTheTS = udt.FormatTime("the date %d/%m/%Y is in %W-th week");
const maxon::String timeOfTS = udt.FormatTime("the time set is %I:%M:%S %p");
DiagnosticOutput("The unix timestamp [@], refers to @, @, and @.", timestamp, dayOfTheTS, weekOfTheTS, timeOfTS);
Definition: string.h:1237
String FormatTime(const Char *formatString) const

Utilities

A few helpful methods are provided with the maxon::UniversalDateTime as utilities:

Note
The Julian day variants have been introduced across the years to adapt to different reference epoch and return a proper Julian day representation of a given time/date. See Variants in Julian day;
// This example shows how to use the utilities found in the UniversalDateTime class
// allocate and initialize an UniversalDateTime at the current timestamp
// convert to the Julian day representation using a legacy variant
// convert to the unix timestamp
const maxon::UInt64 tstamp = udtNow.GetUnixTimestamp();
// retrieve the hash code representation
const maxon::UInt udtHC = udtNow.GetHashCode();
// check the UniversalDateTime's validity
if (udtNow.IsValid())
{
// if valid just reset it
udtNow.Reset();
}
DiagnosticOutput("udtNow has been reset to @", udtNow);
void Reset()
Reset all values to zero.
Definition: datetime.h:236
UInt64 GetUnixTimestamp() const
Definition: datetime.h:266
Float64 GetJulianDay(JULIANDATE variant) const
Bool IsValid() const
Definition: datetime.h:315
Float64 Float
Definition: apibase.h:211
uint64_t UInt64
64 bit unsigned integer datatype.
Definition: apibase.h:193
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
Definition: apibase.h:203
@ LEGACY
Legacy mode with Julian Day + 0.5.

Finally a Julian day can be obtained using:

// This example shows how to compute the Julian day given the required parameters
// set the required parameters
const Int32 year = 2016;
const UChar month = 7;
const UChar day = 1;
const UChar hour = 9;
const UChar min = 0;
const UChar sec = 0;
// compute the Julian day
const maxon::Float64 julianDay = maxon::UniversalDateTime::ToJulianDay(var, year, month, day, hour, min, sec);
// prepare the string representation of the Julian day variant
maxon::String varStr;
switch (var)
{
varStr = "Dublin"_s;
break;
varStr = "Legacy"_s;
break;
varStr = "Lilian"_s;
break;
varStr = "Mars Sol"_s;
break;
varStr = "Modified"_s;
break;
varStr = "Rata die"_s;
break;
varStr = "Reduced"_s;
break;
varStr = "Standard"_s;
break;
}
// show the result
DiagnosticOutput(diagIDString + "On date @/@/@ and time @:@:@ , the Julian day computed using the @ variant is @.", day, month, year, hour, min, sec, varStr, julianDay);
static Float64 ToJulianDay(JULIANDATE variant, Int32 year, UChar month, UChar day, UChar hour, UChar minute, UChar second)
maxon::UChar UChar
Definition: ge_sys_math.h:53
maxon::Int32 Int32
Definition: ge_sys_math.h:56
double Float64
64 bit floating point value (double)
Definition: apibase.h:197
const char Py_ssize_t const char Py_ssize_t min
Definition: modsupport.h:58
JULIANDATE
Variants of Julian Day.
Definition: datetime.h:52
@ DUBLIN
Dublin variant with epoch of 12h Dec 31, 1899.
@ LILIAN
Lilian variant with epoch of Oct 15, 1582[9].
@ MODIFIED
Modified variant with epoch of 0h Nov 17, 1858.
@ REDUCED
Reduced JD with epoch of 12h Nov 16, 1858.
@ STANDARD
Standard variant with epoch of 12h Jan 1, 4713 BC.
@ MARSSOL
Mars Sol Date variant with epoch of 12h Dec 29, 1873.
@ RATEDIE
Rate Die variant with epoch of Jan 1, 1, proleptic Gregorian calendar.
time_t * sec
Definition: pytime.h:57

A maxon::UniversalDateTime object can be read from and written to disk by serializing the data contained using the conventional functions.

// This example shows how to store and retrieve a UniversalDateTime from a file.
// allocate and initialize a UniversalDateTime
// allocate a file URL
const maxon::Url url = (targetFolder + "universaldatetime.txt"_s)iferr_return;
const maxon::Id fileID { "net.maxonexample.universaldatetime" };
// save to file
// read back from file
maxon::ReadDocument(url, fileID, loadedUDT) iferr_return;
Definition: apibaseid.h:243
Definition: url.h:942
std::enable_if< GetCollectionKind< T >::value==COLLECTION_KIND::ARRAY, Result< void > >::type ReadDocument(const Url &url, const Id &id, T &object, const DataDictionary &dict=DataDictionary())
Definition: io.h:40
Result< void > WriteDocument(const Url &url, OPENSTREAMFLAGS flags, const Id &id, const T &object, IOFORMAT format=IOFORMAT::DEFAULT, const DataDictionary &dict=DataDictionary())
Definition: io.h:72
@ NONE
No flags set.

Further Reading