Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/x-slayer/humanize_duration
Humanize a duration to a readable format | 361000 becomes "6 minutes, 1 second"
https://github.com/x-slayer/humanize_duration
duration flutter flutter-package humanize-duration time x-slayer
Last synced: 4 days ago
JSON representation
Humanize a duration to a readable format | 361000 becomes "6 minutes, 1 second"
- Host: GitHub
- URL: https://github.com/x-slayer/humanize_duration
- Owner: X-SLAYER
- License: mit
- Created: 2022-06-07T12:10:55.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-02-08T19:49:00.000Z (9 months ago)
- Last Synced: 2024-10-19T14:26:25.666Z (25 days ago)
- Topics: duration, flutter, flutter-package, humanize-duration, time, x-slayer
- Language: Dart
- Homepage: https://pub.dev/packages/humanize_duration
- Size: 229 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Humanize Duration
Humanize a duration to a readable format.
inspired from [humanizeDuration.js](https://github.com/EvanHahn/HumanizeDuration.js)### Installation and usage
Add package to your pubspec:
```yaml
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.
```dart
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````dart
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](https://github.com/X-SLAYER/Humanize_duration/blob/main/lib/src/humanize_language.dart) class.
```dart
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.
```dart
humanizeDuration(
const Duration(milliseconds: 97320000),
options: const HumanizeOptions(delimiter: ' -- '),
); // 1 day -- 3 hours -- 2 minuteshumanizeDuration(
const Duration(milliseconds: 22140000),
options: const HumanizeOptions(delimiter: ' and '),
); // 6 hours and 9 minutes```
### spacer
String to display between each value and unit.
```dart
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`.```dart
humanizeDuration(
const Duration(milliseconds: 3600000),
options: const HumanizeOptions(units: [Units.hour]),
); // 1 hourhumanizeDuration(
const Duration(milliseconds: 3600000),
options: const HumanizeOptions(units: [Units.minute]),
); // 60 minuteshumanizeDuration(
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.```dart
humanizeDuration(
const Duration(milliseconds: 22140000),
options: const HumanizeOptions(conjunction: ' and '),
); // 6 hours and 9 minuteshumanizeDuration(
const Duration(milliseconds: 22141000),
options: const HumanizeOptions(conjunction: ' and '),
); // 6 hours, 9 minutes and 1 secondhumanizeDuration(
const Duration(milliseconds: 22141000),
options: const HumanizeOptions(
conjunction: ' and ',
lastPrefixComma: true,
),
); // 6 hours, 9 minutes, and 1 second```