PrevNextUpHome SophiaFramework UNIVERSE 5.3

20.4. SFXDate Class and SFXDateDuration Class: for managing date and time duration

SFXDate is the class that manages year, month, date, hour, minute, and second values of a specific instant in time.

SFXDateDuration is the class that manages the time duration in seconds.

Example 20.14. Define the date

SFXDate date(2008, 10, 12, 23, 45, 58);   // 23:45:58, October 12, 2008

Example 20.15. Get the date

SInt32 n1 = date.GetYear();   // n1 = 2008
SInt32 n2 = date.GetMonth();  // n2 = 10
SInt32 n3 = date.GetDay();    // n3 = 12
SInt32 n4 = date.GetHour();   // n4 = 23
SInt32 n5 = date.GetMinute(); // n5 = 45
SInt32 n6 = date.GetSecond(); // n6 = 58

Example 20.16. Operate the date

date.AddYear(1);   // year = 2009
date.AddSecond(3); // second = 1, minute  = 46
date.SubMonth(30); // month = 4, year = 2007

Example 20.17. Get the current time

date = SFXDate::CurrentDate();

Example 20.18. Output the date with format

SFXAnsiString str;
SFXDate date(2008, 10, 12, 23, 45, 58);

// format output
str = date.Format("YYYY/MM/DD hh:mm:ss Wek");
// str = "2008/10/12 23:45:58 Sun" 

Date can be formated as follows:

Table 20.2. Format output table

Format Description Example
YY 2-digit year 99 07
YYYY 4-digit year 1999 2007
MM 2-digit month from 01 to 12
DD 2-digit date from 01 to 31
hh 2-digit hour from 00 to 23
HH 2-digit hour from 01 to 12
mm 2-digit minute from 00 to 59
ss 2-digit second from 00 to 59
MONTH name of month (full name in capital letters) MARCH JUNE
MTH name of month (3-character name in capital letters) MAR JUN
WEEK day of week (full name in capital letters) MONDAY SUNDAY
WEK day of week (3-character name in capital letters) MON SUN

The detailed information is described in SFXDate::Format.

Example 20.19. Date operations (subtraction)

SFXDate date1(2008, 10, 12, 22, 45, 58);
SFXDate date2(2008, 10, 12, 22, 55, 48);

// duration
SFXDateDuration duration = date2 - date1;

SInt32 n1 = duration.AsSInt32(); // n1 = 590

duration.Set(date1 - date2);

SInt32 n2 = duration.AsSInt32(); // n2 = -590