概要
gmtime_s, gmtime_r, _mkgmtime, timegm, localtime_s, localtime_r, _mklocaltime, timelocal をラップしています.Windows と Linux の違いに依らずコンパイル可能です.また,間違いのない命名ルールを採用しています.
| プロトタイプ宣言 | 動作説明 |
|---|---|
| struct tm time2tm(time_t unixtime); | UNIX 時間を tm 構造体へ変換します. |
| time_t tm2time(struct tm& rhs); | tm 構造体を UNIX 時間へ変換します. |
| struct tm timeGm2tmLocal(time_t unixtime); | GMT を示す UNIX 時間を,ローカル時刻を示す tm 構造体へ変換します. |
| time_t tmLocal2timeGm(struct tm& rhs); | ローカル時刻を示す tm 構造体を,GMT を示す UNIX 時間へ変換します. |
| std::string time2str(time_t unixtime); | UNIX 時間を文字列表記に変換します. |
| std::string tm2str(struct tm& rhs); | tm 構造体を文字列表記に変換します. |
| double time2unixday(time_t unixtime); | UNIX 時間を 1/(24*60*60) 倍の値 (俗称: UNIX day) へ変換します. |
| double tm2unixday(struct tm& rhs); | tm 構造体を UNIX 時間の 1/(24*60*60) 倍の値 (俗称: UNIX day) へ変 換します. |
| time_t yday2time(const uint year, const uint yday); | 西暦 + 通日を UNIX 時間へ変換します. |
| struct tm yday2tm(const uint year, const uint yday); | 西暦 + 通日を tm 構造体へ変換します. |
使用例
#include <sstd/sstd.hpp> int main(){ time_t c_time = time(0); // get current time struct tm c_tm = sstd::time2tm(c_time); time_t c_time_2 = sstd::tm2time(c_tm); double unixday = sstd::time2unixday(c_time); double unixday_2 = sstd::tm2unixday(c_tm); printf("GMT: %s\n", sstd::tm2str(c_tm).c_str()); printf("GMT: %s\n", sstd::time2str(c_time_2).c_str()); printf("unixday: %lf\n", unixday); printf("unixday: %lf\n", unixday_2); printf("\n"); struct tm c_tm_l = sstd::timeGm2tmLocal(c_time); time_t c_time_3 = sstd::tmLocal2timeGm(c_tm_l); printf("local: %s\n", sstd::tm2str(c_tm_l).c_str()); printf("GMT: %s\n", sstd::time2str(c_time_3).c_str()); printf("\n"); time_t c_time_yday = sstd::yday2time(c_tm.tm_year+1900, c_tm.tm_yday+1); printf("GMT: %s\n", sstd::time2str(c_time_yday).c_str()); struct tm c_tm_yday = sstd::yday2tm(c_tm.tm_year+1900, c_tm.tm_yday+1); printf("GMT: %s\n", sstd::tm2str(c_tm_yday).c_str()); printf("\n"); }
実行結果
$ ./exe.exe GMT: 2017-10-22 15:20:25 GMT: 2017-10-22 15:20:25 unixday: 17461.639178 unixday: 17461.639178 local: 2017-10-23 00:20:25 GMT: 2017-10-22 15:20:25 GMT: 2017-10-22 00:00:00 GMT: 2017-10-22 00:00:00
実装