tags:

views:

15

answers:

1

Hello,

I am writing some code to give me a history of the last commands written in a shell, including the time, in the format hour:minutes.

I have the following code:

// Modulo by 86400, number of seconds in a day
int totalSeconds = history_time[i] % 86400;
int hours = totalSeconds / 3600;
int minutes = (totalSeconds % 3600) / 60;    

printf("%d %d:%d %s\n", count+1, hours, minutes, history[i]);

I store the time as in the following:

time_t history_time[MAX_HISTORY_SIZE];

// Store the time
time_t t = time(NULL);
history_time[history_index] = t;

The hour happens to be wrong, but I am not sure why. I am using Solaris.

The output is the following:

1 18:29 ls
2 18:29 cd
3 18:29 ls -al | grep test

The minutes are correct, but not the hour. When typing "date" in the command line I get: "Thu Sep 16 14:29:55 EDT 2010", so same minutes but different hour.

I am not sure if it has to do with my code, or with the time() function in Solaris? Maybe date do not use the time() function, I am not sure.

Does anyone please have a solution?

Thank you very much,

Jary

A: 

time() returns UTC time, you have to take your offset from it into account. Or just use a time formatting function asctime after converting UTC to local with localtime().

MK
Thank you very much, I completely forgot about UTC time (and GMT), it makes sense now. Thanks a lot!
Jary
localtime() fixed my problem! Thanks a lot!
Jary