UniversalDateTime Manual

About

maxon::UniversalDateTime is a MAXON API ALIASES 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

Comparison

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

// 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.");
}
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);

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);

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);

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);

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;

Further Reading

maxon::ReadDocument
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:35
maxon::DST::SET
@ SET
When a date-time object gets converted the Daylight Saving Time is added to the target time.
maxon::UniversalDateTime::GetUnixTimestamp
UInt64 GetUnixTimestamp() const
Definition: datetime.h:266
maxon::JULIANDATE::DUBLIN
@ DUBLIN
Dublin variant with epoch of 12h Dec 31, 1899.
maxon::String
Definition: string.h:1213
maxon::UniversalDateTime::FromUnixTimestamp
static UniversalDateTime FromUnixTimestamp(UInt64 timestamp)
maxon::UniversalDateTime::ToJulianDay
static Float64 ToJulianDay(JULIANDATE variant, Int32 year, UChar month, UChar day, UChar hour, UChar minute, UChar second)
maxon::UniversalDateTime::FromValues
static Result< UniversalDateTime > FromValues(Int32 year, UChar month, UChar day, UChar hour, UChar minute, UChar second)
STANDARD
STANDARD
Create standard material.
Definition: lib_substance.h:226
maxon::TimeValue::SetHours
void SetHours(Float64 hours)
Definition: timevalue.h:195
maxon::Id
Definition: apibaseid.h:250
iferr_return
#define iferr_return
Definition: resultbase.h:1465
maxon::Float64
double Float64
64 bit floating point value (double)
Definition: apibase.h:181
maxon::OPENSTREAMFLAGS::NONE
@ NONE
No flags set.
maxon::UniversalDateTime::GetHashCode
HashInt GetHashCode() const
Returns the hash value of this object. This value can be used for all HashSet<>/HashMap<> classes.
Definition: datetime.h:354
maxon::Float
Float64 Float
Definition: apibase.h:195
maxon::UniversalDateTime::ConvertToLocalDateTime
LocalDateTime ConvertToLocalDateTime() const
maxon::UniversalDateTime
Definition: datetime.h:230
maxon::JULIANDATE::LEGACY
@ LEGACY
Legacy mode with Julian Day + 0.5.
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:167
maxon::UniversalDateTime::Reset
void Reset()
Reset all values to zero.
Definition: datetime.h:236
maxon::JULIANDATE::REDUCED
@ REDUCED
Reduced JD with epoch of 12h Nov 16, 1858.
UChar
maxon::UChar UChar
Definition: ge_sys_math.h:57
maxon::Url
Definition: url.h:855
maxon::UniversalDateTime::FormatTime
String FormatTime(const Char *formatString) const
maxon::JULIANDATE::LILIAN
@ LILIAN
Lilian variant with epoch of Oct 15, 1582[9].
maxon::IOFORMAT::JSON
@ JSON
maxon::JULIANDATE::MARSSOL
@ MARSSOL
Mars Sol Date variant with epoch of 12h Dec 29, 1873.
maxon::LocalDateTime
Definition: datetime.h:90
maxon::JULIANDATE
JULIANDATE
Variants of Julian Day.
Definition: datetime.h:51
Int32
maxon::Int32 Int32
Definition: ge_sys_math.h:60
maxon::WriteDocument
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:67
maxon::UniversalDateTime::FromJulianDay
static UniversalDateTime FromJulianDay(JULIANDATE variant, Float64 j)
maxon::UniversalDateTime::GetNow
static UniversalDateTime GetNow()
maxon::JULIANDATE::MODIFIED
@ MODIFIED
Modified variant with epoch of 0h Nov 17, 1858.
maxon::TimeValue
The TimeValue class encapsulates a timer value.
Definition: timevalue.h:32
maxon::UInt64
uint64_t UInt64
64 bit unsigned integer datatype.
Definition: apibase.h:177
maxon::UInt
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
Definition: apibase.h:187
maxon::JULIANDATE::RATEDIE
@ RATEDIE
Rate Die variant with epoch of Jan 1, 1, proleptic Gregorian calendar.
maxon::DST
DST
Daylight Saving Time information.
Definition: datetime.h:41
UInt64
maxon::UInt64 UInt64
Definition: ge_sys_math.h:63
maxon::UniversalDateTime::GetJulianDay
Float64 GetJulianDay(JULIANDATE variant) const
Float64
maxon::Float64 Float64
Definition: ge_sys_math.h:67
maxon::UniversalDateTime::IsValid
Bool IsValid() const
Definition: datetime.h:322
maxon::JULIANDATE::STANDARD
@ STANDARD
Standard variant with epoch of 12h Jan 1, 4713 BC.