I try to add a time offset to a date generated with time C function. Calculation is wrong depending the offset value. if I increase the offset value the calculation became false!
I am using gcc on a CentOS 5.11
#include <stdio.h>
#include <time.h>
#define MAX_SIZE 80
int main( int argc, char * argv[] ) {
time_t timestamp, offset;
struct tm *pTime;
char buffer[ MAX_SIZE ];
//timestamp = time( NULL );
timestamp = 1470356033L;
printf("timestamp = %ld\n", timestamp);
// offset calculation
offset = atol(argv[1]) * (24L * 60L * 60L);
printf("offset = %ld\n", offset);
timestamp += offset;
printf("timestamp = %ld\n", timestamp);
pTime = localtime( & timestamp );
strftime( buffer, MAX_SIZE, "%d/%m/%Y %H:%M:%S", pTime );
printf( "Date and french time : %s\n", buffer );
return 0;
}
./testDate 0
timestamp = 1470356033
offset = 0
timestamp = 1470356033
Date and french time : 05/08/2016 02:13:53
This Result is OK, it is reference date without offset
./testDate 4
timestamp = 1470356033
offset = 345600
timestamp = 1470701633
Date and french time : 09/08/2016 02:13:53
This Result is also OK, it is reference date with 4 days offset
./testDate 90
timestamp = 1470356033
offset = 7776000
timestamp = 1478132033
Date and french time : 03/11/2016 01:13:53
This Result is wrong, it is reference date with 90 days offset.
Date is OK but 1 hour is missing, it should be 02:13:53 but actual output is 01:13:53