timeの加算
現在時刻から1秒足した時間を取得します。
# include <stdio.h>
# include <time.h> // add
int main(void){
time_t timer;
struct tm *s_tm;
// 現在時刻でtimerを取得
time(&timer);
// timerからtm構造体を取得
s_tm = localtime(&timer);
printf("分: %d\n",s_tm->tm_min);
printf("秒: %d\n",s_tm->tm_sec);
// timeの加算 : timerに1秒足す
timer += 1;
// timerからtm構造体を取得
s_tm = localtime(&timer);
printf("分: %d\n",s_tm->tm_min);
printf("秒: %d\n",s_tm->tm_sec);
}
わかってる人から見れば当たり前のコードですね。
しかし、意外とサンプルコードが見つからなくて不安になったので記事にしました。
参考
time http://www9.plala.or.jp/sgwr-t/lib/time.html
オンライン実行