UNIX 時間( UNIX エポック[=1970年1月1日0時0分0秒]からの経過秒数)のミリ秒版
( = UNIX エポックを起点としたシステム時間(システム時刻)をミリ秒で表現した値)
を取得する方法です。
例えば Java では System.currentTimeMillis() といったメソッドでさくっと取得できるのですが、Objective-C ではいくつか注意点があります:
- 時刻取得関数 CFAbsoluteTimeGetCurrent() は2001年1月1日を起点としています
- CFAbsoluteTimeGetCurrent() は整数部分を秒とした小数値( CFAbsoluteTime 型 = double 型)を返します
- 1970年1月1日から2001年1月1日までの経過時間の値が定数 kCFAbsoluteTimeIntervalSince1970 として用意されています
ということで、以下のようなコードで、エポックからのミリ秒を取得することができます。ついでにUNIX時間の取得コードも記載しておきます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (UInt64)getEpochSeconds | |
{ | |
return (UInt64)floor(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970); | |
} | |
+ (UInt64)getEpochMilliSeconds | |
{ | |
return (UInt64)floor((CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) * 1000.0); | |
} |
※参考情報:
システム時刻 - Wikipedia
(システム時間を得るための各種OSのシステムコール関数、プログラミング言語のAPIが記載されています)
0 件のコメント:
コメントを投稿