Created
May 4, 2012 15:02
-
-
Save seaneagan/2595325 to your computer and use it in GitHub Desktop.
Adding weeks, months, and years to Duration
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
interface Duration default DurationImplementation { | |
// exact | |
static final int MONTHS_PER_YEAR = 12; | |
static final int DAYS_PER_WEEK = 7; | |
static final int MINUTES_PER_HOUR = 60; | |
static final int SECONDS_PER_MINUTE = 60; | |
static final int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR; | |
static final int MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR; | |
static final int MILLISECONDS_PER_MINUTE = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE; | |
static final int MILLISECONDS_PER_SECOND = 1000; | |
// standard | |
static final int STANDARD_DAYS_PER_YEAR = 365; | |
static final int DAYS_PER_LEAP_YEAR = STANDARD_DAYS_PER_YEAR + 1; | |
static final int STANDARD_HOURS_PER_DAY = 24; | |
static final int STANDARD_MINUTES_PER_DAY = MINUTES_PER_HOUR * STANDARD_HOURS_PER_DAY; | |
static final int STANDARD_SECONDS_PER_DAY = SECONDS_PER_HOUR * STANDARD_HOURS_PER_DAY; | |
static final int STANDARD_MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * STANDARD_HOURS_PER_DAY; | |
// average | |
static final num AVERAGE_DAYS_PER_YEAR = STANDARD_DAYS_PER_YEAR + 1/4 - 1/100 + 1/400; | |
static final num AVERAGE_DAYS_PER_MONTH = AVERAGE_DAYS_PER_YEAR / MONTHS_PER_YEAR; | |
static final num AVERAGE_WEEKS_PER_YEAR = AVERAGE_DAYS_PER_YEAR / DAYS_PER_WEEK; | |
static final num AVERAGE_WEEKS_PER_MONTH = AVERAGE_DAYS_PER_MONTH / DAYS_PER_WEEK; | |
/// normalizes into milliseconds, days, and months fields | |
const Duration([ | |
int milliseconds, | |
int seconds, | |
int minutes, | |
int hours, | |
int days, | |
int weeks, | |
int months, | |
int years]); | |
final int milliseconds, days, months; | |
// non-exact | |
int get inMilliseconds(); | |
int get inSeconds(); | |
int get inMinutes(); | |
int get inHours(); | |
int get inDays(); | |
int get inWeeks(); | |
int get inMonths(); | |
int get inYears(); | |
} | |
class DurationImplementation { | |
const DurationImplementation([ | |
int years = 0, | |
int months = 0, | |
int weeks = 0, | |
int days = 0, | |
int hours = 0, | |
int minutes = 0, | |
int seconds = 0, | |
int milliseconds = 0]) : | |
months = years * Duration.MONTHS_PER_YEAR + months, | |
days = weeks * Duration.DAYS_PER_WEEK + days, | |
milliseconds = | |
hours * Duration.MILLISECONDS_PER_HOUR + | |
minutes * Duration.MILLISECONDS_PER_MINUTE + | |
seconds * Duration.MILLISECONDS_PER_SECOND + | |
milliseconds; | |
final int milliseconds, days, months; | |
int get inMilliseconds() => _inMilliseconds.floor(); | |
int get inSeconds() => _inSeconds.floor(); | |
int get inMinutes() => _inMinutes.floor(); | |
int get inHours() => _inHours.floor(); | |
int get inDays() => _inDays.floor(); | |
int get inWeeks() => _inWeeks.floor(); | |
int get inMonths() => _inMonths.floor(); | |
int get inYears() => _inYears.floor(); | |
num get _inMilliseconds() => milliseconds + (days + _monthsAsDays) * Duration.STANDARD_MILLISECONDS_PER_DAY; | |
num get _inSeconds() => _inMilliseconds / Duration.MILLISECONDS_PER_SECOND; | |
num get _inMinutes() => _inMilliseconds / Duration.MILLISECONDS_PER_MINUTE; | |
num get _inHours() => _inMilliseconds / Duration.MILLISECONDS_PER_HOUR; | |
num get _inDays() => days + _monthsAsDays + _millisecondsAsDays; | |
num get _inWeeks() => _inDays / Duration.DAYS_PER_WEEK; | |
num get _inMonths() => months + (days + _millisecondsAsDays) / Duration.AVERAGE_DAYS_PER_MONTH; | |
num get _inYears() => _inMonths / Duration.MONTHS_PER_YEAR; | |
num get _monthsAsDays() => months * Duration.AVERAGE_DAYS_PER_MONTH; | |
num get _millisecondsAsDays() => milliseconds / Duration.STANDARD_MILLISECONDS_PER_DAY; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment