Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
问题:使用time()计算一天中的时间_Time_Solaris - Fatal编程技术网

问题:使用time()计算一天中的时间

问题:使用time()计算一天中的时间,time,solaris,Time,Solaris,我正在编写一些代码,以小时:分钟的格式为我提供在shell中编写的最后命令的历史记录,包括时间 我有以下代码: // 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",

我正在编写一些代码,以小时:分钟的格式为我提供在shell中编写的最后命令的历史记录,包括时间

我有以下代码:

// 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]);
我将时间存储在以下位置:

time_t history_time[MAX_HISTORY_SIZE];

// Store the time
time_t t = time(NULL);
history_time[history_index] = t;
1 18:29 ls
2 18:29 cd
3 18:29 ls -al | grep test
时间碰巧错了,但我不知道为什么。我正在使用Solaris

输出如下:

time_t history_time[MAX_HISTORY_SIZE];

// Store the time
time_t t = time(NULL);
history_time[history_index] = t;
1 18:29 ls
2 18:29 cd
3 18:29 ls -al | grep test
分钟是正确的,但不是小时。 在命令行中键入“date”时,我会得到:“Thu Sep 16 14:29:55 EDT 2010”,因此分钟数相同,但小时数不同

我不确定这是与我的代码有关,还是与Solaris中的time()函数有关?也许date不使用time()函数,我不确定

有人有解决办法吗

多谢各位


Jary

time()返回UTC时间,您必须将其偏移量考虑在内。或者,在使用localtime()将UTC转换为本地时间后,只需使用时间格式化函数asctime即可。

您是否意识到time()会产生“日历”时间或自纪元(1970年1月1日00:00:00)起的UTC秒数?这意味着您正在查看的小时将从您的区域设置偏移一个可预测的量(您从UTC meridian的偏移量)。这不是问题吗?非常感谢,我完全忘记了UTC时间(和GMT),现在有意义了。谢谢!localtime()修复了我的问题!谢谢!