Wednesday, 15 June 2011

calculating time in C - AIX machine -



calculating time in C - AIX machine -

i editing time value using variable of type struct tm (adding seconds tm->tm_sec), getting wrong results after doing mktime(&t).

doing in linux gets me proper results, in aix not. problem?

#include <stdio.h> #include <time.h> #include <langinfo.h> #include <locale.h> int main () { struct tm tm; struct tm *end; time_t t; char str[20] = {'\0'}; //if (strptime("7 feb 2013 01:47:30", "%d %b %y %h:%m:%s", &tm) == null) if (strptime("2012-10-17-01-07-30", "%y-%m-%d-%h-%m-%s", &tm) == null) {printf("error\n"); } tm.tm_sec = (tm.tm_sec + 1200); //tm.tm_sec = 12; //t = mktime(&tm); //t = t + 12; //end =localtime(&t); strftime(str,20,"%y %m %d %h %m %s",&tm); printf("str %s\n",str); homecoming 0; }

i believe right reply utilize time_t, big number representing time in seconds since midnight of 1 jan 1970. adding arbitrary number of seconds here becomes trivial.

i expect if adding seconds tm->tm_sec, overflows, , causes result incorrect. if unlucky, need ripple alter in seconds way through year (adding 5 seconds 31 dec 2013 23:59:56 take 01 jan 2014 00:00:01). of course of study can done, instead of:

t =+ 5;

you dozen steps along line of

tm.tm_sec += 5; if (tm.tm_sec >= 60) { tm.tm_sec -= 60; tm.tm_min += 1; if (tm.tm_min >= 60) { ... , on ... } }

it gets more interesting if overflow days in month, since have take business relationship of number of days in each month, 28, 29, 30 or 31 depending on month [and if it's leap-year or not].

c aix

No comments:

Post a Comment