Skip to content

Instantly share code, notes, and snippets.

@Number-3434
Last active April 17, 2024 16:04
Show Gist options
  • Save Number-3434/8428f7967b85bfd6374e31d190180c3c to your computer and use it in GitHub Desktop.
Save Number-3434/8428f7967b85bfd6374e31d190180c3c to your computer and use it in GitHub Desktop.
Utility library for creating Durations in Dart
import 'custom_durations.dart';
/// {@template customDurationClass}
/// Utility class for creating custom [Duration]s
///
/// This class will not augment [Duration], it is only intended as a shorthand
/// for creating [Duration]s.
///
/// **See also:**
/// - [Duration], the class this is a utility for
/// - [Microseconds]
/// - [Milliseconds]
/// - [Seconds]
/// - [Minutes]
/// - [Hours]
/// - [Days]
/// - [DateTime] to represent a point in time.
/// - [Stopwatch] to measure time-spans.
/// {@endtemplate}
abstract class CustomDuration extends Duration {
const CustomDuration({
num microseconds = 0,
num milliseconds = 0,
num seconds = 0,
num minutes = 0,
num hours = 0,
num days = 0
}) : super(microseconds:
(
microseconds
+ Microseconds.perMillisecond * milliseconds
+ Microseconds.perSecond * seconds
+ Microseconds.perMinute * minutes
+ Microseconds.perHour * hours
+ Microseconds.perDay * days
) ~/ 1 /* The ~/ opertaor performs a DIV operation, returning
its result as an int.
This rounds the number to the nearest microsecond,
whilst still allowing the value to be constant.
Normally, you would use the round() method, but this
isn't constant. */
);
}
/// {@macro customDurationClass}
library;
// Export all the classes out
export 'custom_duration.dart';
export 'days.dart';
export 'hours.dart';
export 'microseconds.dart';
export 'milliseconds.dart';
export 'minutes.dart';
export 'seconds.dart';
// The following are equivalent:
const a1 = Duration(milliseconds: 1);
const b1 = Milliseconds(1);
const c1 = Duration(microseconds: 1000);

// You can also use floating values
const d1 = Seconds(.0001);

// Each class contains shorthands for constant values
const a2 = Duration.millisecondsPerDay;
const a3 = Milliseconds.perDay;

Utility library for creating Durations.

Classes will not augment Duration, since they are only intended as a shorthand for creating Durations.

See also:

import 'custom_durations.dart';
/// {@macro customDurationClass}
class Days extends CustomDuration {
/// Creates a new [Days] from the given [value] (in days).
///
/// If [value] is a [double],
/// the value's accuracy is retained to microseconds.
const Days(
num value, {
super.microseconds,
super.milliseconds,
super.seconds,
super.minutes,
super.hours
}) : super(days: value);
}
import 'custom_durations.dart';
/// {@macro customDurationClass}
class Hours extends CustomDuration {
/// Creates a new [Hours] from the given [value] (in hours).
///
/// [value] is the duration (in hours). If [value] is a [double],
/// the value's accuracy is retained to microseconds.
const Hours(
num value, {
super.microseconds,
super.milliseconds,
super.seconds,
super.minutes,
super.days
}) : super(hours: value);
static const perDay = Duration.hoursPerDay;
}
import 'custom_durations.dart';
/// {@macro customDurationClass}
class Microseconds extends CustomDuration {
/// Creates a new [Microseconds] from the given [value] (in microseconds).
const Microseconds(
num value, {
super.milliseconds,
super.seconds,
super.minutes,
super.hours,
super.days
}) : super(microseconds: value);
static const perMillisecond = Duration.microsecondsPerMillisecond;
static const perSecond = Duration.microsecondsPerSecond;
static const perMinute = Duration.microsecondsPerMinute;
static const perHour = Duration.microsecondsPerHour;
static const perDay = Duration.microsecondsPerDay;
}
import 'custom_durations.dart';
/// {@macro customDurationClass}
class Milliseconds extends CustomDuration {
/// Creates a new [Milliseconds] from the given [value] (in milliseconds).
///
/// [value] is the duration (in hours). If [value] is a [double],
/// the value's accuracy is retained to microseconds.
const Milliseconds(
num value, {
super.microseconds,
super.seconds,
super.minutes,
super.hours,
super.days
}) : super(milliseconds: value);
static const perSecond = Duration.millisecondsPerSecond;
static const perMinute = Duration.millisecondsPerMinute;
static const perHour = Duration.millisecondsPerHour;
static const perDay = Duration.millisecondsPerDay;
}
import 'custom_durations.dart';
/// {@macro customDurationClass}
class Minutes extends CustomDuration {
/// Creates a new [Minutes] from the given [value] (in minutes).
///
/// [value] is the duration (in minutes). If [value] is a [double],
/// the value's accuracy is retained to microseconds.
const Minutes(
num value, {
super.microseconds,
super.milliseconds,
super.seconds,
super.hours,
super.days
}) : super(minutes: value);
static const perHour = Duration.minutesPerHour;
static const perDay = Duration.minutesPerDay;
}
import 'custom_durations.dart';
/// {@macro customDurationClass}
class Seconds extends CustomDuration {
/// Creates a new [Seconds] from the given [value] (in seconds).
///
/// [value] is the duration (in seconds). If [value] is a [double],
/// the value's accuracy is retained to microseconds.
const Seconds(
num value, {
super.microseconds,
super.milliseconds,
super.minutes,
super.hours,
super.days
}) : super(seconds: value);
static const perMinute = Duration.secondsPerMinute;
static const perHour = Duration.secondsPerHour;
static const perDay = Duration.secondsPerDay;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment