User Tools

Site Tools


programming:cpp:unixtime

UNIX 時間

UNIX 時間は 1970年1月1日午前0時0分0秒を 0 として経過した秒数を示したものである.
多くの PC システム,ソフトウェア上で用いられている.
なお,うるう秒は数えないらしい.

time_t 型を用いて扱うことが可能.

TestUnixtime.cpp
#include <iostream>
#include <ctime>
 
int main () {
 
  // int64_t unixtime_i = 1651736619; // 2022-05-05 16:43:39
  // time_t now = (time_t)unixtime_t;
 
  time_t now = std::time(nullptr); // 現在時刻
  tm *tm_event = localtime(&now); // 現地時間に直す
 
  int year = tm_event->tm_year + 1900; // tm_year は 1900 年始まり
  int month = tm_event->tm_mon + 1; // tm_mon は 0 始まり
  int day = tm_event->tm_mday;
 
  std::cout << year << "/" << month << "/" << day <<std::endl;
 
  std::exit(0);
 
}

tm は以下のように定義されている

struct tm {
    int  tm_sec;    /* seconds after the minute [0-60] */
    int  tm_min;    /* minutes after the hour [0-59] */
    int  tm_hour;   /* hours since midnight [0-23] */
    int  tm_mday;   /* day of the month [1-31] */
    int  tm_mon;    /* months since January [0-11] */
    int  tm_year;   /* years since 1900 */
    int  tm_wday;   /* days since Sunday [0-6] */
    int  tm_yday;   /* days since January 1 [0-365] */
    int  tm_isdst;  /* Daylight Savings Time flag */
    long tm_gmtoff; /* offset from CUT in seconds */
    char *tm_zone;  /* timezone abbreviation */
};

Unixtime 変換ツール

programming/cpp/unixtime.txt · Last modified: 2022/09/12 08:38 by odagawa