Giter Site home page Giter Site logo

x-slayer / humanize_duration Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 1.0 234 KB

Humanize a duration to a readable format | 361000 becomes "6 minutes, 1 second"

Home Page: https://pub.dev/packages/humanize_duration

License: MIT License

Dart 40.81% Kotlin 0.17% Swift 1.57% Objective-C 0.05% CMake 24.61% C++ 28.38% C 1.93% HTML 2.47%
duration flutter flutter-package humanize-duration time x-slayer

humanize_duration's Introduction

Humanize Duration

Humanize a duration to a readable format. inspired from humanizeDuration.js

Installation and usage

Add package to your pubspec:

dependencies:
  humanize_duration: any # or the latest version on Pub

Usage

By default, Humanize Duration will humanize down to the millisecond. It will humanize in English by default.

humanizeDuration(const Duration(milliseconds: 3000)); // '3 seconds'
humanizeDuration(const Duration(milliseconds: 97320000)); // '1 day, 3 hours, 2 minutes'

Options and languages

languages

Supported languages: [Arabic, French, English, Spanish ...] For a list of supported languages, you can use the getSupportedLanguages

getSupportedLanguages(); // [ar, en, fr, es, jp, gr, du, fa, ge, it, ko, pt, ru, tr, zh_cn, zh_tw]

humanizeDuration(
  const Duration(milliseconds: 97320000),
  language: const ArLanguage(),
);
//  ١ يوم , ٣ ساعات

// OR get language by alpha
humanizeDuration(
  const Duration(milliseconds: 97320000),
  language: getLanguageByLocale('ar'),
);

Add a custom language

Firstly you need to implement HumanizeLanguage class.

import 'package:humanize_duration/humanize_duration.dart';

class EuLanguage implements HumanizeLanguage {
 const EuLanguage();

 @override
 String name() => 'eu';

 @override
 String day(int value) => 'egun';

 @override
 String hour(int value) => 'ordu';

 @override
 String millisecond(int value) => 'milisegundo';

 @override
 String minute(int value) => 'minutu';

 @override
 String month(int value) => 'hilabete';

 @override
 String second(int value) => 'segundo';

 @override
 String week(int value) => 'aste';

 @override
 String year(int value) => 'hilabete';
}

delimiter

String to display between the previous unit and the next value.

humanizeDuration(
    const Duration(milliseconds: 97320000),
    options: const HumanizeOptions(delimiter: ' -- '),
  ); // 1 day -- 3 hours -- 2 minutes

humanizeDuration(
  const Duration(milliseconds: 22140000),
  options: const HumanizeOptions(delimiter: ' and '),
); // 6 hours and 9 minutes

spacer

String to display between each value and unit.

humanizeDuration(
  const Duration(milliseconds: 22140000),
  options: const HumanizeOptions(spacer: ' whole '),
); // 6 whole hours, 9 whole minutes

units

It can be one, or a combination of any, of the following. Units.year, Units.month, Units.week, Units.day, Units.hour, Units.minute, Units.second, Units.millisecond.

humanizeDuration(
  const Duration(milliseconds: 3600000),
  options: const HumanizeOptions(units: [Units.hour]),
); // 1 hour

humanizeDuration(
  const Duration(milliseconds: 3600000),
  options: const HumanizeOptions(units: [Units.minute]),
); // 60 minutes

humanizeDuration(
  const Duration(milliseconds: 3600000),
  options: const HumanizeOptions(units: [Units.day, Units.hour]),
); // 1 hour

conjunction

String to include before the final unit. You can also set lastPrefixComma to false to eliminate the final comma.

humanizeDuration(
  const Duration(milliseconds: 22140000),
  options: const HumanizeOptions(conjunction: ' and '),
); // 6 hours and 9 minutes

humanizeDuration(
  const Duration(milliseconds: 22141000),
  options: const HumanizeOptions(conjunction: ' and '),
); // 6 hours, 9 minutes and 1 second

 humanizeDuration(
  const Duration(milliseconds: 22141000),
  options: const HumanizeOptions(
    conjunction: ' and ',
    lastPrefixComma: true,
  ),
); // 6 hours, 9 minutes, and 1 second

humanize_duration's People

Contributors

x-slayer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

flutterworks

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.