Dart: Structured web apps [7] Date

DartのDateクラスを使うと現在時刻などを知ることができます。

APIリファレンスに明言されていませんが、DartのDateクラスは、「1970/01/01 00:00:00.000」を基準とした実装となっているようです。現在時刻のインスタンスを取得するためのnow()コンストラクタが提供されているので、(1), (2)で、まずはそれを使ってみています。これだけで「time :2012-04-21 06:59:47.095」のように表示できます。current.valueは、「1970/01/01 00:00:00.000」から経過したミリ秒の値となるので「msecs:1334959187095」のように表示されます。

年月日などを個別に取得することもできます。コメントで、各値のとる範囲を書いてあります。weekdayはDateに定数が定義されているので、それを使って文字列へ変更しています。月についてもDateに定数が定義されているので、それらを使って文字列へ変更するようにした方が良いのですが、ちょっと手を抜いています。

class DateSample {
  void run() {
    Date current = new Date.now();
    print("time :${current}"); // (1)
    print("msecs:${current.value}"); // (2)
    
    int y = current.year;
    int m = current.month; // 1..12
    int d = current.day; // 1..31
    int h = current.hours; // 0..23
    int min = current.minutes; // 0..59
    int s = current.seconds; // 0..59
    int ms = current.milliseconds; // 0..999
    int w = current.weekday;
    String weekday = "";
    switch(w) {
      case Date.MON: weekday = "MON"; break;
      case Date.TUE: weekday = "TUE"; break;
      case Date.WED: weekday = "WED"; break;
      case Date.THU: weekday = "THU"; break;
      case Date.FRI: weekday = "FRI"; break;
      case Date.SUN: weekday = "SUN"; break;
      case Date.SAT: weekday = "SAT"; break;
    }
    print("${y}/${m}/${d} ${h}:${min}:${s}.${ms} (${weekday})");
    
    Date date = new Date.withTimeZone(1970, 1, 1, 0, 0, 0, 0, new TimeZone.utc());
    print("date(UTC)          :${date}");
    print("date.value(UTC)    :${date.value}"); // 1970/01/01 00:00:00.000が基準
    print("current-date       :${current.difference(date)}");
    print("current-date(day)  :${current.difference(date).inDays}");
    print("current-date(hours):${current.difference(date).inHours}");
    print("current-date(mins) :${current.difference(date).inMinutes}");
    print("current-date(secs) :${current.difference(date).inSeconds}");
    print("current-date(msecs):${current.difference(date).inMilliseconds}");
    date = new Date.fromEpoch(0, new TimeZone.local());
    print("date(Local)        :${date}");
    print("date.value(Local)  :${date.value}");
  }
}
void main() {
  DateSample app = new DateSample();
  app.run();
}

Dateのインスタンスを取得するためには、コンストラクタwithTimeZone()や、コンストラクタfromEpoch()を使う方法があります。withTimeZone()は、時刻とタイムゾーンを指定しますし、fromEpoch()は1970/01/01 00:00:00.000からの経過ミリ秒数とタイムゾーンを指定します。サンプルでは1970/01/01 00:00:00.000に相当するDateのインスタンスを生成していますが、withTimeZone()ではUTCを、fromEpoch()ではローカルのタイムゾーンを指定しているという違いがあります。このため、date.valueの表示でUTCのものは「1970-01-01 00:00:00.000Z」となりますが、ローカルのものは「1970-01-01 09:00:00.000」となります。inMilliseconds()のようなメソッドを使うと、1970/01/01 00:00:00.000からの経過時間を知ることができます。

実行結果は次のようになります。

time :2012-04-21 06:59:47.095
msecs:1334959187095
2012/4/21 6:59:47.95 (SAT)
date(UTC)          :1970-01-01 00:00:00.000Z
date.value(UTC)    :0
current-date       :370821:59:47.095
current-date(day)  :15450
current-date(hours):370821
current-date(mins) :22249319
current-date(secs) :1334959187
current-date(msecs):1334959187095
date(Local)        :1970-01-01 09:00:00.000
date.value(Local)  :0

同じタグの記事: Dart
同じタグの記事: dartlang
同じタグの記事: Date
同じカテゴリの記事: Program