duration 2.0.4

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 91

duration #

Utilities to make working with 'Duration's easier.

Format duration #

Use printDuration to print a human readable durations. By default, printDuration will print the duration down to the second. It uses english locale by default.

main() {
    final dur = Duration(
        days: 5,
        hours: 23,
        minutes: 59,
        seconds: 59,
        milliseconds: 999,
        microseconds: 999);
    // => 5d, 23h, 59m, 59s
    printDuration(dur);
  
  // => 3 seconds
  printDuration(aMillisecond * 3000);

  // => 2 seconds 250 milliseconds
  printDuration(aMillisecond * 2250);

  // => 1 day 3 hours 2 minutes
  printDuration(aMillisecond * 97320000);
}

With desired locale #

Use locale parameter to format with desired locale.

main() {
  // => 5 días 9 horas
  printDuration(aDay * 5 + anHour * 9,
      abbreviated: false, locale: spanishLocale);
}

Abbreviate units #

Use abbreviated parameter to use abbreviated units.

main() {
  final dur = Duration(
      days: 5,
      hours: 23,
      minutes: 59,
      seconds: 59,
      milliseconds: 999,
      microseconds: 999);
  // => 5d, 23h, 59m, 59s, 999ms, 999us
  printDuration(dur, abbreviated: true, tersity: DurationTersity.all);
}

Spacer #

Use spacer to add a string between amount and unit.

main() {
  // => 5 whole days 9 whole hours
  printDuration(aDay * 5 + anHour * 9, spacer: ' whole ');
}

Delimiter #

Use delimiter to separate each individual part with a string.

main() {
  // => 5 days, 9 hours and 10 minute
  printDuration(aDay * 5 + anHour * 9 + aMinute * 10, delimiter: ', ');
}

Conjugation #

Use conjugation to add a string before the final unit. Use it in conjunction with delimiter to add ',' and 'and' to separate individual parts.

main() {
  // => 5 days, 9 hours and 10 minutes
  printDuration(aDay * 5 + anHour * 9 + aMinute * 10,
      delimiter: ', ', conjugation: ' and ');
}

Changelog #

2.0.4 #

  • Terisity now takes account missing values inbetween.

2.0.1 #

  • Russian and Portugese support

2.0.0 #

  • Removed dependence on quiver_time
  • Upgraded to Dart 2 stable

1.0.0 #

  • printDuration
  • prettyDuration
  • printSeconds
  • prettySeconds
  • printMilliseconds
  • prettyMilliseconds

example/duration_example.dart

import 'package:duration/duration.dart';
import 'package:duration/locale.dart';

main() {
  // => 5 days 9 hours
  printDuration(aDay * 5 + anHour * 9);

  // More examples

  final dur = Duration(
      days: 5,
      hours: 23,
      minutes: 59,
      seconds: 59,
      milliseconds: 999,
      microseconds: 999);

  // => 5d, 23h, 59m, 59s
  printDuration(dur);

  // => 3.455 milliseconds
  printMilliseconds(aMicrosecond * 3455);

  // => 3 seconds
  printDuration(aMillisecond * 3000);

  // => 2 seconds 250 milliseconds
  printDuration(aMillisecond * 2250);

  // => 1 day 3 hours 2 minutes
  printDuration(aMillisecond * 97320000);

  // => 5 días 9 horas
  printDuration(aDay * 5 + anHour * 9,
      abbreviated: false, locale: spanishLocale);

  // => 5d, 23h, 59m, 59s, 999ms, 999us
  printDuration(dur, abbreviated: true, tersity: DurationTersity.all);

  // => 5 whole days 9 whole hours
  printDuration(aDay * 5 + anHour * 9, spacer: ' whole ');

  // => 5 days, 9 hours, 10 minute
  printDuration(aDay * 5 + anHour * 9 + aMinute * 10, delimiter: ', ');

  // => 5 days, 9 hours and 10 minutes
  printDuration(aDay * 5 + anHour * 9 + aMinute * 10,
      delimiter: ', ', conjugation: ' and ');
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  duration: ^2.0.4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:duration/duration.dart';
  
Version Uploaded Documentation Archive
2.0.4 Jan 21, 2019 Go to the documentation of duration 2.0.4 Download duration 2.0.4 archive
2.0.2 Jan 9, 2019 Go to the documentation of duration 2.0.2 Download duration 2.0.2 archive
2.0.1 Jan 9, 2019 Go to the documentation of duration 2.0.1 Download duration 2.0.1 archive
2.0.0 Aug 18, 2018 Go to the documentation of duration 2.0.0 Download duration 2.0.0 archive
1.0.0 Jan 1, 2018 Go to the documentation of duration 1.0.0 Download duration 1.0.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
91
Health:
Code health derived from static analysis. [more]
90
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
91
Learn more about scoring.

We analyzed this package on May 9, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.0
  • pana: 0.12.15

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:duration/duration.dart.

Health suggestions

Fix lib/src/milliseconds.dart. (-6.31 points)

Analysis of lib/src/milliseconds.dart reported 13 hints, including:

line 9 col 16: Use = to separate a named parameter from its default value.

line 10 col 28: Use = to separate a named parameter from its default value.

line 11 col 21: Use = to separate a named parameter from its default value.

line 12 col 21: Use = to separate a named parameter from its default value.

line 48 col 16: Use = to separate a named parameter from its default value.

Fix lib/src/duration.dart. (-3.45 points)

Analysis of lib/src/duration.dart reported 7 hints, including:

line 32 col 29: Use = to separate a named parameter from its default value.

line 33 col 26: Use = to separate a named parameter from its default value.

line 37 col 21: Use = to separate a named parameter from its default value.

line 80 col 37: Use isEmpty instead of length

line 113 col 29: Use = to separate a named parameter from its default value.

Maintenance suggestions

The package description is too short. (-10 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.3.0