LoginSignup
3
3

More than 5 years have passed since last update.

C言語 timeの加算

Posted at

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

オンライン実行

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3