<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>to_time_t &#8211; カミュプリィの雑多なメモ</title>
	<atom:link href="https://www.commuply.co.jp/technic/memo/tag/to_time_t/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.commuply.co.jp/technic/memo</link>
	<description></description>
	<lastBuildDate>Sun, 22 Dec 2024 06:05:18 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://www.commuply.co.jp/technic/memo/wp-content/uploads/site-icon-150x150.png</url>
	<title>to_time_t &#8211; カミュプリィの雑多なメモ</title>
	<link>https://www.commuply.co.jp/technic/memo</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>[C++]今の日時を取得</title>
		<link>https://www.commuply.co.jp/technic/memo/get-now/</link>
		
		<dc:creator><![CDATA[ume]]></dc:creator>
		<pubDate>Fri, 25 Mar 2022 14:10:29 +0000</pubDate>
				<category><![CDATA[未分類]]></category>
		<category><![CDATA[2038年問題]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Char]]></category>
		<category><![CDATA[chrono]]></category>
		<category><![CDATA[GMT]]></category>
		<category><![CDATA[gmtime_s]]></category>
		<category><![CDATA[L]]></category>
		<category><![CDATA[localtime_s]]></category>
		<category><![CDATA[Now]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[nullptr]]></category>
		<category><![CDATA[sizeof]]></category>
		<category><![CDATA[strftime]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[time_point]]></category>
		<category><![CDATA[time_t]]></category>
		<category><![CDATA[to_time_t]]></category>
		<category><![CDATA[using namespace std]]></category>
		<category><![CDATA[UTC]]></category>
		<category><![CDATA[wchar_t]]></category>
		<category><![CDATA[wcsftime]]></category>
		<category><![CDATA[ローカルタイム]]></category>
		<category><![CDATA[今の日時]]></category>
		<category><![CDATA[協定世界時]]></category>
		<category><![CDATA[年/月/日]]></category>
		<category><![CDATA[日/月/年]]></category>
		<category><![CDATA[日付の並び順]]></category>
		<category><![CDATA[日時]]></category>
		<category><![CDATA[月/日/年]]></category>
		<category><![CDATA[現地時間]]></category>
		<category><![CDATA[経過秒]]></category>
		<guid isPermaLink="false">https://www.commuply.co.jp/technic/notesdomino/?p=167</guid>

					<description><![CDATA[はじめに 　今の日付と時刻を知りたいとおもったことはありませんか。いくつか方法を記載します。　ユーザー定義の変数名は日本語にしています。メリハリがあるとコードが読みやすいかなぁとおもったので。　サンプルでは『using  [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 id="%25e3%2581%25af%25e3%2581%2598%25e3%2582%2581%25e3%2581%25ab" class="wp-block-heading">はじめに</h2>



<p class="wp-block-paragraph">　今の日付と時刻を知りたいとおもったことはありませんか。いくつか方法を記載します。<br>　ユーザー定義の変数名は日本語にしています。メリハリがあるとコードが読みやすいかなぁとおもったので。<br>　サンプルでは『using namespace std;』を省略していますので、コードを試すときはこの行をおぎなってください。</p>



<h2 id="%25e7%25b5%258c%25e9%2581%258e%25e7%25a7%2592%25e3%2582%2592-timet-%25e5%25bd%25a2%25e5%25bc%258f%25e3%2581%25a7%25e5%258f%2596%25e5%25be%2597" class="wp-block-heading">『<strong>経過秒</strong>』を time_t 形式で取得</h2>



<p class="wp-block-paragraph">　とりあえず time_t 形式にします。これは『1970/01/01 00:00:00 UTC からの<strong>経過秒</strong>』です。<br>　32ビットシステムでは 2038/01/19 03:14:07 まで表現できるそうです。もうすぐトラブルが発生しそうな予感がしますね。64ビットシステムへの移行が必要です。</p>



<h3 id="now%25e3%2582%2592%25e4%25bd%25bf%25e3%2581%2586%25e6%2596%25b9%25e6%25b3%2595" class="wp-block-heading">now()を使う方法</h3>



<pre class="wp-block-preformatted">#include &lt;chrono&gt;
chrono::system_clock::time_point 日時 = chrono::system_clock::<strong>now()</strong>;
time_t <strong>経過秒</strong> = chrono::system_clock::to_time_t(日時);</pre>



<h3 id="timenullptr%25e3%2582%2592%25e4%25bd%25bf%25e3%2581%2586%25e6%2596%25b9%25e6%25b3%2595" class="wp-block-heading">time(nullptr)を使う方法</h3>



<pre class="wp-block-preformatted">#include &lt;chrono&gt;
time_t <strong>経過秒</strong> = time(nullptr);</pre>



<p class="wp-block-paragraph">　『nullptr』を使うことが推奨されているようですが、かわりに『NULL』や『0』を使うこともできます。</p>



<h2 id="%25e6%25a7%258b%25e9%2580%25a0%25e4%25bd%2593-tm-%25e5%25bd%25a2%25e5%25bc%258f%25e3%2581%25b8%25e5%25a4%2589%25e6%258f%259b" class="wp-block-heading">構造体 tm 形式へ変換</h2>



<p class="wp-block-paragraph">　先ほど求めた<strong>経過秒</strong>（time_t）を<strong>今の日時</strong> (struct tm) にします。<strong>今の日時</strong>といっても色々ありますが、協定世界時 (UTC,GMT) へ変換するものと、現地時間（ローカルタイム）へ変換するものを記載します。</p>



<h3 id="%25e5%258d%2594%25e5%25ae%259a%25e4%25b8%2596%25e7%2595%258c%25e6%2599%2582%25e3%2581%25b8" class="wp-block-heading">協定世界時へ</h3>



<pre class="wp-block-preformatted">struct tm <strong>今の日時</strong>;
gmtime_s(&amp;<strong>今の日時</strong>, &amp;<strong>経過秒</strong>);</pre>



<h3 id="%25e7%258f%25be%25e5%259c%25b0%25e6%2599%2582%25e9%2596%2593%25e6%2597%25a5%25e6%259c%25ac%25e6%2599%2582%25e9%2596%2593%25e3%2581%25b8" class="wp-block-heading">現地時間（日本時間）へ</h3>



<pre class="wp-block-preformatted">struct tm <strong>今の日時</strong>;
localtime_s(&amp;<strong>今の日時</strong>, &amp;<strong>経過秒</strong>);</pre>



<h2 id="%25e4%25bb%258a%25e3%2581%25ae%25e6%2597%25a5%25e6%2599%2582-struct-tm-%25e3%2582%2592%25e6%2596%2587%25e5%25ad%2597%25e3%2581%25ae%25e6%2597%25a5%25e6%2599%2582-char%25e3%2581%25b8" class="wp-block-heading"><strong>今の日時</strong> (struct tm) を文字<strong>の日時</strong> (char[])へ</h2>



<p class="wp-block-paragraph">　<strong>今の日時</strong>を文字として整形してみましょう。日付の文字列は国によって異なりますが、今回は日本の形式にします。<br>　・日本 : <strong>年</strong>/<span class="bold-red">月</span>/日<br>　・イギリス : 日/<strong><span class="bold-red">月</span></strong>/<strong>年</strong><br>　・アメリカ : <span class="bold-red">月</span>/日/<strong>年</strong><br>　国によって日付の並び順が異なりますが、何だか話す順番に通じるものがあるような気がします。</p>



<h3 id="char%25e5%259e%258b%25e3%2581%25ae%25e5%25b9%25b4%25e6%259c%2588%25e6%2597%25a5-%25e6%2599%2582%25e5%2588%2586%25e7%25a7%2592%25e3%2581%25b8" class="wp-block-heading">char型の『年/月/日 時:分:秒』へ</h3>



<pre class="wp-block-preformatted">char <strong>文字の日時</strong>[128];
strftime(<strong>文字の日時</strong>, 128, "%Y/%m/%d %H:%M:%S", &amp;<strong>今の日時</strong>);</pre>



<p class="wp-block-paragraph">　char[128] では sizeof(char[128]) が128になります。strftime(<strong>文字の日時</strong>, <span class="bold-red">sizeof(<strong>文字の日時</strong>)</span>, &#8230; としても大丈夫そうです。</p>



<h3 id="wchart%25e5%259e%258b%25e3%2581%25ae%25e5%25b9%25b4%25e6%259c%2588%25e6%2597%25a5-%25e6%2599%2582%25e5%2588%2586%25e7%25a7%2592%25e3%2581%25b8" class="wp-block-heading">wchar_t型の『年/月/日 時:分:秒』へ</h3>



<pre class="wp-block-preformatted">wchar_t <strong>文字の日時</strong>[128];
wcsftime(<strong>文字の日時</strong>, 128, L"%Y/%m/%d %H:%M:%S", &amp;<strong>今の日時</strong>);</pre>



<p class="wp-block-paragraph">　wchar_t[128] では sizeof(wchar_t[128]) が 256 になります。wcsftime(<strong>文字の日時</strong>, <span class="bold-red">sizeof(<strong>文字の日時</strong>)</span>, &#8230; とはできませんね。</p>



<h2 id="%25e3%2581%2595%25e3%2581%2584%25e3%2581%2594%25e3%2581%25ab" class="wp-block-heading">さいごに</h2>



<p class="wp-block-paragraph">　コード全体を書いてみましょう。</p>



<pre class="wp-block-preformatted">#include &lt;chrono&gt;
using namespace std;
time_t <strong>経過秒</strong> = time(nullptr);
struct tm <strong>今の日時</strong>;
localtime_s(&amp;<strong>今の日時</strong>, &amp;<strong>経過秒</strong>);
char <strong>文字の日時</strong>[128];
strftime(<strong>文字の日時</strong>, 128, "%Y/%m/%d %H:%M:%S", &amp;<strong>今の日時</strong>);
cout &lt;&lt; "今は " &lt;&lt; <strong>文字の日時</strong> &lt;&lt; endl;
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
