UniversalDateTime Manual

内容表

关于

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 const maxon::UniversalDateTime udtNow( maxon::UniversalDateTime::GetNow ());

// 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 const maxon::UniversalDateTime udtFromUnixTimestamp( maxon::UniversalDateTime::FromUnixTimestamp (aTimeStamp));

// 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 const maxon::UniversalDateTime udtFromJulianDay( maxon::UniversalDateTime::FromJulianDay ( JULIANDATE::STANDARD , aJulianDay));

// assign a UniversalDateTime given the year, month, day, hours, minutes, seconds const maxon::UniversalDateTime udtFromValues = maxon::UniversalDateTime::FromValues (2016, 7, 1, 9, 0, 0) iferr_return ;

比较

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; const maxon::UniversalDateTime udt = maxon::UniversalDateTime::FromUnixTimestamp (timestamp);

// clone into a new UniversalDateTime const maxon::UniversalDateTime udtCloned(udt);

// allocate and initialize a UniversalDateTime with a slightly different unix timestamp const maxon::UniversalDateTime udtPast = maxon::UniversalDateTime::FromUnixTimestamp (1467363600 - 1);

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

注意
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 const maxon::UniversalDateTime udtNow = maxon::UniversalDateTime::GetNow ();

// 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:

注意
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; const maxon::UniversalDateTime udt = maxon::UniversalDateTime::FromUnixTimestamp (timestamp);

// 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:

注意
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 maxon::UniversalDateTime udtNow = maxon::UniversalDateTime::GetNow ();

// convert to the Julian day representation using a legacy variant const maxon::Float jday = udtNow. GetJulianDay ( maxon::JULIANDATE::LEGACY );

// 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. 重置 (); } 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 JULIANDATE var = maxon::JULIANDATE::STANDARD ; 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) { case maxon::JULIANDATE::DUBLIN : varStr = "Dublin" _s; break ; case maxon::JULIANDATE::LEGACY : varStr = "Legacy" _s; break ; case maxon::JULIANDATE::LILIAN : varStr = "Lilian" _s; break ; case maxon::JULIANDATE::MARSSOL : varStr = "Mars Sol" _s; break ; case maxon::JULIANDATE::MODIFIED : varStr = "Modified" _s; break ; case maxon::JULIANDATE::RATEDIE : varStr = "Rata die" _s; break ; case maxon::JULIANDATE::REDUCED : varStr = "Reduced" _s; break ; case maxon::JULIANDATE::STANDARD : 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 const maxon::UniversalDateTime savedUDT = maxon::UniversalDateTime::GetNow ();

// allocate a file URL const maxon::Url url = (targetFolder + "universaldatetime.txt" _s) iferr_return ; const maxon::Id fileID { "net.maxonexample.universaldatetime" };

// save to file maxon::WriteDocument (url, maxon::OPENSTREAMFLAGS::NONE , fileID, savedUDT, maxon::IOFORMAT::JSON ) iferr_return ;

// read back from file maxon::UniversalDateTime loadedUDT; maxon::ReadDocument (url, fileID, loadedUDT) iferr_return ;

延伸阅读

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())
定义: 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
定义: datetime.h:267
maxon::JULIANDATE::DUBLIN
@ DUBLIN
Dublin variant with epoch of 12h Dec 31, 1899.
maxon::String
定义: string.h:1197
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.
定义: lib_substance.h:226
maxon::TimeValue::SetHours
void SetHours(Float64 hours)
定义: timevalue.h:196
maxon::Id
定义: apibaseid.h:250
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::Float64
double Float64
64 bit floating point value (double)
定义: apibase.h:179
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.
定义: datetime.h:345
maxon::Float
Float64 Float
定义: apibase.h:193
maxon::UniversalDateTime::ConvertToLocalDateTime
LocalDateTime ConvertToLocalDateTime() const
maxon::UniversalDateTime
定义: datetime.h:231
maxon::JULIANDATE::LEGACY
@ LEGACY
Legacy mode with Julian Day + 0.5.
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::UniversalDateTime::Reset
void Reset()
Reset all values to zero.
定义: datetime.h:237
maxon::JULIANDATE::REDUCED
@ REDUCED
Reduced JD with epoch of 12h Nov 16, 1858.
UChar
maxon::UChar UChar
定义: ge_sys_math.h:55
maxon::Url
定义: url.h:819
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
定义: datetime.h:91
maxon::JULIANDATE
JULIANDATE
Variants of Julian Day.
定义: datetime.h:52
Int32
maxon::Int32 Int32
定义: ge_sys_math.h:58
maxon::WriteDocument
Result< void > WriteDocument(const Url &url, OPENSTREAMFLAGS flags, const Id &id, const T &object, IOFORMAT format=IOFORMAT::DEFAULT, const DataDictionary &dict=DataDictionary())
定义: 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.
定义: timevalue.h:33
maxon::UInt64
uint64_t UInt64
64 bit unsigned integer datatype.
定义: apibase.h:175
maxon::UInt
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
定义: apibase.h:185
maxon::JULIANDATE::RATEDIE
@ RATEDIE
Rate Die variant with epoch of Jan 1, 1, proleptic Gregorian calendar.
maxon::DST
DST
Daylight Saving Time information.
定义: datetime.h:42
UInt64
maxon::UInt64 UInt64
定义: ge_sys_math.h:61
maxon::UniversalDateTime::GetJulianDay
Float64 GetJulianDay(JULIANDATE variant) const
Float64
maxon::Float64 Float64
定义: ge_sys_math.h:65
maxon::UniversalDateTime::IsValid
Bool IsValid() const
定义: datetime.h:323
maxon::JULIANDATE::STANDARD
@ STANDARD
Standard variant with epoch of 12h Jan 1, 4713 BC.

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1