C examples for working with date and time: Reference:

C examples for working with date and time: Reference: http://www.cplusplus.com/reference/clibrary/ctime/ #include int main (int argc, char * const ar...
Author: Sheena Bruce
5 downloads 2 Views 20KB Size
C examples for working with date and time: Reference: http://www.cplusplus.com/reference/clibrary/ctime/ #include int main (int argc, char * const argv[]) { time_t rawtime; // structure for storing the calendar date and time // http://www.cplusplus.com/reference/clibrary/ctime/tm/ struct tm *timeinfo; time(&rawtime); // get current system time and assign to rawtime timeinfo = localtime(&rawtime); // convert time_t to tm printf("Current local date and time is %s\n", asctime(timeinfo)); // to extract the individual fields in the tm structure printf("seconds = %i\n", timeinfo->tm_sec); printf("minutes = %i\n", timeinfo->tm_min); printf("hours = %i\n", timeinfo->tm_hour); printf("day of the month = %i\n", timeinfo->tm_mday); printf("months since January = %i\n", timeinfo->tm_mon); printf("years since 1900 = %i\n", timeinfo->tm_year); printf("days since Sunday = %i\n", timeinfo->tm_wday); printf("days since January 1 = %i\n", timeinfo->tm_yday); printf("daylight saving time flag = %i\n", timeinfo->isdst);

// // // // // // // // //

seconds 0-59 minutes 0-59 hours 0-23 day of the month 1-31 months since January 0-11 years since 1900 days since Sunday 0-6 days since January 1 0-365 daylight saving time

// find day of the week for a given date timeinfo->tm_year = 2011 – 1900; // set the year to 2011 timeinfo->tm_mon = 9 – 1; // set the month to September timeinfo->tm_mday = 3; // set the day to 3 mktime(timeinfo); // call mktime to set timeinfo->tm_wday char *weekday[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; printf("The day of the week is %s\n", weekday[timeinfo->tm_wday]);

// add n days to a given date time(&rawtime); timeinfo = localtime(&rawtime); // init all the fields in the tm structure timeinfo->tm_year = 2012-1900; // year 2012 (1900 base) timeinfo->tm_mon = 2 - 1; // month Feb (0 base) timeinfo->tm_mday = 26; // day 26 (1 base) timeinfo->tm_hour = 0; // (0 base) timeinfo->tm_min = 0; // (0 base) timeinfo->tm_sec = 0; // (0 base) timeinfo->tm_isdst = -1; // set isdst to -1 and mktime will automatically adjust isdst to // either 0 = NONE DST (Winter) or 1 = DST (Summer) // isdst changes at 25 hours 59 minutes 59 seconds (i.e. 3 am) rawtime = mktime(timeinfo); // convert from tm to number of seconds since 00:00 hour Jan 1, 1970 UTC rawtime = rawtime + 4*86400; // adding 4 days of seconds timeinfo = localtime(&rawtime); // converting back to tm printf("2y=%i, m=%i, d=%i\n",timeinfo->tm_year,timeinfo->tm_mon,timeinfo->tm_mday); return 0; }

Objective-C examples for working with date and time: CFAbsoluteTime CFDateRef CFDateRef CFGregorianDate CFGregorianUnits Boolean CFComparisonResult

absTime; aCFDate1; aCFDate2; gregDate; gregUnit; status; result;

// get current system date and time absTime = CFAbsoluteTimeGetCurrent(); // convert absolute date to Gregorian date gregDate = CFAbsoluteTimeGetGregorianDate (absTime,CFTimeZoneCopySystem()); printf("year = %i ",gregDate.year); printf("month = %i ",gregDate.month); printf("day = %i ",gregDate.day); printf("hour = %i ",gregDate.hour); printf("minute = %i ",gregDate.minute); printf("second = %i\n",gregDate.second); // add 1 day and 4 hours to the current time gregUnit.years=0; gregUnit.months=0; gregUnit.days=1; gregUnit.hours=4; gregUnit.minutes=0; gregUnit.seconds=0; absTime = CFAbsoluteTimeAddGregorianUnits (CFAbsoluteTimeGetCurrent(),CFTimeZoneCopySystem(),gregUnit); // convert absolute date to Gregorian date gregDate = CFAbsoluteTimeGetGregorianDate (absTime,CFTimeZoneCopySystem()); // absolute time, time zone printf("year = %i ",gregDate.year); printf("month = %i ",gregDate.month); printf("day = %i ",gregDate.day); printf("hour = %i ",gregDate.hour); printf("minute = %i ",gregDate.minute); printf("second = %i\n",gregDate.second);

// Construct a Gregorian date. gregDate.year = 2010; gregDate.month = 3; gregDate.day = 12; gregDate.hour = 6; gregDate.minute = 33; gregDate.second = 22.7; // Check the validity of the date. status = CFGregorianDateIsValid(gregDate, kCFGregorianAllUnits); printf("Is my Gregorian date valid? %d\n", status); // Convert the Gregorian date to absolute time. absTime = CFGregorianDateGetAbsoluteTime(gregDate, CFTimeZoneCopySystem()); printf("The Absolute Time from a Gregorian date is: %d\n", absTime); // get day of the week int i = CFAbsoluteTimeGetDayOfWeek(CFAbsoluteTimeGetCurrent(), CFTimeZoneCopySystem()); // sun=7,mon=1,...sat=6 printf("day of week %i\n",i); // Create two CFDates from absolute time. aCFDate2 = CFDateCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent()); aCFDate1 = CFDateCreate(kCFAllocatorDefault, absTime); // Compare these two dates result = CFDateCompare(aCFDate1, aCFDate2, NULL); switch (result) { case kCFCompareLessThan: printf("date1 is before date2!\n"); break; case kCFCompareEqualTo: printf("date1 is the same as date2!\n"); break; case kCFCompareGreaterThan: printf("date1 is after date2!\n"); break; }

// Computes the time difference between two specified absolute times and returns the result as an interval in Gregorian units. printf("time difference:\n"); gregUnit = CFAbsoluteTimeGetDifferenceAsGregorianUnits(CFAbsoluteTimeGetCurrent(),absTime,CFTimeZoneCopySystem(),kCFGregori anUnitsDays); printf("days = %i hours = %i minutes = %i\n",gregUnit.days,gregUnit.hours,gregUnit.minutes); gregUnit = CFAbsoluteTimeGetDifferenceAsGregorianUnits(CFAbsoluteTimeGetCurrent(),absTime,CFTimeZoneCopySystem(),kCFGregori anUnitsHours); printf("days = %i hours = %i minutes = %i\n",gregUnit.days,gregUnit.hours,gregUnit.minutes); gregUnit = CFAbsoluteTimeGetDifferenceAsGregorianUnits(CFAbsoluteTimeGetCurrent(),absTime,CFTimeZoneCopySystem(),kCFGregori anUnitsMinutes); printf("days = %i hours = %i minutes = %i\n",gregUnit.days,gregUnit.hours,gregUnit.minutes); gregUnit = CFAbsoluteTimeGetDifferenceAsGregorianUnits(CFAbsoluteTimeGetCurrent(),absTime, CFTimeZoneCopySystem(), (kCFGregorianUnitsDays | kCFGregorianUnitsHours | kCFGregorianUnitsMinutes | kCFGregorianUnitsSeconds)); CFStringRef output = CFStringCreateWithFormat(NULL, 0, CFSTR("%d days %d hours %d minutes %d seconds\n"), gregUnit.days, gregUnit.hours, gregUnit.minutes, gregUnit.seconds); CFShow(output);