DateTimeData Manual
A
DateTimeData
stores a date and a time. Using a
DateTime
structure, it stores the year, month, day, hour, minute and second.
For more specific operation related to time, see
TimeValue Manual
.
使用 C4DAtom::GetParameter() to retrieve the data of a parameter and cast it to DateTimeData .
// This example retrieves the actual DateTime from a sky object.// Checks if we retrieved the data and its type. if (!success || data. GetType () != DATETIME_DATA ) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );
// Retrieves the data from data container. DateTimeData * dateTimeData = static_cast< DateTimeData * > (data. GetCustomDataType ( DATETIME_DATA ));
// Converts DateTime To string. maxon::String dateString = DateToString (timeRetrieved); maxon::String timeString = TimeToString (timeRetrieved);
// Outputs the result. DiagnosticOutput ( "the date is @ and the time is @" , dateString, timeString);
// Allocates a new DateTimeData. AutoAlloc<DateTimeData> dateTimeData; if (dateTimeData == nullptr ) return maxon::NullptrError( MAXON_SOURCE_LOCATION );
// Creates a Date time. DateTime dateTime(1912, 6, 23, 23, 59, 59);
// Sets the DateTimeData. dateTimeData-> SetDateTime (dateTime);
使用
C4DAtom::SetParameter()
to set the
DateTimeData
of your parameter.
The custom data type must be specified in the
GeData
constructor.
A parameter can be added to a description with the function ::GetDDescription(). To attach a custom GUI to that parameter define DESC_CUSTOMGUI settings and call SetParameter(). See DateTimeControl Manual .
// This example shows how to implement a BaseTimeData parameter with a custom GUI. DescID cid = DescLevel (dateTimeID, DATETIME_DATA , 0); if (!singleid || cid. IsPartOf (*singleid, nullptr )) { BaseContainer settings = GetCustomDataTypeDefault ( DATETIME_DATA ); // Sets the name 设置。 SetString ( DESC_NAME , "Date Time" _s); 设置。 SetString ( DESC_SHORT_NAME , "Date Time" _s);// Sets the custom GUI displaying that parameter. // GetCustomDataTypeDefault sets it to DATETIME_GUI by default. 设置。 SetInt32 ( DESC_CUSTOMGUI , DATETIME_GUI );
// Activates the now button 设置。 SetBool ( DATETIME_NOW_BUTTON , true );
// Activates the clock 设置。 SetBool ( DATETIME_TIME_CONTROL , true );
// Activates the calendar 设置。 SetBool ( DATETIME_DATE_CONTROL , true );
// The maximized GUI is shown by default. 设置。 SetBool ( DESC_GUIOPEN , true );
// Inserts the parameter in the description if (!description-> SetParameter (cid, settings, ID_OBJECTPROPERTIES )) return false ; }
Represents a date and a time.
Inside DateTimeData , DataTime is only accessible via those functions:
// Creates a DateTime for Alan Turing's birthday. DateTime timeSet(1912, 6, 23, 23, 59, 59);
// Allocates a new DateTimeData. AutoAlloc<DateTimeData> dateTimeData; if (dateTimeData == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );
// Sets the DateTimeData to the new time/date. dateTimeData-> SetDateTime (timeSet);
// Retrieves the DateTime From DateTimeData. const DateTime timeGet = dateTimeData-> GetDateTime ();
The Structure of the DateTime is composed by those properties.
A DateTime can be created passing some argument in the constructor as follow:
DateTime (Int32 t_year, Int32 t_month, Int32 t_day, Int32 t_hour, Int32 t_minute, Int32 t_second);
// Allocates a new DateTimeData using AutoAlloc. AutoAlloc<DateTimeData> dateTimeData; if (dateTimeData == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );// Released the pointer if needed. DateTimeData * anotherPointer = dateTimeData. 发行 ();
// Another way to Alloc a DateTimeData using GeData. GeData data = GeData ( DATETIME_DATA , DEFAULTVALUE ); DateTimeData * value = static_cast< DateTimeData * > (data. GetCustomDataType ( DATETIME_DATA )); if (value == nullptr ) return maxon::OutOfMemoryError( MAXON_SOURCE_LOCATION );
// Creates a Date time. DateTime dateTime(1912, 6, 23, 23, 59, 59);
// Sets the DateTimeData. value-> SetDateTime (dateTime);
// Frees the pointer as we took ownership. DateTimeData::Free (anotherPointer);
There are several functions to manipulate dates and times.
DateTime can be converted as a String with:
// Sets a DateTime to now. DateTime now; GetDateTimeNow (now);
// Retrieves the month's name in the current Cinema 4D interface language. const maxon::String month = GetMonthName (now. month );
// Outputs the result. DiagnosticOutput ( "The actual monmth is @" , month); DateTime can also be parsed from String to Int32 with thoses functions:
// Checks the result of the function. if (! ParseTimeString (time, hrs, min, sec)) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION ); DiagnosticOutput ( "This time @ can be parse to @ hours @ minutes @ seconds" , time, hrs, min, sec);
Transforms DateTime to Julian date in both directions.
// Outputs "2457382.523". DiagnosticOutput ( "The date 25 December 2015 converted to a julian day is @" , julianDay);
// Outputs "2457382.023". julianDay -= 0.5; DiagnosticOutput ( "To get the correct Julian day, subtract 0.5 @" , julianDay);
// We need to add 0.5 to a Julian day before using FromJulianDay. julianDay += 0.5; const DateTime backToTime = FromJulianDay (julianDay); // Outputs "12:32:59 - Friday - 25 / Dec / 2014". DiagnosticOutput ( "Date from julian day is @" , FormatString ( FormatTime (pszFormat, backToTime)));
Retrieves the current date and time.
Converts date and time to GMT or local date and time.
// Sets a DateTime to now. DateTime now; GetDateTimeNow (now); DateTime outGMT; if ( LocalToGMTime (now, outGMT)) { const maxon::String localTimeToString = FormatTime ( "%H:%M:%S - %A - %d / %b / %G" , now); const maxon::String gmtTimeToString = FormatTime ( "%H:%M:%S - %A - %d / %b / %G" , outGMT); DiagnosticOutput ( "Local time @ to GMT result is @" , localTimeToString, gmtTimeToString); } else { DiagnosticOutput ( "Failed to convert the local time to a GMT" ); }
Gets the day of the week.
// Create an array for all days of the week. const maxon::String days[7] = { "Monday" _s, "Tuesday" _s, "Wednesday" _s, "Thursday" _s, "Friday" _s, "Saturday" _s, "Sunday" _s };
// GetDayOfWeek return a DAYOFWEEK so we need to cast that into a Int32 to retrieves the text in our days' array. const maxon::Int32 day = static_cast< maxon::Int32 > ( GetDayOfWeek (1912, 6, 23));
// Outputs the result. DiagnosticOutput ( "Alan Turing is born a @" , days[day]);
Tries to correct invalid data.
There are several operators that can be used to compare DateTime .
// Check if both value are equals. DiagnosticOutput ( "Compare DateTime : @ and @ equality is @" , FormatString ( FormatTime (pszFormat, t1)), FormatString ( FormatTime (pszFormat, t2)), t1 == t2); // Another way to compare DateTime is using the function. // Result is guaranty to be less than 0 , equal to 0 , or greater than 0. maxon::Int32 result = CompareDateTime (t1, t2); if (result < 0) DiagnosticOutput ( "@ is less than @" , FormatString ( FormatTime (pszFormat, t1)), FormatString ( FormatTime (pszFormat, t2))); else if (result == 0) DiagnosticOutput ( "@ is equal to @" , FormatString ( FormatTime (pszFormat, t1)), FormatString ( FormatTime (pszFormat, t2))); else DiagnosticOutput ( "@ is greater than @" , FormatString ( FormatTime (pszFormat, t1)), FormatString ( FormatTime (pszFormat, t2)));