date_utils.dart 2.71 KB
Newer Older
Eric Seidel's avatar
Eric Seidel committed
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Forked from https://github.com/dart-lang/sdk/blob/master/samples-dev/swarm/swarm_ui_lib/util/DateUtils.dart
class DateUtils {

  static const WEEKDAYS = const ['Monday', 'Tuesday', 'Wednesday', 'Thursday',
                                 'Friday', 'Saturday', 'Sunday'];

  static const YESTERDAY = 'Yesterday';

Hixie's avatar
Hixie committed
13
  static const MS_IN_WEEK = DateTime.DAYS_PER_WEEK * Duration.MILLISECONDS_PER_DAY;
Eric Seidel's avatar
Eric Seidel committed
14

15 16
  // TODO(jmesserly): locale specific date format
  static String _twoDigits(int n) {
Hixie's avatar
Hixie committed
17 18 19
    if (n >= 10)
      return '$n';
    return '0$n';
20 21
  }

Hixie's avatar
Hixie committed
22
  /// Formats a time in H:MM A format
Eric Seidel's avatar
Eric Seidel committed
23 24 25 26 27 28
  static String toHourMinutesString(Duration duration) {
    assert(duration.inDays == 0);
    int hours = duration.inHours;
    String a;
    if (hours >= 12) {
      a = 'pm';
Hixie's avatar
Hixie committed
29
      if (hours != 12)
Eric Seidel's avatar
Eric Seidel committed
30 31 32
        hours -= 12;
    } else {
      a = 'am';
Hixie's avatar
Hixie committed
33
      if (hours == 0)
Eric Seidel's avatar
Eric Seidel committed
34 35 36
        hours += 12;
    }
    String twoDigits(int n) {
Hixie's avatar
Hixie committed
37 38 39
      if (n >= 10)
        return '$n';
      return '0$n';
Eric Seidel's avatar
Eric Seidel committed
40
    }
Hixie's avatar
Hixie committed
41 42
    String mm = twoDigits(duration.inMinutes.remainder(Duration.MINUTES_PER_HOUR));
    return '$hours:$mm $a';
Eric Seidel's avatar
Eric Seidel committed
43 44
  }

Hixie's avatar
Hixie committed
45 46 47 48 49
  /// A date/time formatter that takes into account the current date/time:
  ///  - if it's from today, just show the time
  ///  - if it's from yesterday, just show 'Yesterday'
  ///  - if it's from the same week, just show the weekday
  ///  - otherwise, show just the date
Eric Seidel's avatar
Eric Seidel committed
50 51
  static String toRecentTimeString(DateTime then) {
    bool datesAreEqual(DateTime d1, DateTime d2) {
Hixie's avatar
Hixie committed
52 53 54
      return (d1.year == d2.year) &&
             (d1.month == d2.month) &&
             (d1.day == d2.day);
Eric Seidel's avatar
Eric Seidel committed
55 56 57 58 59
    }

    final now = new DateTime.now();
    if (datesAreEqual(then, now)) {
      return toHourMinutesString(new Duration(
Hixie's avatar
Hixie committed
60 61 62 63 64 65
        days: 0,
        hours: then.hour,
        minutes: then.minute,
        seconds: then.second,
        milliseconds: then.millisecond)
      );
Eric Seidel's avatar
Eric Seidel committed
66 67 68 69 70 71 72 73 74
    }

    final today = new DateTime(now.year, now.month, now.day, 0, 0, 0, 0);
    Duration delta = today.difference(then);
    if (delta.inMilliseconds < Duration.MILLISECONDS_PER_DAY) {
      return YESTERDAY;
    } else if (delta.inMilliseconds < MS_IN_WEEK) {
      return WEEKDAYS[then.weekday];
    } else {
75 76
      String twoDigitMonth = _twoDigits(then.month);
      String twoDigitDay = _twoDigits(then.day);
Hixie's avatar
Hixie committed
77
      return '${then.year}-$twoDigitMonth-$twoDigitDay';
Eric Seidel's avatar
Eric Seidel committed
78 79
    }
  }
80 81 82 83 84

  static String toDateString(DateTime then) {
    // TODO(jmesserly): locale specific date format
    String twoDigitMonth = _twoDigits(then.month);
    String twoDigitDay = _twoDigits(then.day);
Hixie's avatar
Hixie committed
85
    return '${then.year}-$twoDigitMonth-$twoDigitDay';
86
  }
Eric Seidel's avatar
Eric Seidel committed
87
}