エポックからの秒数を表す time_t 値から、時間、分、秒を整数値として抽出したいと考えています。
時間の値が正しくありません。なんで?
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
char buf[64];
while (1) {
time_t t = time(NULL);
struct tm *tmp = gmtime(&t);
int h = (t / 360) % 24; /* ### My problem. */
int m = (t / 60) % 60;
int s = t % 60;
printf("%02d:%02d:%02d\n", h, m, s);
/* For reference, extracts the correct values. */
strftime(buf, sizeof(buf), "%H:%M:%S\n", tmp);
puts(buf);
sleep(1);
}
}
出力 (時間は 10 である必要があります)
06:15:35
10:15:35
06:15:36
10:15:36
06:15:37
10:15:37