tools
読み取り中…
検索中…
一致する文字列を見つけられません
time.h
[詳解]
1#pragma once
2#include <string>
3#include <ctime>
4#include <sstream>
5#include <iomanip>
6#include <array>
7#include <chrono>
8#include <thread>
9
10/**
11 * @file time.h
12 * @brief 時刻・時間ユーティリティ
13 *
14 * 時間単位の列挙型 (tu)、スリープ関数、現在時刻の取得・フォーマット、
15 * Unix タイムスタンプ取得、秒数を時分秒に分割する関数を提供します。
16 */
17
18/**
19 * @enum tu
20 * @brief 時間単位を表す列挙型
21 *
22 * sleepc() や getCTime() などの時間関数に渡す単位を指定します。
23 */
24enum class tu {
25 n, ///< ナノ秒
26 c, ///< マイクロ秒
27 l, ///< ミリ秒
28 s, ///< 秒
29 m, ///< 分
30 h, ///< 時間
31 d, ///< 日
32 o, ///< 月
33 y ///< 年
34};
35
36/**
37 * @brief 指定した時間単位でスレッドをスリープさせる
38 * @param unit 時間単位 (tu::n / tu::c / tu::l / tu::s / tu::m / tu::h)
39 * @param value スリープする時間の値
40 * @throws std::invalid_argument サポートされていない単位 (d / o / y) が渡された場合
41 */
42inline void sleepc(tu unit, double value) {
43 switch (unit) {
44 case tu::n: std::this_thread::sleep_for(std::chrono::duration<double, std::nano>(value)); break;
45 case tu::c: std::this_thread::sleep_for(std::chrono::duration<double, std::micro>(value)); break;
46 case tu::l: std::this_thread::sleep_for(std::chrono::duration<double, std::milli>(value)); break;
47 case tu::s: std::this_thread::sleep_for(std::chrono::duration<double>(value)); break;
48 case tu::m: std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<60>>(value)); break;
49 case tu::h: std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<3600>>(value)); break;
50 default: throw std::invalid_argument("Invalid time unit");
51 }
52}
53
54/**
55 * @brief 現在のローカル時刻を指定フォーマットの文字列で返す
56 * @param format strftime 書式文字列(例: "%Y-%m-%d %H:%M:%S")
57 * @return フォーマットされた現在時刻文字列
58 */
59inline std::string getCTime(const std::string format) {
60 const std::time_t t = std::time(nullptr);
61 std::ostringstream oss;
62 oss << std::put_time(std::localtime(&t), format.c_str());
63 return oss.str();
64}
65
66/**
67 * @brief 現在の Unix タイムスタンプ(秒)を返す
68 * @return エポック(1970-01-01 00:00:00 UTC)からの経過秒数
69 */
70inline int64_t getUnixTime() {
71 return static_cast<int64_t>(std::time(nullptr));
72}
73
74/**
75 * @brief 指定した時間単位の現在値をローカル時刻から返す
76 * @param unit 取得する時間単位(tu::s / tu::m / tu::h / tu::d / tu::o / tu::y)
77 * @return 対応する時刻フィールドの整数値(年は西暦、月は 1〜12、日は 1〜31)
78 * @throws std::invalid_argument サポートされていない単位が渡された場合
79 */
80inline int getCTime(const tu unit) {
81 auto current = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
82 std::tm current_tm = *std::localtime(&current);
83 switch (unit) {
84 case tu::s: return current_tm.tm_sec;
85 case tu::m: return current_tm.tm_min;
86 case tu::h: return current_tm.tm_hour;
87 case tu::d: return current_tm.tm_mday;
88 case tu::o: return current_tm.tm_mon+1;
89 case tu::y: return current_tm.tm_year+1900;
90 default: throw std::invalid_argument("Invalid time unit");
91 }
92}
93
94/**
95 * @brief 秒数を 時間・分・秒 の配列に分割する
96 * @param total_seconds 分割する総秒数
97 * @return {時間, 分, 秒} の順に格納した 3 要素の配列
98 */
99inline std::array<int,3> splitTime(int total_seconds) {
100 int hours = total_seconds / 3600;
101 int minutes = (total_seconds % 3600) / 60;
102 int seconds = total_seconds % 60;
103 return {hours, minutes, seconds};
104}
std::array< int, 3 > splitTime(int total_seconds)
秒数を 時間・分・秒 の配列に分割する
Definition time.h:99
int64_t getUnixTime()
現在の Unix タイムスタンプ(秒)を返す
Definition time.h:70
std::string getCTime(const std::string format)
現在のローカル時刻を指定フォーマットの文字列で返す
Definition time.h:59
void sleepc(tu unit, double value)
指定した時間単位でスレッドをスリープさせる
Definition time.h:42
tu
時間単位を表す列挙型
Definition time.h:24
@ s
Definition time.h:28
@ h
時間
Definition time.h:30
@ l
ミリ秒
Definition time.h:27
@ y
Definition time.h:33
@ c
マイクロ秒
Definition time.h:26
@ m
Definition time.h:29
@ n
ナノ秒
Definition time.h:25
@ d
Definition time.h:31
@ o
Definition time.h:32