LocalDateTime Manual
maxon::LocalDateTime is a MAXON API class used to create an object capable of representing local time and date in a convenient and effective manner to reduce the effort of handling localized time and date representation. It's indeed recommended to use the maxon::UniversalDateTime whenever possible to store normalized time and date values and convert them the localized as late as possible.
A maxon::LocalDateTime can be created and initialized using:
// allocate a UniversalDateTime object on the stack initialized with the current time const maxon::LocalDateTime ldtNow( maxon::LocalDateTime::GetNow ());
// define a unix-compliant timestamp equal to 01.07.2016 09:00:00 (in UTC time) const UInt64 aTimeStamp(1467363600);
// allocate and initialize a second LocalDateTime object using a unix timestamp // NOTE: using LocalDateTime::FromUnixTimeStamp will add the local timezone to the passed timestamp const maxon::LocalDateTime ldtFromUnixTimestamp( maxon::LocalDateTime::FromUnixTimestamp (aTimeStamp));
// allocate and initialize a third LocalDateTime object using spare date/time values const maxon::LocalDateTime ldtFromValues = maxon::LocalDateTime::FromValues (2016, 7, 1, 9, 0, 0) iferr_return ;
// allocate and initialize a forth LocalDateTime object using a properly formatted date/time string const maxon :: String timedateStr = "2016-07-01 09:00:00"_s; const Char * timedateFrmtStr = "%Y-%m-%d %H:% M :% S "; const maxon ::LocalDateTime ldtFromString = maxon ::LocalDateTime::FromString(timedateStr, timedateFrmtStr) iferr_return ;
A maxon::LocalDateTime can be compared by using standard operators:
// allocate and initialize a LocalDateTime const UInt64 timestamp = 1467363600; const maxon::LocalDateTime ldt = maxon::LocalDateTime::FromUnixTimestamp (timestamp);
// clone into a new LocalDateTime const maxon::LocalDateTime ldtCloned(ldt);
// allocate and initialize a LocalDateTime starting from the first one maxon::LocalDateTime ldtAnother(ldt);
// update the hours ldtAnother._hour -= 2;
// compare the initial with the clone if (ldt == ldtCloned) { // the two instances are equal // do something here DiagnosticOutput ( "ldt and ldtCloned share the same time/date values." ); }
// compare the initial with the clone if (ldtAnother != ldt) { // the first instance is smaller than the second // do something here DiagnosticOutput ( "ldtAnother time/date values are different than those stored in ldt." ); }
A maxon::LocalDateTime can be converted to maxon::UniversalDateTime using:
// allocate and initialize a LocalDateTime to the current time/date const maxon::LocalDateTime ldtNow( maxon::LocalDateTime::GetNow ()); const maxon::UniversalDateTime udtNow = ldtNow.ConvertToUniversalDateTime(); DiagnosticOutput ( "Current date/time in local time is: @" , ldtNow); DiagnosticOutput ( "Current date/time in universal time is: @" , udtNow);
Similarly the following methods return maxon::String representations of the maxon::LocalDateTime via:
// allocate and initialize a timestamp const UInt64 timestamp = 1467363600; // allocate and initialize a LocalDateTime object const maxon::LocalDateTime ldt = maxon::LocalDateTime::FromUnixTimestamp (timestamp);
// extract useful strings to represent the given timestamp const maxon::String dayOfTheTS = ldt. FormatTime ( "%A the %j-th day of year %Y" ); const maxon::String weekOfTheTS = ldt. FormatTime ( "the date %d/%m/%Y is in %W-th week" ); const maxon::String timeOfTS = ldt. FormatTime ( "the time set is %I:%M:%S %p" ); DiagnosticOutput ( "Today [@] is @, @, and @." , ldt. ToString ( nullptr ), dayOfTheTS, weekOfTheTS, timeOfTS);
A few helpful methods are provided with the maxon::LocalDateTime as utilities:
// allocate and initialize a LocalDateTime to the current time const maxon::LocalDateTime ldtNow( maxon::LocalDateTime::GetNow ());
// retrieve the status about the leap year const maxon::Bool isLeapYear(ldtNow.IsLeapYear());
// retrieve the hash code representation const maxon::UInt ldtHash = ldtNow.GetHashCode(); DiagnosticOutput ( "Current date [@] hash-code is @" , ldtNow, ldtHash); if (isLeapYear) DiagnosticOutput ( "Current date [@] belongs to a leap year" , ldtNow); else DiagnosticOutput ( "Current date [@] doesn't belong to a leap year" , ldtNow);
// retrieve the information about the timezone offset maxon::Bool isDSTaffecting; const maxon::TimeValue timezoneOffset = ldtNow.GetTimezoneOffset(&isDSTaffecting); if (isDSTaffecting) DiagnosticOutput ( "Local date/time is offsetted by @h respect to UTC and DST is affecting the offset" , timezoneOffset. GetHours ()); else DiagnosticOutput ( "Local date/time is offsetted by @h respect to UTC" , timezoneOffset. GetHours ()); const Int32 year = 2016; const UChar month = 12; const UChar day = 21;
// retrieve the status about the leap year for a given date const maxon::Bool isAnotherLeapYear( maxon::LocalDateTime::IsLeapYear (year)); if (isAnotherLeapYear) DiagnosticOutput ( "@ was a leap year. " , year); else DiagnosticOutput ( "@ wasn't a leap year. " , year);
// check the current day of the week const maxon::DAYOFWEEK dayOfWeek = maxon::LocalDateTime::GetDayOfWeek (year, month, day);
// prepare the string representation of the day maxon::String dayStr; switch (dayOfWeek) { case maxon::DAYOFWEEK::MONDAY : dayStr = "Monday" _s; break ; case maxon::DAYOFWEEK::TUESDAY : dayStr = "Tuesday" _s; break ; case maxon::DAYOFWEEK::WEDNESDAY : dayStr = "Wednesday" _s; break ; case maxon::DAYOFWEEK::THURSDAY : dayStr = "Thursday" _s; break ; case maxon::DAYOFWEEK::FRIDAY : dayStr = "Friday" _s; break ; case maxon::DAYOFWEEK::SATURDAY : dayStr = "Saturday" _s; break ; case maxon::DAYOFWEEK::SUNDAY : dayStr = "Sunday" _s; break ; } DiagnosticOutput ( "On @/@/@ was a @. " , day, month, year, dayStr);