それを行うための高速で低ゴミの方法はありますか?うるう秒やその他の日付/時刻の面白いビジネスを考慮していないため、単純なモジュラス演算を実行することはできません。
6 に答える
整数演算でうるう年を処理する方法を理解し、エポックから日付/時刻までの秒数からコンバーターを実装しました(ただし、59秒を超えることはありません)。以下のCコードは、Javaへの移植が非常に簡単なはずです。
#include <string.h>
#include <time.h>
typedef unsigned uint;
typedef unsigned long long uint64;
struct tm* SecondsSinceEpochToDateTime(struct tm* pTm, uint64 SecondsSinceEpoch)
{
uint64 sec;
uint quadricentennials, centennials, quadrennials, annuals/*1-ennial?*/;
uint year, leap;
uint yday, hour, min;
uint month, mday, wday;
static const uint daysSinceJan1st[2][13]=
{
{0,31,59,90,120,151,181,212,243,273,304,334,365}, // 365 days, non-leap
{0,31,60,91,121,152,182,213,244,274,305,335,366} // 366 days, leap
};
/*
400 years:
1st hundred, starting immediately after a leap year that's a multiple of 400:
n n n l \
n n n l } 24 times
... /
n n n l /
n n n n
2nd hundred:
n n n l \
n n n l } 24 times
... /
n n n l /
n n n n
3rd hundred:
n n n l \
n n n l } 24 times
... /
n n n l /
n n n n
4th hundred:
n n n l \
n n n l } 24 times
... /
n n n l /
n n n L <- 97'th leap year every 400 years
*/
// Re-bias from 1970 to 1601:
// 1970 - 1601 = 369 = 3*100 + 17*4 + 1 years (incl. 89 leap days) =
// (3*100*(365+24/100) + 17*4*(365+1/4) + 1*365)*24*3600 seconds
sec = SecondsSinceEpoch + 11644473600LL;
wday = (uint)((sec / 86400 + 1) % 7); // day of week
// Remove multiples of 400 years (incl. 97 leap days)
quadricentennials = (uint)(sec / 12622780800ULL); // 400*365.2425*24*3600
sec %= 12622780800ULL;
// Remove multiples of 100 years (incl. 24 leap days), can't be more than 3
// (because multiples of 4*100=400 years (incl. leap days) have been removed)
centennials = (uint)(sec / 3155673600ULL); // 100*(365+24/100)*24*3600
if (centennials > 3)
{
centennials = 3;
}
sec -= centennials * 3155673600ULL;
// Remove multiples of 4 years (incl. 1 leap day), can't be more than 24
// (because multiples of 25*4=100 years (incl. leap days) have been removed)
quadrennials = (uint)(sec / 126230400); // 4*(365+1/4)*24*3600
if (quadrennials > 24)
{
quadrennials = 24;
}
sec -= quadrennials * 126230400ULL;
// Remove multiples of years (incl. 0 leap days), can't be more than 3
// (because multiples of 4 years (incl. leap days) have been removed)
annuals = (uint)(sec / 31536000); // 365*24*3600
if (annuals > 3)
{
annuals = 3;
}
sec -= annuals * 31536000ULL;
// Calculate the year and find out if it's leap
year = 1601 + quadricentennials * 400 + centennials * 100 + quadrennials * 4 + annuals;
leap = !(year % 4) && (year % 100 || !(year % 400));
// Calculate the day of the year and the time
yday = sec / 86400;
sec %= 86400;
hour = sec / 3600;
sec %= 3600;
min = sec / 60;
sec %= 60;
// Calculate the month
for (mday = month = 1; month < 13; month++)
{
if (yday < daysSinceJan1st[leap][month])
{
mday += yday - daysSinceJan1st[leap][month - 1];
break;
}
}
// Fill in C's "struct tm"
memset(pTm, 0, sizeof(*pTm));
pTm->tm_sec = sec; // [0,59]
pTm->tm_min = min; // [0,59]
pTm->tm_hour = hour; // [0,23]
pTm->tm_mday = mday; // [1,31] (day of month)
pTm->tm_mon = month - 1; // [0,11] (month)
pTm->tm_year = year - 1900; // 70+ (year since 1900)
pTm->tm_wday = wday; // [0,6] (day since Sunday AKA day of week)
pTm->tm_yday = yday; // [0,365] (day since January 1st AKA day of year)
pTm->tm_isdst = -1; // daylight saving time flag
return pTm;
}
うるう秒やその他の日付/時刻の面白いビジネスを考慮していないため、単純なモジュラス演算を実行することはできません。
Javaは、一般的にうるう秒を考慮していません。つまり、公式にはプラットフォーム次第ですが、一般的な本番プラットフォームのいずれにも実装されているとは思いません。うるう秒を考慮する必要がありますか?そうした場合、データソースが何であり、何を反映させたいかに応じて、追加または削除する秒数の単純なテーブルベースのルックアップを実行できるはずです。
「その他の日時おかしな商売」については、この特定の計算でおかしな商売はないと思います。たとえば、タイムゾーンは、エポックからの経過時間には関係ありません。
「エポック」とは、01-01-1970、00:00:00GMTを意味します。
long secondsSinceEpoch = ...;
// The constructor of Date expects milliseconds
// since 01-01-1970, 00:00:00 GMT
Date date = new Date(secondsSinceEpoch * 1000L);
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(df.format(date));
Calendar = Calendar.getInstance();
calendar.setTimeInMillis(secondsSinceTheEpoch*1000);
これは、高速でゴミゼロのソリューションです。Calendar
非常に重いオブジェクトであり、初期化に448バイトのヒープとほぼマイクロ秒を要するため(Java 6、64ビットHotSpot、OS X)、呼び出しごとにの新しいインスタンスを作成しないことが非常に重要です。
HmsCalculator
単一のスレッドからの使用を目的としています(各スレッドは異なるインスタンスを使用する必要があります)。
public class HmsCalculator
{
private final Calendar c = Calendar.getInstance();
public Hms toHms(long t) { return toHms(t, new Hms()); }
public Hms toHms(long t, Hms hms) {
c.setTimeInMillis(t*1000);
return hms.init(c);
}
public static class Hms {
public int h, m, s;
private Hms init(Calendar c) {
h = c.get(HOUR_OF_DAY); m = c.get(MINUTE); s = c.get(SECOND);
return this;
}
public String toString() { return String.format("%02d:%02d:%02d",h,m,s); }
}
public static void main(String[] args) {
System.out.println(new HmsCalculator().toHms(
System.currentTimeMillis()/1000));
}
}
PS私はそれらすべての静的インポートを貼り付けませんでした(退屈です)。
java.time
Java 8以降では、使用する組み込みクラスInstant
はjava.timeフレームワークのものです。
Instant instant = Instant.ofEpochSecond ( 1_469_168_058L );
コンソールにダンプします。
System.out.println ( "instant: " + instant );
インスタント:2016-07-22T06:14:18Z
パフォーマンス
実行速度やごみの発生に関して、どのように機能するのか正確にはわかりません。java.time.Instant
ただし、このクラスに対してテストする必要があります。Java 9のソースコードをよく読んでみると、非常にシンプルで高速に見えます。
いくつかのメソッドジャンプを使用すると、基本的に整数のペアを割り当てるだけです。(a)エポックからの秒数(64ビットlong
)および(b)ナノ秒のカウントを秒の小数部(32ビットint
)として割り当てます。いくつかの簡単なチェックを行った後:
最初にゼロの値を探します。この場合、エポック自体の静的インスタンスを返します。
if ((seconds | nanoOfSecond) == 0) {
return EPOCH;
}
次に、最小/最大定数のペアに対して秒数の健全性チェックを実行します。
if (seconds < MIN_SECOND || seconds > MAX_SECOND) {
throw new DateTimeException("Instant exceeds minimum or maximum instant");
}
次に、コンストラクターを呼び出します。コンストラクターは、整数値のペアをメンバー変数のペア(long
およびint
プリミティブ)に割り当てます。
this.seconds = epochSecond;
this.nanos = nanos;
もちろん、それは単なる建設です。時刻などの部分を調べることは、より多くの作業を意味します。toString
別のクラスを含むメソッドを介して文字列を生成する場合と同様に、 DateTimeFormatter
。toString
ソースコードは1行です。
return DateTimeFormatter.ISO_INSTANT.format(this);
また、UTC以外のタイムゾーンで年、月、日、時間などの部分が必要な場合は、より多くの作業ZoneId
とZonedDateTime
クラスが必要になることを覚えておいてください。例えば:
ZoneId zoneId = ZoneId.of( "America/Montreal" );
ZonedDateTime zdt = instant.atZone( zoneId );