Giter Club home page Giter Club logo

duration's Introduction

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

duration's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

duration's Issues

Czech locale should be cz not cs

Czech locale key in locales.dart in locales map should have value cs rather than cz.
At least this is what Platform.localeName gives me 'cs-CZ'

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

Time string with short fractional seconds is parsed incorrectly

Given a string such as 245:09:08.12, the resulting duration parsed returns with milliseconds of 0 and microseconds of 12 which is incorrect. The resulting duration should return with a millisecond of 120 with no microseconds.

Code:

parseTime('245:09:08.12').toString()

Expected:

Duration (245:09:08.120000)

Actual:

Duration (245:09:08.000012)

[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

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.

Merge into intl?

I've noticed that intl package has 'not yet implemented' duration formatters for quite some time now. Is it possible that this package could be implemented in intl?

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.

[Feature] Add maxUnit to prettyDuration

Hi,

The number of units can be fixed using "tersity" and "upperTersity", but it would be nice if it was determined by "upperTersity".

For example:

prettyDuration(
          const Duration(seconds: 5),
          abbreviated: true,
          tersity: DurationTersity.second,
          upperTersity: DurationTersity.week,
          //maxUnit: 2,
)

Current: 5s
Expectation: 5s

prettyDuration(
          const Duration(hours: 8, minutes: 3, seconds: 5),
          abbreviated: true,
          tersity: DurationTersity.second,
          upperTersity: DurationTersity.week,
          //maxUnit: 2,
)

Current: 8h, 3mins, 5s
Expectation: 8h, 3mins

prettyDuration(
          const Duration(days: 10, hours: 8, minutes: 3, seconds: 5),
          abbreviated: true,
          tersity: DurationTersity.second,
          upperTersity: DurationTersity.week,
          //maxUnit: 2,
)

Current: 1w, 3d, 8h, 3mins, 5s
Expectation: 1w, 3d

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!

Documentation has never mentioned prettyDuration method

Thank you for this awesome plugin! I am surprised that the documentation has not mentioned the most important method of the plugin!

prettyDuration()

I almost skipped this plugin because of the documentation. Good thing I took a look at the source.

Error when parsing microseconds from string

When parsing a string with us function tryParseDuration always returns Format error

Example: tryParseDuration('1us');

Reason is a mistake in validation RegExp, extra ] after "us":

final match = RegExp(r'^(\d+)(d|h|m|s|ms|us])$').matchAsPrefix(part);

in file duration-2.0.15/lib/src/parse/parse.dart, line 13

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;
}

Add a way to omit last unit

Hello!
Just a quick word to say that a feature to be able to omit the last unit would be appreciated.

Example
// 
Duration duration = Duration(hours: 4, minutes: 30);

// 4h30min
prettyDuration(
        duration,
        abbreviated: true,
        tersity: DurationTersity.minute,
        delimiter: '',
        spacer: ''
)

I'd like a way to omit the last unit, in this case the minutes. Which would make 4h30.

Thank you for considering this suggestion!

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.