Utilities to make working with 'Duration's easier.

Overview

duration pub package

Utilities to make working with 'Duration's easier.

NOTE: Use prettyDuration, prettySeconds, prettyMilliseconds instead of printDuration, printSeconds, printMilliseconds if you only want to format/convert and don't want to print to console!

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: DurationLocale.fromLanguageCode('ru'),
  );
}

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 ',
  );
}

Parse duration

Parse duration

main() {
  final Duration dur = parseDuration('245:09:08.007006');
  print(dur);
}

Parse time

main() {
  final Duration dur = parseTime('245:09:08.007006');
  print(dur);
}
Comments
  • Max Tersity and Hebrew locale

    Max Tersity and Hebrew locale

    I added an option called maxTersity which allows the limiting of the number of time epochs that are being printed. This is useful if you have limited space for the time string and you don't care about losing the granularity of smaller time epochs.

    I also added Hebrew locale. It works 95% but due to syntactic differences in the Hebrew language support for the other 5% will require some architectural changes. I will make these in a separate pull request

    opened by yonatann 3
  • Fixed DurationTersity being dependant on time contained inside Duration

    Fixed DurationTersity being dependant on time contained inside Duration

    Right now if you specify for instance DurationTersity.minute but the Duration is less than 1 day, printDuration will print hours, minutes and seconds. This commit ensures that printDuration will always only print up to the limit set in the tersity parameter.

    opened by eligt 3
  • Add Czech locale

    Add Czech locale

    I'm adding support for Czech language.

    I hope you don't mind creating enum CzechDurationLocalePluralization as using switch statement feels more natural to me.

    On the other hand, this enum will be available to consumers because it's public API of library now, if it's a problem I can just replace it with if-else statements instead.

    opened by comatory 2
  • Can not parse duration from a string with weeks

    Can not parse duration from a string with weeks

    If I try to parse the duration '19w, 3d, 5m' I got an exception "Invalid duration format". But if I try to parse the duration '138d, 5m' I got the correct result.

    The error is because the regex does not expect the 'w' format: final match = RegExp(r'^(\d+)(d|h|m|s|ms|us)$').matchAsPrefix(part);

    So I can go from a duration to a text but I can't go from a text to a duration.

    Can you fix this please?

    Thank you!

    opened by GrupoDO 2
  • Remove debug logging from printDuration

    Remove debug logging from printDuration

    Looks like printDuration has a debug logging in. See here

    Reproduced below

    String printDuration(Duration duration,
        {DurationTersity tersity = DurationTersity.second,
        DurationLocale locale = const EnglishDurationLocale(),
        String spacer,
        String delimiter,
        String conjugation,
        bool abbreviated = false}) {
      final String fmt = prettyDuration(duration,
          tersity: tersity,
          locale: locale,
          spacer: spacer,
          delimiter: delimiter,
          conjunction: conjugation,
          abbreviated: abbreviated);
      print(fmt);
      return fmt;
    }
    
    opened by jeffjose 2
  • [Suggestion] Support

    [Suggestion] Support "uppperTersity"

    Support "uppperTersity"

    Example: If you have Duration(hours: 25), you may want it to print hours 25 instead of days 1 hours 24

    Adding an "upperTersity" option would enable such behavior

    opened by nwparker 2
  • Formats supported by parse function?

    Formats supported by parse function?

    To me from the documentation, it is not clear what format the parse function supports. I'm currently interested into that function but I'm holding back as the documentation is not clear.

    opened by ichitaka 2
  • Year, month and week support added

    Year, month and week support added

    I need to print duration as a year, month and week too, I just add these features to your package and update the version, the changelog of the package. pub publish is enough 💯

    I need these features so can you publish the new version of the package as soon as possible by the way, Would you consider adding me as an uploader to be able to upload new features?

    opened by onatcipli 2
  • Always printing. ALWAYS.

    Always printing. ALWAYS.

    First of all thanks for the great plugin. Second of all is there a way to get the duration without printing it to the terminal?

    It is super annoying doing debug with this especially if i'm checking for the duration every second. That means a print statement in every second.

    Thanks again for plugin it just needs some more locales. If you need help with the Arabic locale send me a message.

    opened by omargalal20084 2
  • Add Japanese locale and Add defaultSpacer for CJK locale

    Add Japanese locale and Add defaultSpacer for CJK locale

    Hi, I just added Japanese locale.

    and I added defaultSpacer to DurationLocale for CJK locale. It should be no space between value and unit on CJK locale.

    const dur = Duration(days: 5, hours: 23);
    
    prettyDuration(dur, locale: ChineseSimplifiedDurationLocale())
    // current: '5 日 23 小时'
    // changed: '5日 23小时'
    
    prettyDuration(dur, locale: JapaneseDurationLocale())
    // current: '5 日 23 時間'
    // changed: '5日 23時間'
    
    prettyDuration(dur, locale: KoreanDurationLocale())
    // current: '5 일 23 시간'
    // changed: '5일 23시간'
    

    Thank you!

    opened by kyle-seongwoo-jun 1
  • upperTersity param not working in printDuration

    upperTersity param not working in printDuration

    I am trying to limit the upperTersity to days, but its taking the default value of weeks. The code is -

    final playTime = Duration(minutes: value); // value = 14966
    final humanizedTime = printDuration(
        playTime,
        abbreviated: true,
        tersity: DurationTersity.hour,
        upperTersity: DurationTersity.day,
    ); // output = 1w, 3d, 9h
    

    I have tested the tersity param and its working. upperTersity is not working with any value of DurationTersity, it is always giving weeks

    opened by tusharlock10 1
  • Handle negative durations properly

    Handle negative durations properly

    Dart 2.18, Windows 10 64-bit This code:

    import "package:duration/duration.dart";
    void main() {
    	print(prettyDuration(Duration(seconds: -1), abbreviated: false));
    }
    

    prints: 59 seconds
    when I run it, instead of something like 1 second ago.

    opened by Keithcat1 0
  • [CJK] update hour() about abbreviation

    [CJK] update hour() about abbreviation

    I fixed wrong expressions. these shouldn't be abbreviated.

    时 / 時 / 시 are units used to indicate specific time. (e.g. at 1 o' clock)

    小时 / 小時 / 時間 / 시간 are units used to indicate duration. (e.g. it takes 1 hour.)

    opened by kyle-seongwoo-jun 0
  • PrettyDuration with abbreviated : true has incorrect identifier for minutes.

    PrettyDuration with abbreviated : true has incorrect identifier for minutes.

    This code snippet produces 5h, 33min, 20s. I expected min to be abbreviated to m.

      prettyDuration(
        Duration(milliseconds: 20000000),
        abbreviated: true,
      );
    
    opened by bruntzcreative 0
  • Add number of months instead of weeks if they exceed a certain amount

    Add number of months instead of weeks if they exceed a certain amount

    • default is 2 months as until that a timespan in weeks is sufficient (totally based on my personal gut feeling)
    • calculates with a rough average of 365 / 12 = 30.42 days per month
    opened by sebastianmueller 1
Owner
null
Okan YILDIRIM 37 Jul 10, 2022
Material color utilities

Material color utilities Algorithms and utilities that power the Material Design 3 (M3) color system, including choosing theme colors from images and

null 878 Jan 8, 2023
Generates utilities to aid in serializing to/from JSON.

Provides Dart Build System builders for handling JSON. json_serializable Package: https://pub.dev/packages/json_serializable Source code The core pack

Google 1.4k Jan 8, 2023
Working proof of the Go server running inside Flutter

flap Working proof of the Go server running inside Flutter Video in action Prerequisites Flutter >2.0 Go >1.16 Build Go server cd go macOS: make maco

cPidx 28 Dec 17, 2022
An http request client, which supports Manageable Requests Cancellation, Request Policy (Timeout and Retry), Easier Multipart Requests, etc.

A wrapper around Dart's http package, which supports Manageable Requests Cancellation, Request Policy (Timeout and Retry), Easier Multipart Requests, Error Handling, etc.

Iandi Santulus 5 Oct 10, 2021
Quiver is a set of utility libraries for Dart that makes using many Dart libraries easier and more convenient, or adds additional functionality.

Quiver is a set of utility libraries for Dart that makes using many Dart libraries easier and more convenient, or adds additional functionality.

Google 905 Jan 2, 2023
This library contains methods that make it easy to consume Mpesa Api.

This library contains methods that make it easy to consume Mpesa Api. It's multi-platform, and supports CLI, server, mobile, desktop, and the browser.

Eddie Genius 3 Dec 15, 2021
A JMAP client library in Dart to make JMAP method calls and process the responses

JMAP Dart client A JMAP client library to make JMAP method calls and process the responses. We most notably use it to write the TMail Flutter applicat

LINAGORA 18 Dec 19, 2022
Library for help you make userbot or bot telegram and support tdlib telegram database and only support nodejs dart and google-apps-script

To-Do telegram client dart ✅️ support multi token ( bot / userbot ) ✅️ support bot and userbot ✅️ support telegram-bot-api local server ✅️ support tel

Azka Full Snack Developer:) 73 Jan 7, 2023
A Flutter library to make Rest API clients more easily. Inspired by Java Feing.

A Flutter library to make Rest API clients more easily. Inspired by Java Feing. Features Facilitated JSON encode and decode using common interfaces. F

null 2 Mar 15, 2022
Contains utility functions and classes in the style of dart:collection to make working with collections easier

The collection package for Dart contains a number of separate libraries with utility functions and classes that makes working with collections easier.

Dart 273 Dec 27, 2022
Nebula makes your Flutter development journey easier by providing helper widgets, utilities and abstractions.

Nebula makes your Flutter development journey easier by providing helper widgets, utilities and abstractions.

Aldrin's Art Factory 1 Apr 21, 2022
The essential IntelliJ/Android Studio plugin for making working with Flutter easier than ever!

Flutter Enhancement Suite The essential plugin for making working with Flutter easier than ever! Easy-to-use tools for managing your pubspec.yaml, sni

Marius Höfler 241 Dec 29, 2022
This package helps developer to sort the flutter/dart packages and plugins alphabetically, This makes it easier when managing too many packages and when working with teams

Package helps to sort the flutter/dart packages and plugins alphabetically, This makes it easier when managing too many packages and when working with

DANCHE 7 Dec 21, 2022
Addons to supabase dart (and Flutter), to make development easier.

supabase_addons Make great apps with a great backend! Supabase is an open source Firebase alternative. It has support for auth, database and storage u

Bruno D'Luka 20 Dec 3, 2022
A wrapper around Navigator 2.0 and Router/Pages to make their use a easier.

APS Navigator - App Pagination System This library is just a wrapper around Navigator 2.0 and Router/Pages API that tries to make their use easier: ??

Guilherme Silva 14 Oct 17, 2022
Flutter tips and tricks to make the development smoother and easier

Table of Contents Custom Clippers in Flutter Check if Website is Up or Down in Dart Section Titles on ListView in Flutter Circular Progress in Flutter

Praharsh Bhatt 17 Oct 11, 2022
A performant, expressjs like server framework with a few gadgets that make life even easier.

Alfred A performant, expressjs like server framework thats easy to use and has all the bits in one place. Quickstart: import 'package:alfred/alfred.da

Ryan Knell 449 Jan 2, 2023
Powerful, helpfull, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server with boilerplate code free.

flutter_axelor_sdk Powerful, helpful, extensible and highly customizable API's that wrap http client to make communication easier with Axelor server w

Abd al-Rahman al-Ktefane 5 Dec 25, 2022
A Deep Learning Based Attendance System is a mobile application that aims to make it easier for lecturers to check the attendance status of students which are attending the course.

Attendance System / Flutter App A Deep Learning Based Attendance System is a mobile application that aims to make it easier for lecturers to check the

Merthan Kavak 11 Oct 24, 2022