#navi(contents-page-name): No such page: 日時

世界協定時(UTC)を取得する・gmtime

gmtime関数を用いて世界協定時間(UTC)の時刻を取得してみます。
同時にローカル時刻も取得し表示します。

c-top.html is not found or not readable.

関連記事

gmtimeの書式

gmtimeを使用したC言語サンプルコード

以下にgmtimeを使用したCサンプルコードと実行例を記します。

&ref(): File not found: "gmtime.c" at page "日時/世界協定時(UTC)を取得する・gmtime"; (改行コードLF)

#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t now;
    struct tm *ptm;
    char buf[128];

    now = time(NULL);
    ptm = localtime(&now);
    strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S %Z", ptm);
    printf("localtime : %s\n", buf);

    ptm = gmtime(&now);
    strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S %Z", ptm);
    printf("gmtime    : %s\n", buf);

    return 0;
}

コンパイルし実行した結果を以下に記します。

$ gcc gmtime.c -o gmtime
$ ./gmtime 
localtime : 2014/07/26 23:51:47 JST
gmtime    : 2014/07/26 14:51:47 GMT

以上、gmtime関数を使用したCサンプルコードでした。

c-btm.html is not found or not readable.

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS