├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── example └── duration_example.dart ├── lib ├── duration.dart ├── locale.dart └── src │ ├── duration.dart │ ├── locale │ ├── arabic.dart │ ├── chinese_hans.dart │ ├── chinese_hant.dart │ ├── czech.dart │ ├── dutch.dart │ ├── english.dart │ ├── finnish.dart │ ├── french.dart │ ├── german.dart │ ├── greek.dart │ ├── hebrew.dart │ ├── indonesian.dart │ ├── italian.dart │ ├── japanese.dart │ ├── korean.dart │ ├── locale.dart │ ├── norwegian.dart │ ├── polish.dart │ ├── portuguese_br.dart │ ├── romanian.dart │ ├── russian.dart │ ├── spanish.dart │ ├── swedish.dart │ ├── thai.dart │ ├── turkish.dart │ ├── ukrainian.dart │ └── vietnamese.dart │ ├── milliseconds.dart │ ├── parse │ └── parse.dart │ ├── tersity.dart │ └── time_const.dart ├── pubspec.yaml └── test ├── locale └── ukrainian_test.dart ├── locale_test.dart ├── parse └── time_test.dart ├── pretty_duration_test.dart └── pretty_second_test.dart /.gitignore: -------------------------------------------------------------------------------- 1 | # Files and directories created by pub 2 | .packages 3 | .dart_tool 4 | .pub/ 5 | build/ 6 | # Remove the following pattern if you wish to check in your lock file 7 | pubspec.lock 8 | 9 | # Directory created by dartdoc 10 | doc/api/ 11 | 12 | .idea 13 | *.iml -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 4.0.3 4 | 5 | + Replace `first` with `maxUnits` in `Duration.pretty()` 6 | 7 | ## 4.0.2 8 | 9 | + `parseDuration` exception message fixes 10 | 11 | ## 4.0.1 12 | 13 | + README and some documentation fixes 14 | 15 | ## 4.0.0 16 | 17 | + Extension method `Duration.pretty()` 18 | + Handle negative durations 19 | 20 | **Breaking change**: 21 | + Removed `printDuration` and `printMilliseconds` functions 22 | 23 | ## 3.0.15 24 | 25 | **Breaking change**: 26 | + Czeck locale should be `cs` not `cz` 27 | 28 | ## 3.0.14 29 | 30 | + Vietnamese locale 31 | + Greek locale 32 | + `DurationTersity` is now enhanced Enum 33 | 34 | ## 3.0.13 35 | 36 | + Abbreviation fix for Chinese, Japanese and Korean 37 | 38 | ## 3.0.12 39 | 40 | + Ukranian locale 41 | 42 | ## 3.0.11 43 | 44 | + printDuration bug fix for upperTersity 45 | 46 | ## 3.0.10 47 | 48 | + Upper tersity support 49 | + **Breaking change**: DurationTersity.id is made private 50 | + **Breaking change**: DurationTersity order is reversed 51 | 52 | ## 3.0.9 53 | 54 | + Added Finish locale 55 | 56 | ## 3.0.8 57 | 58 | + **Breaking change**: In Dutch locale, minute is now abbreviated as 'min' not 'm' 59 | + parseDuration now parses weeks 60 | + Added some documentation to parseDuration and parseTime 61 | 62 | ## 3.0.7 63 | 64 | + Chinese and Czech languages 65 | + **Breaking changes** to chinese locale 66 | + **Breaking change**: Minute is now abbreviated as 'min' not 'm' 67 | 68 | ## 3.0.6 69 | 70 | + Fixed `parseDuration` 71 | + Documented `parseDuration` in README 72 | 73 | ## 3.0.5 74 | 75 | + README note on `prettyDuration` 76 | 77 | ## 3.0.4 78 | 79 | + English plural fix for 0 years 80 | 81 | ## 3.0.2 82 | 83 | + English plural fix for negative amounts 84 | 85 | ## 3.0.1 86 | 87 | + Added Indonesian, Korean, Romanian and Arabic locales 88 | 89 | ## 3.0.0 90 | 91 | + Migration to null safety 92 | 93 | ## 2.0.15 94 | 95 | + Hebrew locale 96 | 97 | ## 2.0.14 98 | 99 | + Chinese locale 100 | + Dutch locale 101 | 102 | ## 2.0.12 103 | 104 | + Fix for Itallian locale 105 | + Fix for German locale 106 | 107 | ## 2.0.11 108 | 109 | + dartfmt 110 | 111 | ## 2.0.10 112 | 113 | + Added `ItalianDurationLocale` 114 | 115 | ## 2.0.9 116 | 117 | + Added `DurationLocale.fromLanguageCode` 118 | + Added constants for all locales 119 | 120 | ## 2.0.6 121 | 122 | + `tryParseDuration`, `tryParseTime` and `tryParseDurationAny` 123 | 124 | ## 2.0.5 125 | 126 | + Added `parseDuration` 127 | 128 | ## 2.0.4 129 | 130 | + Terisity now takes account missing values inbetween. 131 | 132 | ## 2.0.1 133 | 134 | + Russian and Portugese support 135 | 136 | ## 2.0.0 137 | 138 | + Removed dependence on `quiver_time` 139 | + Upgraded to Dart 2 stable 140 | 141 | ## 1.0.0 142 | 143 | + `printDuration` 144 | + `prettyDuration` 145 | + `printSeconds` 146 | + `prettySeconds` 147 | + `printMilliseconds` 148 | + `prettyMilliseconds` 149 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, Ravi Teja Gudapati 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # duration [![pub package](https://img.shields.io/pub/v/duration.svg)](https://pub.dartlang.org/packages/duration) 2 | 3 | Utilities to make working with 'Duration's easier. 4 | 5 | # Format duration 6 | 7 | Use `Duration.pretty()` method or `prettyDuration` function to format a duration. 8 | 9 | ```dart 10 | main() { 11 | final dur = Duration( 12 | days: 5, 13 | hours: 23, 14 | minutes: 59, 15 | seconds: 59, 16 | milliseconds: 999, 17 | microseconds: 999, 18 | ); 19 | 20 | // => 5d, 23h, 59m, 59s 21 | print(dur.pretty()); 22 | 23 | // => 3 seconds 24 | print((aMillisecond * 3000).pretty()); 25 | 26 | // => 2 seconds 250 milliseconds 27 | print((aMillisecond * 2250).pretty); 28 | 29 | // => 1 day 3 hours 2 minutes 30 | print((aMillisecond * 97320000).pretty()); 31 | } 32 | ``` 33 | 34 | ## With desired locale 35 | 36 | Use `locale` parameter to format with desired locale. 37 | 38 | ```dart 39 | main() { 40 | // => 5 días 9 horas 41 | final dur = aDay * 5 + anHour * 9; 42 | print( 43 | dur.pretty( 44 | abbreviated: false, 45 | locale: DurationLocale.fromLanguageCode('ru'), 46 | )); 47 | } 48 | ``` 49 | 50 | ## Abbreviate units 51 | 52 | Use `abbreviated` parameter to use abbreviated units. 53 | 54 | ```dart 55 | main() { 56 | final dur = Duration( 57 | days: 5, 58 | hours: 23, 59 | minutes: 59, 60 | seconds: 59, 61 | milliseconds: 999, 62 | microseconds: 999, 63 | ); 64 | 65 | // => 5d, 23h, 59m, 59s, 999ms, 999us 66 | print(dur.pretty(abbreviated: true, tersity: DurationTersity.all)); 67 | } 68 | ``` 69 | 70 | ## Spacer 71 | 72 | Use `spacer` to add a string between amount and unit. 73 | 74 | ```dart 75 | main() { 76 | // => 5 whole days 9 whole hours 77 | print((aDay * 5 + anHour * 9).pretty(spacer: ' whole ')); 78 | } 79 | ``` 80 | 81 | ## Delimiter 82 | 83 | Use `delimiter` to separate each individual part with a string. 84 | 85 | ```dart 86 | main() { 87 | // => 5 days, 9 hours and 10 minute 88 | print((aDay * 5 + anHour * 9 + aMinute * 10).pretty(delimiter: ', ')); 89 | } 90 | ``` 91 | 92 | ## Conjugation 93 | 94 | Use `conjugation` to add a string before the final unit. Use it in conjunction with 95 | delimiter to add ',' and 'and' to separate individual parts. 96 | 97 | ```dart 98 | main() { 99 | // => 5 days, 9 hours and 10 minutes 100 | print( 101 | (aDay * 5 + anHour * 9 + aMinute * 10).pretty( 102 | delimiter: ', ', 103 | conjugation: ' and ', 104 | )); 105 | } 106 | ``` 107 | 108 | # Parse duration 109 | 110 | ## Parse duration 111 | 112 | ```dart 113 | main() { 114 | final Duration dur = parseDuration('245:09:08.007006'); 115 | print(dur); 116 | } 117 | ``` 118 | 119 | ## Parse time 120 | 121 | ```dart 122 | main() { 123 | final Duration dur = parseTime('245:09:08.007006'); 124 | print(dur); 125 | } 126 | ``` -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | analyzer: 2 | 3 | include: package:lints/recommended.yaml 4 | 5 | linter: 6 | rules: 7 | omit_local_variable_types: false 8 | -------------------------------------------------------------------------------- /example/duration_example.dart: -------------------------------------------------------------------------------- 1 | import 'package:duration/duration.dart'; 2 | import 'package:duration/locale.dart'; 3 | 4 | void main() { 5 | // => 5 days 9 hours 6 | print((aDay * 5 + anHour * 9).pretty()); 7 | 8 | // More examples 9 | 10 | final dur = Duration( 11 | days: 5, 12 | hours: 23, 13 | minutes: 59, 14 | seconds: 59, 15 | milliseconds: 999, 16 | microseconds: 999); 17 | 18 | // => 5 days 23 hours 59 minutes 59 seconds 19 | print(dur.pretty()); 20 | 21 | // => 3.455 milliseconds 22 | print(prettyMilliseconds(aMicrosecond * 3455)); 23 | 24 | // => 3 seconds 25 | print((aMillisecond * 3000).pretty()); 26 | 27 | // => 2 seconds 28 | print((aMillisecond * 2250).pretty()); 29 | 30 | // => 1 day 3 hours 2 minutes 31 | print((aMillisecond * 97320000).pretty()); 32 | 33 | // => 5 días 9 horas 34 | print((aDay * 5 + anHour * 9) 35 | .pretty(abbreviated: false, locale: spanishLocale)); 36 | 37 | // => 5d, 23h, 59m, 59s, 999ms, 999us 38 | print(dur.pretty(abbreviated: true, tersity: DurationTersity.microsecond)); 39 | 40 | // => 5 whole days 9 whole hours 41 | print((aDay * 5 + anHour * 9).pretty(spacer: ' whole ')); 42 | 43 | // => 5 days, 9 hours, 10 minute 44 | print((aDay * 5 + anHour * 9 + aMinute * 10).pretty(delimiter: ', ')); 45 | 46 | // => 5 days, 9 hours and 10 minutes 47 | print((aDay * 5 + anHour * 9 + aMinute * 10) 48 | .pretty(delimiter: ', ', conjunction: ' and ')); 49 | } 50 | -------------------------------------------------------------------------------- /lib/duration.dart: -------------------------------------------------------------------------------- 1 | /// Support for doing something awesome. 2 | /// 3 | /// More dartdocs go here. 4 | library duration; 5 | 6 | export 'src/duration.dart'; 7 | export 'src/milliseconds.dart'; 8 | export 'src/parse/parse.dart'; 9 | export 'src/tersity.dart'; 10 | export 'src/time_const.dart'; 11 | -------------------------------------------------------------------------------- /lib/locale.dart: -------------------------------------------------------------------------------- 1 | export 'src/locale/locale.dart'; 2 | -------------------------------------------------------------------------------- /lib/src/duration.dart: -------------------------------------------------------------------------------- 1 | import 'locale/locale.dart'; 2 | import 'tersity.dart'; 3 | 4 | /// Converts [duration] into legible string with given level of [tersity] 5 | /// 6 | /// Example: 7 | /// 8 | /// final dur = Duration( 9 | /// days: 5, 10 | /// hours: 23, 11 | /// minutes: 59, 12 | /// seconds: 59, 13 | /// milliseconds: 999, 14 | /// microseconds: 999); 15 | /// prettyDuration(dur) // => 5d 23h 59m 59s 999ms 999us 16 | /// 17 | /// Use [abbreviated] to determine if units should be abbreviated or not. 18 | /// 19 | /// Example: 20 | /// 21 | /// // => 5 days 9 hours 22 | /// prettyDuration(aDay * 5 + anHour * 9, abbreviated: false) 23 | /// 24 | /// Use [locale] to format according to the desired locale. 25 | /// 26 | /// Example: 27 | /// 28 | /// // => 5días 9horas 29 | /// printDuration(aDay * 5 + anHour * 9, 30 | /// abbreviated: false, locale: spanishLocale); 31 | String prettyDuration(Duration duration, 32 | {DurationTersity tersity = DurationTersity.second, 33 | DurationTersity upperTersity = DurationTersity.week, 34 | DurationLocale locale = const EnglishDurationLocale(), 35 | String? spacer, 36 | String? delimiter, 37 | String? conjunction, 38 | bool abbreviated = false, 39 | int maxUnits = 0}) { 40 | if (abbreviated && delimiter == null) { 41 | delimiter = ', '; 42 | spacer = ''; 43 | } else { 44 | delimiter ??= ' '; 45 | spacer ??= locale.defaultSpacer; 46 | } 47 | 48 | String sign = duration.isNegative ? '-' : ''; 49 | duration = duration.abs(); 50 | 51 | var out = []; 52 | 53 | for (final currentTersity in DurationTersity.values) { 54 | if (currentTersity > upperTersity) { 55 | continue; 56 | } else if (currentTersity < tersity) { 57 | break; 58 | } 59 | int value = duration.inUnit(currentTersity); 60 | if (currentTersity != upperTersity) { 61 | value %= currentTersity.mod; 62 | } 63 | if (value > 0) { 64 | out.add( 65 | '$value$spacer${locale.inUnit(currentTersity, value, abbreviated)}'); 66 | } else if (currentTersity == tersity && out.isEmpty) { 67 | out.add('0$spacer${locale.inUnit(currentTersity, value, abbreviated)}'); 68 | } 69 | } 70 | 71 | if (maxUnits > 0 && out.length > maxUnits) { 72 | out = out.sublist(0, maxUnits); 73 | } 74 | 75 | if (out.length == 1) { 76 | return sign + out.first; 77 | } else { 78 | if (conjunction == null || out.length == 1) { 79 | return sign + out.join(delimiter); 80 | } else { 81 | return sign + 82 | out.sublist(0, out.length - 1).join(delimiter) + 83 | conjunction + 84 | out.last; 85 | } 86 | } 87 | } 88 | 89 | extension PrettyDuration on Duration { 90 | String pretty( 91 | {DurationTersity tersity = DurationTersity.second, 92 | DurationTersity upperTersity = DurationTersity.week, 93 | DurationLocale locale = const EnglishDurationLocale(), 94 | String? spacer, 95 | String? delimiter, 96 | String? conjunction, 97 | bool abbreviated = false, 98 | int maxUnits = 0}) { 99 | return prettyDuration(this, 100 | tersity: tersity, 101 | upperTersity: upperTersity, 102 | locale: locale, 103 | spacer: spacer, 104 | delimiter: delimiter, 105 | conjunction: conjunction, 106 | abbreviated: abbreviated, 107 | maxUnits: maxUnits); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /lib/src/locale/arabic.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class ArabicDurationLocale extends DurationLocale { 4 | const ArabicDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'ع'; 10 | } else { 11 | if (amount == 1) { 12 | return 'عام'; 13 | } else if (amount == 2) { 14 | return 'عامين'; 15 | } else if (amount > 2 && amount < 11) { 16 | return 'أعوام'; 17 | } else if (amount > 10) { 18 | return 'عام'; 19 | } 20 | 21 | return 'أعوام'; 22 | } 23 | } 24 | 25 | @override 26 | String month(int amount, [bool abbreviated = true]) { 27 | if (abbreviated) { 28 | return 'ش'; 29 | } else { 30 | if (amount == 1) { 31 | return 'شهر'; 32 | } else if (amount == 2) { 33 | return 'شهرين'; 34 | } else if (amount > 2 && amount < 11) { 35 | return 'أشهر'; 36 | } else if (amount > 10) { 37 | return 'شهر'; 38 | } 39 | return 'شهور'; 40 | } 41 | } 42 | 43 | @override 44 | String week(int amount, [bool abbreviated = true]) { 45 | if (abbreviated) { 46 | return 'أ'; 47 | } else { 48 | if (amount == 1) { 49 | return 'أسبوع'; 50 | } else if (amount == 2) { 51 | return 'اسبوعين'; 52 | } else if (amount > 2 && amount < 11) { 53 | return 'اسابيع'; 54 | } else if (amount > 10) { 55 | return 'أسبوع'; 56 | } 57 | return 'اسابيع'; 58 | } 59 | } 60 | 61 | @override 62 | String day(int amount, [bool abbreviated = true]) { 63 | if (abbreviated) { 64 | return 'ي'; 65 | } else { 66 | if (amount == 1) { 67 | return 'يوم'; 68 | } else if (amount == 2) { 69 | return 'يومين'; 70 | } else if (amount > 2 && amount < 11) { 71 | return 'ايام'; 72 | } else if (amount > 10) { 73 | return 'يوم'; 74 | } 75 | return 'ايام'; 76 | } 77 | } 78 | 79 | @override 80 | String hour(int amount, [bool abbreviated = true]) { 81 | if (abbreviated) { 82 | return 'س'; 83 | } else { 84 | if (amount == 1) { 85 | return 'ساعة'; 86 | } else if (amount == 2) { 87 | return 'ساعتين'; 88 | } else if (amount > 2 && amount < 11) { 89 | return 'ساعات'; 90 | } else if (amount > 10) { 91 | return 'ساعة'; 92 | } 93 | return 'ساعات'; 94 | } 95 | } 96 | 97 | @override 98 | String minute(int amount, [bool abbreviated = true]) { 99 | if (abbreviated) { 100 | return 'د'; 101 | } else { 102 | if (amount == 1) { 103 | return 'دقيقة'; 104 | } else if (amount == 2) { 105 | return 'دقيقتين'; 106 | } else if (amount > 2 && amount < 11) { 107 | return 'دقائق'; 108 | } else if (amount > 10) { 109 | return 'دقيقة'; 110 | } 111 | return 'دقائق'; 112 | } 113 | } 114 | 115 | @override 116 | String second(int amount, [bool abbreviated = true]) { 117 | if (abbreviated) { 118 | return 'ث'; 119 | } else { 120 | if (amount == 1) { 121 | return 'ثانية'; 122 | } else if (amount == 2) { 123 | return 'ثانيتين'; 124 | } else if (amount > 2 && amount < 11) { 125 | return 'ثواني'; 126 | } else if (amount > 10) { 127 | return 'ثانية'; 128 | } 129 | return 'ثواني'; 130 | } 131 | } 132 | 133 | @override 134 | String millisecond(int amount, [bool abbreviated = true]) { 135 | if (abbreviated) { 136 | return 'م ث'; 137 | } else { 138 | if (amount == 1) { 139 | return 'ملّي ثانية'; 140 | } else if (amount == 2) { 141 | return 'ملّي ثانيتين'; 142 | } else if (amount > 2 && amount < 11) { 143 | return 'ملّي ثواني'; 144 | } else if (amount > 10) { 145 | return 'ملّي ثانية'; 146 | } 147 | return 'ملّي ثواني'; 148 | } 149 | } 150 | 151 | @override 152 | String microseconds(int amount, [bool abbreviated = true]) { 153 | if (abbreviated) { 154 | return 'ميكرو ث'; 155 | } else { 156 | if (amount == 1) { 157 | return 'ميكرو ثانية'; 158 | } else if (amount == 2) { 159 | return 'ميكرو ثانيتيتن'; 160 | } else if (amount > 2 && amount < 11) { 161 | return 'ميكرو ثواني'; 162 | } else if (amount > 10) { 163 | return 'ميكرو ثواني'; 164 | } 165 | return 'ميكرو ثواني'; 166 | } 167 | } 168 | } 169 | -------------------------------------------------------------------------------- /lib/src/locale/chinese_hans.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class ChineseSimplifiedDurationLocale extends DurationLocale { 4 | const ChineseSimplifiedDurationLocale(); 5 | 6 | @override 7 | String get defaultSpacer => ''; 8 | 9 | @override 10 | String year(int amount, [bool abbreviated = true]) { 11 | return '年'; 12 | } 13 | 14 | @override 15 | String month(int amount, [bool abbreviated = true]) { 16 | return '月'; 17 | } 18 | 19 | @override 20 | String week(int amount, [bool abbreviated = true]) { 21 | return '周'; 22 | } 23 | 24 | @override 25 | String day(int amount, [bool abbreviated = true]) { 26 | return '日'; 27 | } 28 | 29 | @override 30 | String hour(int amount, [bool abbreviated = true]) { 31 | return '小时'; 32 | } 33 | 34 | @override 35 | String minute(int amount, [bool abbreviated = true]) { 36 | return '分'; 37 | } 38 | 39 | @override 40 | String second(int amount, [bool abbreviated = true]) { 41 | return '秒'; 42 | } 43 | 44 | @override 45 | String millisecond(int amount, [bool abbreviated = true]) { 46 | return '毫秒'; 47 | } 48 | 49 | @override 50 | String microseconds(int amount, [bool abbreviated = true]) { 51 | return '微秒'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /lib/src/locale/chinese_hant.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class ChineseTraditionalDurationLocale extends DurationLocale { 4 | const ChineseTraditionalDurationLocale(); 5 | 6 | @override 7 | String get defaultSpacer => ''; 8 | 9 | @override 10 | String year(int amount, [bool abbreviated = true]) { 11 | return '年'; 12 | } 13 | 14 | @override 15 | String month(int amount, [bool abbreviated = true]) { 16 | return '月'; 17 | } 18 | 19 | @override 20 | String week(int amount, [bool abbreviated = true]) { 21 | return '週'; 22 | } 23 | 24 | @override 25 | String day(int amount, [bool abbreviated = true]) { 26 | return '日'; 27 | } 28 | 29 | @override 30 | String hour(int amount, [bool abbreviated = true]) { 31 | return '小時'; 32 | } 33 | 34 | @override 35 | String minute(int amount, [bool abbreviated = true]) { 36 | return '分鐘'; 37 | } 38 | 39 | @override 40 | String second(int amount, [bool abbreviated = true]) { 41 | return '秒'; 42 | } 43 | 44 | @override 45 | String millisecond(int amount, [bool abbreviated = true]) { 46 | return '毫秒'; 47 | } 48 | 49 | @override 50 | String microseconds(int amount, [bool abbreviated = true]) { 51 | return '微秒'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /lib/src/locale/czech.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | enum CzechDurationLocalePluralization { 4 | one, 5 | few, 6 | many, 7 | } 8 | 9 | class CzechDurationLocale extends DurationLocale { 10 | const CzechDurationLocale(); 11 | 12 | static CzechDurationLocalePluralization getPluralization(int amount) { 13 | if (amount == 1) { 14 | return CzechDurationLocalePluralization.one; 15 | } else if (amount > 1 && amount < 5) { 16 | return CzechDurationLocalePluralization.few; 17 | } else { 18 | return CzechDurationLocalePluralization.many; 19 | } 20 | } 21 | 22 | @override 23 | String year(int amount, [bool abbreviated = true]) { 24 | if (abbreviated) { 25 | return 'r'; 26 | } 27 | 28 | switch (CzechDurationLocale.getPluralization(amount)) { 29 | case CzechDurationLocalePluralization.one: 30 | return 'rok'; 31 | case CzechDurationLocalePluralization.few: 32 | return 'roky'; 33 | case CzechDurationLocalePluralization.many: 34 | default: 35 | return 'let'; 36 | } 37 | } 38 | 39 | @override 40 | String month(int amount, [bool abbreviated = true]) { 41 | if (abbreviated) { 42 | return 'měs.'; 43 | } 44 | 45 | switch (CzechDurationLocale.getPluralization(amount)) { 46 | case CzechDurationLocalePluralization.one: 47 | return 'měsíc'; 48 | case CzechDurationLocalePluralization.few: 49 | return 'měsíce'; 50 | case CzechDurationLocalePluralization.many: 51 | default: 52 | return 'měsíců'; 53 | } 54 | } 55 | 56 | @override 57 | String week(int amount, [bool abbreviated = true]) { 58 | if (abbreviated) { 59 | return 't'; 60 | } 61 | 62 | switch (CzechDurationLocale.getPluralization(amount)) { 63 | case CzechDurationLocalePluralization.one: 64 | return 'týden'; 65 | case CzechDurationLocalePluralization.few: 66 | return 'týdny'; 67 | case CzechDurationLocalePluralization.many: 68 | default: 69 | return 'týdnů'; 70 | } 71 | } 72 | 73 | @override 74 | String day(int amount, [bool abbreviated = true]) { 75 | if (abbreviated) { 76 | return 'd'; 77 | } 78 | 79 | switch (CzechDurationLocale.getPluralization(amount)) { 80 | case CzechDurationLocalePluralization.one: 81 | return 'den'; 82 | case CzechDurationLocalePluralization.few: 83 | return 'dny'; 84 | case CzechDurationLocalePluralization.many: 85 | default: 86 | return 'dnů'; 87 | } 88 | } 89 | 90 | @override 91 | String hour(int amount, [bool abbreviated = true]) { 92 | if (abbreviated) { 93 | return 'h'; 94 | } 95 | 96 | switch (CzechDurationLocale.getPluralization(amount)) { 97 | case CzechDurationLocalePluralization.one: 98 | return 'hodina'; 99 | case CzechDurationLocalePluralization.few: 100 | return 'hodiny'; 101 | case CzechDurationLocalePluralization.many: 102 | default: 103 | return 'hodin'; 104 | } 105 | } 106 | 107 | @override 108 | String minute(int amount, [bool abbreviated = true]) { 109 | if (abbreviated) { 110 | return 'm'; 111 | } 112 | 113 | switch (CzechDurationLocale.getPluralization(amount)) { 114 | case CzechDurationLocalePluralization.one: 115 | return 'minuta'; 116 | case CzechDurationLocalePluralization.few: 117 | return 'minuty'; 118 | case CzechDurationLocalePluralization.many: 119 | default: 120 | return 'minut'; 121 | } 122 | } 123 | 124 | @override 125 | String second(int amount, [bool abbreviated = true]) { 126 | if (abbreviated) { 127 | return 's'; 128 | } 129 | 130 | switch (CzechDurationLocale.getPluralization(amount)) { 131 | case CzechDurationLocalePluralization.one: 132 | return 'sekunda'; 133 | case CzechDurationLocalePluralization.few: 134 | return 'sekundy'; 135 | case CzechDurationLocalePluralization.many: 136 | default: 137 | return 'sekund'; 138 | } 139 | } 140 | 141 | @override 142 | String millisecond(int amount, [bool abbreviated = true]) { 143 | if (abbreviated) { 144 | return 'ms'; 145 | } 146 | 147 | switch (CzechDurationLocale.getPluralization(amount)) { 148 | case CzechDurationLocalePluralization.one: 149 | return 'milisekunda'; 150 | case CzechDurationLocalePluralization.few: 151 | return 'milisekundy'; 152 | case CzechDurationLocalePluralization.many: 153 | default: 154 | return 'milisekund'; 155 | } 156 | } 157 | 158 | @override 159 | String microseconds(int amount, [bool abbreviated = true]) { 160 | if (abbreviated) { 161 | return 'μs'; 162 | } 163 | 164 | switch (CzechDurationLocale.getPluralization(amount)) { 165 | case CzechDurationLocalePluralization.one: 166 | return 'mikrosekunda'; 167 | case CzechDurationLocalePluralization.few: 168 | return 'mikrosekundy'; 169 | case CzechDurationLocalePluralization.many: 170 | return 'mikrosekund'; 171 | } 172 | } 173 | } 174 | -------------------------------------------------------------------------------- /lib/src/locale/dutch.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class DutchDurationLocale extends DurationLocale { 4 | const DutchDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'j'; 10 | } else { 11 | return amount == 1 ? 'jaar' : 'jaren'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'm'; 19 | } else { 20 | return 'maand${amount != 1 ? 'en' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'w'; 28 | } else { 29 | return amount == 1 ? 'week' : 'weken'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'd'; 37 | } else { 38 | return 'dag${amount != 1 ? 'en' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'u'; 46 | } else { 47 | return amount == 1 ? 'uur' : 'uren'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'min'; 55 | } else { 56 | return amount == 1 ? 'minuut' : 'minuten'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'seconde${amount != 1 ? 'n' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'milliseconde${amount != 1 ? 'n' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'microseconde${amount != 1 ? 'n' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/english.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class EnglishDurationLocale extends DurationLocale { 4 | const EnglishDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'y'; 10 | } else { 11 | return 'year${amount.abs() != 1 ? 's' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'mon'; 19 | } else { 20 | return 'month${amount.abs() != 1 ? 's' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'w'; 28 | } else { 29 | return 'week${amount.abs() != 1 ? 's' : ''}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'd'; 37 | } else { 38 | return 'day${amount.abs() != 1 ? 's' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'hour${amount.abs() != 1 ? 's' : ''}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'min'; 55 | } else { 56 | return 'minute${amount.abs() != 1 ? 's' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'second${amount.abs() != 1 ? 's' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'millisecond${amount.abs() != 1 ? 's' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'microsecond${amount.abs() != 1 ? 's' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/finnish.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class FinnishDurationLocale extends DurationLocale { 4 | const FinnishDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'v'; 10 | } else { 11 | return 'vuosi${amount.abs() != 1 ? 'a' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'kk'; 19 | } else { 20 | return 'kuukausi${amount.abs() != 1 ? 'a' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'vko'; 28 | } else { 29 | return 'viikko${amount.abs() != 1 ? 'a' : ''}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'pvä'; 37 | } else { 38 | return 'päivä${amount.abs() != 1 ? 'ä' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 't'; 46 | } else { 47 | return 'tunti${amount.abs() != 1 ? 'a' : ''}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'min'; 55 | } else { 56 | return 'minuutti${amount.abs() != 1 ? 'a' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'sekunti${amount.abs() != 1 ? 'a' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'millisekunti${amount.abs() != 1 ? 'a' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'mikrosekunti${amount.abs() != 1 ? 'a' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/french.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class FrenchDurationLocale extends DurationLocale { 4 | const FrenchDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'an'; 10 | } else { 11 | return 'année${amount > 1 ? 's' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'mo'; 19 | } else { 20 | return 'mois'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'sem'; 28 | } else { 29 | return 'semaine${amount > 1 ? 's' : ''}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'j'; 37 | } else { 38 | return 'jour${amount > 1 ? 's' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'heure${amount > 1 ? 's' : ''}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'min'; 55 | } else { 56 | return 'minute${amount > 1 ? 's' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'seconde${amount > 1 ? 's' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'milliseconde${amount > 1 ? 's' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'microseconde${amount > 1 ? 's' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/german.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class GermanDurationLocale extends DurationLocale { 4 | const GermanDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'J'; 10 | } else { 11 | return 'Jahr${amount > 1 ? 'e' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'M'; 19 | } else { 20 | return 'Monat${amount > 1 ? 'e' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'W'; 28 | } else { 29 | return 'Woche${amount > 1 ? 'n' : ''}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'T'; 37 | } else { 38 | return 'Tag${amount > 1 ? 'e' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'Std'; 46 | } else { 47 | return 'Stunde${amount > 1 ? 'n' : ''}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'Min'; 55 | } else { 56 | return 'Minute${amount > 1 ? 'n' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 'Sek'; 64 | } else { 65 | return 'Sekunde${amount > 1 ? 'n' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'Ms'; 73 | } else { 74 | return 'Millisekunde${amount > 1 ? 'n' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'Us'; 82 | } else { 83 | return 'Mikrosekunde${amount > 1 ? 'n' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/greek.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class GreekDurationLocale extends DurationLocale { 4 | const GreekDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'χ'; 10 | } else { 11 | return 'χρόν' + (amount.abs() != 1 ? 'ια' : 'ος'); 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'μην'; 19 | } else { 20 | return 'μήν' + (amount.abs() != 1 ? 'ες' : 'ας'); 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'ε'; 28 | } else { 29 | return 'εβδομάδ' + (amount.abs() != 1 ? 'ες' : 'α'); 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'μ'; 37 | } else { 38 | return 'μέρ' + (amount.abs() != 1 ? 'ες' : 'α'); 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'ω'; 46 | } else { 47 | return 'ώρ' + (amount.abs() != 1 ? 'ες' : 'α'); 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'λεπ'; 55 | } else { 56 | return 'λεπτ' + (amount.abs() != 1 ? 'ά' : 'ό'); 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 'δ'; 64 | } else { 65 | return 'δευτερόλεπτ' + (amount.abs() != 1 ? 'α' : 'ο'); 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'χ'; 73 | } else { 74 | return 'χιλιοστ' + (amount.abs() != 1 ? 'ά' : 'ό'); 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'μ'; 82 | } else { 83 | return 'μικροδευτερόλεπτ' + (amount.abs() != 1 ? 'α' : 'ο'); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/hebrew.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class HebrewDurationLocale extends DurationLocale { 4 | const HebrewDurationLocale(); 5 | 6 | @override 7 | String day(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'י'; 10 | } else { 11 | return amount == 1 ? 'יום' : 'ימים'; 12 | } 13 | } 14 | 15 | @override 16 | String hour(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'ש'; 19 | } else { 20 | return amount == 1 ? 'שעה' : 'שעות'; 21 | } 22 | } 23 | 24 | @override 25 | String microseconds(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'µs'; 28 | } else { 29 | return amount == 1 ? 'מיקרו שניה' : 'מיקרו שניות'; 30 | } 31 | } 32 | 33 | @override 34 | String millisecond(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'ms'; 37 | } else { 38 | return amount == 1 ? 'מילי שניה' : 'מילי שניות'; 39 | } 40 | } 41 | 42 | @override 43 | String minute(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'דק.'; 46 | } else { 47 | return amount == 1 ? 'דקה' : 'דקות'; 48 | } 49 | } 50 | 51 | @override 52 | String month(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'ח'; 55 | } else { 56 | return amount == 1 ? 'חודש' : 'חודשיים'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 'שנ.'; 64 | } else { 65 | return amount == 1 ? 'שניה' : 'שניות'; 66 | } 67 | } 68 | 69 | @override 70 | String week(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'שב.'; 73 | } else { 74 | return amount == 1 ? 'שבוע' : 'שבועות'; 75 | } 76 | } 77 | 78 | @override 79 | String year(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'שנ.'; 82 | } else { 83 | return amount == 1 ? 'שנה' : 'שנים'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/indonesian.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class IndonesianDurationLocale extends DurationLocale { 4 | const IndonesianDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'thn'; 10 | } else { 11 | return 'tahun${amount > 1 ? '' : ''}'; //no pluralization 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'bln'; 19 | } else { 20 | return 'bulan${amount > 1 ? '' : ''}'; //no pluralization 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'mgg'; 28 | } else { 29 | return 'minggu${amount > 1 ? '' : ''}'; //no pluralization 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'hr'; 37 | } else { 38 | return 'hari${amount > 1 ? '' : ''}'; //no pluralization 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'j'; 46 | } else { 47 | return 'jam${amount > 1 ? '' : ''}'; //no pluralization 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'm'; 55 | } else { 56 | return 'menit${amount > 1 ? '' : ''}'; //no pluralization 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 'd'; 64 | } else { 65 | return 'detik${amount > 1 ? '' : ''}'; //no pluralization 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'milidetik'; 73 | } else { 74 | return 'milidetik${amount > 1 ? '' : ''}'; //no pluralization 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'mikrodetik'; 82 | } else { 83 | return 'mikrodetik${amount > 1 ? 's' : ''}'; //no pluralization 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/italian.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class ItalianDurationLocale extends DurationLocale { 4 | const ItalianDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'a'; 10 | } else { 11 | return 'ann${(amount == 0 || amount > 1) ? 'i' : 'o'}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'm'; 19 | } else { 20 | return 'mes${(amount == 0 || amount > 1) ? 'i' : 'e'}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'set'; 28 | } else { 29 | return 'settiman${(amount == 0 || amount > 1) ? 'e' : 'a'}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'g'; 37 | } else { 38 | return 'giorn${(amount == 0 || amount > 1) ? 'i' : 'o'}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'or${(amount == 0 || amount > 1) ? 'e' : 'a'}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'm'; 55 | } else { 56 | return 'minut${(amount == 0 || amount > 1) ? 'i' : 'o'}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'second${(amount == 0 || amount > 1) ? 'i' : 'o'}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'millisecond${(amount == 0 || amount > 1) ? 'i' : 'o'}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'μs'; 82 | } else { 83 | return 'microsecond${(amount == 0 || amount > 1) ? 'i' : 'o'}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/japanese.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class JapaneseDurationLocale extends DurationLocale { 4 | const JapaneseDurationLocale(); 5 | 6 | @override 7 | String get defaultSpacer => ''; 8 | 9 | @override 10 | String year(int amount, [bool abbreviated = true]) { 11 | return '年'; 12 | } 13 | 14 | @override 15 | String month(int amount, [bool abbreviated = true]) { 16 | return '月'; 17 | } 18 | 19 | @override 20 | String week(int amount, [bool abbreviated = true]) { 21 | return '週'; 22 | } 23 | 24 | @override 25 | String day(int amount, [bool abbreviated = true]) { 26 | return '日'; 27 | } 28 | 29 | @override 30 | String hour(int amount, [bool abbreviated = true]) { 31 | return '時間'; 32 | } 33 | 34 | @override 35 | String minute(int amount, [bool abbreviated = true]) { 36 | return '分'; 37 | } 38 | 39 | @override 40 | String second(int amount, [bool abbreviated = true]) { 41 | return '秒'; 42 | } 43 | 44 | @override 45 | String millisecond(int amount, [bool abbreviated = true]) { 46 | return 'ミリ秒'; 47 | } 48 | 49 | @override 50 | String microseconds(int amount, [bool abbreviated = true]) { 51 | return 'マイクロ秒'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /lib/src/locale/korean.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class KoreanDurationLocale extends DurationLocale { 4 | const KoreanDurationLocale(); 5 | 6 | @override 7 | String get defaultSpacer => ''; 8 | 9 | @override 10 | String year(int amount, [bool abbreviated = true]) { 11 | return '년'; 12 | } 13 | 14 | @override 15 | String month(int amount, [bool abbreviated = true]) { 16 | return '월'; 17 | } 18 | 19 | @override 20 | String week(int amount, [bool abbreviated = true]) { 21 | return '주'; 22 | } 23 | 24 | @override 25 | String day(int amount, [bool abbreviated = true]) { 26 | return '일'; 27 | } 28 | 29 | @override 30 | String hour(int amount, [bool abbreviated = true]) { 31 | return '시간'; 32 | } 33 | 34 | @override 35 | String minute(int amount, [bool abbreviated = true]) { 36 | return '분'; 37 | } 38 | 39 | @override 40 | String second(int amount, [bool abbreviated = true]) { 41 | return '초'; 42 | } 43 | 44 | @override 45 | String millisecond(int amount, [bool abbreviated = true]) { 46 | return '밀리초'; 47 | } 48 | 49 | @override 50 | String microseconds(int amount, [bool abbreviated = true]) { 51 | return '마이크로초'; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /lib/src/locale/locale.dart: -------------------------------------------------------------------------------- 1 | library duration.locale; 2 | 3 | import 'package:duration/duration.dart'; 4 | 5 | part 'english.dart'; 6 | part 'french.dart'; 7 | part 'greek.dart'; 8 | part 'hebrew.dart'; 9 | part 'polish.dart'; 10 | part 'portuguese_br.dart'; 11 | part 'russian.dart'; 12 | part 'spanish.dart'; 13 | part 'swedish.dart'; 14 | part 'norwegian.dart'; 15 | part 'turkish.dart'; 16 | part 'italian.dart'; 17 | part 'german.dart'; 18 | part 'dutch.dart'; 19 | part 'chinese_hans.dart'; 20 | part 'chinese_hant.dart'; 21 | part 'thai.dart'; 22 | part 'romanian.dart'; 23 | part 'arabic.dart'; 24 | part 'korean.dart'; 25 | part 'indonesian.dart'; 26 | part 'czech.dart'; 27 | part 'finnish.dart'; 28 | part 'japanese.dart'; 29 | part 'ukrainian.dart'; 30 | part 'vietnamese.dart'; 31 | 32 | /// Interface to print time units for different locale 33 | abstract class DurationLocale { 34 | const DurationLocale(); 35 | 36 | String get defaultSpacer => ' '; 37 | 38 | /// Print [amount] years for the corresponding locale. The unit is abbreviated 39 | /// if [abbreviated] is set to true. 40 | String year(int amount, [bool abbreviated = true]); 41 | 42 | /// Print [amount] month for the corresponding locale. The unit is abbreviated 43 | /// if [abbreviated] is set to true. 44 | String month(int amount, [bool abbreviated = true]); 45 | 46 | /// Print [amount] week for the corresponding locale. The unit is abbreviated 47 | /// if [abbreviated] is set to true. 48 | String week(int amount, [bool abbreviated = true]); 49 | 50 | /// Print [amount] day for the corresponding locale. The unit is abbreviated 51 | /// if [abbreviated] is set to true. 52 | String day(int amount, [bool abbreviated = true]); 53 | 54 | /// Print [amount] hour for the corresponding locale. The unit is abbreviated 55 | /// if [abbreviated] is set to true. 56 | String hour(int amount, [bool abbreviated = true]); 57 | 58 | /// Print [amount] minute for the corresponding locale. The unit is abbreviated 59 | /// if [abbreviated] is set to true. 60 | String minute(int amount, [bool abbreviated = true]); 61 | 62 | /// Print [amount] second for the corresponding locale. The unit is abbreviated 63 | /// if [abbreviated] is set to true. 64 | String second(int amount, [bool abbreviated = true]); 65 | 66 | /// Print [amount] millisecond for the corresponding locale. The unit is abbreviated 67 | /// if [abbreviated] is set to true. 68 | String millisecond(int amount, [bool abbreviated = true]); 69 | 70 | /// Print [amount] microseconds for the corresponding locale. The unit is 71 | /// abbreviated if [abbreviated] is set to true. 72 | String microseconds(int amount, [bool abbreviated = true]); 73 | 74 | String inUnit(DurationTersity unit, int amount, [bool abbreviated = true]) { 75 | switch (unit) { 76 | case DurationTersity.week: 77 | return week(amount, abbreviated); 78 | case DurationTersity.day: 79 | return day(amount, abbreviated); 80 | case DurationTersity.hour: 81 | return hour(amount, abbreviated); 82 | case DurationTersity.minute: 83 | return minute(amount, abbreviated); 84 | case DurationTersity.second: 85 | return second(amount, abbreviated); 86 | case DurationTersity.millisecond: 87 | return millisecond(amount, abbreviated); 88 | case DurationTersity.microsecond: 89 | return microseconds(amount, abbreviated); 90 | default: 91 | throw UnsupportedError('unsupported duration unit: $unit'); 92 | } 93 | } 94 | 95 | static DurationLocale? fromLanguageCode(String languageCode) { 96 | return _locales[languageCode]; 97 | } 98 | } 99 | 100 | /// [DurationLocale] for English language 101 | const EnglishDurationLocale englishLocale = EnglishDurationLocale(); 102 | 103 | /// [DurationLocale] for French language 104 | const FrenchDurationLocale frenchLocale = FrenchDurationLocale(); 105 | 106 | /// [DurationLocale] for Greek language 107 | const GreekDurationLocale greekLocale = GreekDurationLocale(); 108 | 109 | /// [DurationLocale] for Hebrew language 110 | const HebrewDurationLocale hebrewLocale = HebrewDurationLocale(); 111 | 112 | /// [DurationLocale] for Polish language 113 | const PolishDurationLocale polishLocale = PolishDurationLocale(); 114 | 115 | /// [DurationLocale] for Portuguese language 116 | const PortugueseBRDurationLanguage portugueseBrLocale = 117 | PortugueseBRDurationLanguage(); 118 | 119 | /// [DurationLocale] for Russian language 120 | const RussianDurationLanguage russianLocale = RussianDurationLanguage(); 121 | 122 | /// [DurationLocale] for Spanish language 123 | const SpanishDurationLanguage spanishLocale = SpanishDurationLanguage(); 124 | 125 | /// [DurationLocale] for Swedish language 126 | const SwedishDurationLanguage swedishLocale = SwedishDurationLanguage(); 127 | 128 | /// [DurationLocale] for Norwegian language 129 | const NorwegianDurationLanguage norwegianLocale = NorwegianDurationLanguage(); 130 | 131 | /// [DurationLocale] for Turkish language 132 | const TurkishDurationLocale turkishLocale = TurkishDurationLocale(); 133 | 134 | /// [DurationLocale] for Italian language 135 | const ItalianDurationLocale italianLocale = ItalianDurationLocale(); 136 | 137 | /// [DurationLocale] for German language 138 | const GermanDurationLocale germanLocale = GermanDurationLocale(); 139 | 140 | /// [DurationLocale] for Dutch language 141 | const DutchDurationLocale dutchLocale = DutchDurationLocale(); 142 | 143 | /// [DurationLocale] for Chinese (Simplified) language 144 | const ChineseSimplifiedDurationLocale chineseSimplifiedDurationLocale = 145 | ChineseSimplifiedDurationLocale(); 146 | 147 | ChineseSimplifiedDurationLocale get chineseDurationLocale => 148 | chineseSimplifiedDurationLocale; 149 | 150 | /// [DurationLocale] for Chinese (Traditional) language 151 | const ChineseTraditionalDurationLocale chineseTraditionalLocale = 152 | ChineseTraditionalDurationLocale(); 153 | 154 | /// [DurationLocale] for Thai language 155 | const ThaiDurationLocale thaiLocale = ThaiDurationLocale(); 156 | 157 | /// [DurationLocale] for Indonesian language 158 | const IndonesianDurationLocale indonesianLocale = IndonesianDurationLocale(); 159 | 160 | /// [DurationLocale] for Korean language 161 | const KoreanDurationLocale koreanLocale = KoreanDurationLocale(); 162 | 163 | /// [DurationLocale] for Romanian language 164 | const RomanianDurationLocale romanianLocale = RomanianDurationLocale(); 165 | 166 | /// [DurationLocale] for Arabic language 167 | const ArabicDurationLocale arabicLocale = ArabicDurationLocale(); 168 | 169 | /// [DurationLocale] for Czech language 170 | const CzechDurationLocale czechLocale = CzechDurationLocale(); 171 | 172 | /// [DurationLocale] for Finnish language 173 | const FinnishDurationLocale finnishLocale = FinnishDurationLocale(); 174 | 175 | /// [DurationLocale] for Japanese language 176 | const JapaneseDurationLocale japaneseLocale = JapaneseDurationLocale(); 177 | 178 | ///[DurationLocale] for Ukrainian language 179 | const UkrainianDurationLocale ukrainianLocale = UkrainianDurationLocale(); 180 | 181 | ///[DurationLocale] for Vietnamese language 182 | const VietnameseDurationLocale vietnameseLocale = VietnameseDurationLocale(); 183 | 184 | const _locales = { 185 | 'el': greekLocale, 186 | 'en': englishLocale, 187 | 'fr': frenchLocale, 188 | 'he': hebrewLocale, 189 | 'pl': polishLocale, 190 | 'pt': portugueseBrLocale, 191 | 'ru': russianLocale, 192 | 'es': spanishLocale, 193 | 'sv': swedishLocale, 194 | 'nb': norwegianLocale, 195 | 'tr': turkishLocale, 196 | 'it': italianLocale, 197 | 'de': germanLocale, 198 | 'nl': dutchLocale, 199 | 'zh': chineseSimplifiedDurationLocale, 200 | 'zh_Hant': chineseTraditionalLocale, 201 | 'th': thaiLocale, 202 | 'id': indonesianLocale, 203 | 'ko': koreanLocale, 204 | 'ro': romanianLocale, 205 | 'ar': arabicLocale, 206 | 'cs': czechLocale, 207 | 'fi': finnishLocale, 208 | 'ja': japaneseLocale, 209 | 'uk': ukrainianLocale, 210 | 'vi': vietnameseLocale, 211 | }; 212 | -------------------------------------------------------------------------------- /lib/src/locale/norwegian.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class NorwegianDurationLanguage extends DurationLocale { 4 | const NorwegianDurationLanguage(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | // No need to abbreviate a 2 letter word. It's also the same in singular and plural. 9 | return 'år'; 10 | } 11 | 12 | @override 13 | String month(int amount, [bool abbreviated = true]) { 14 | if (abbreviated) { 15 | return 'mnd'; 16 | } else { 17 | return 'måned${amount > 1 ? 'er' : ''}'; 18 | } 19 | } 20 | 21 | @override 22 | String week(int amount, [bool abbreviated = true]) { 23 | if (abbreviated) { 24 | return 'u'; 25 | } else { 26 | return 'uke${amount > 1 ? 'r' : ''}'; 27 | } 28 | } 29 | 30 | @override 31 | String day(int amount, [bool abbreviated = true]) { 32 | if (abbreviated) { 33 | return 'd'; 34 | } else { 35 | return 'dag${amount > 1 ? 'er' : ''}'; 36 | } 37 | } 38 | 39 | @override 40 | String hour(int amount, [bool abbreviated = true]) { 41 | if (abbreviated) { 42 | return 't'; 43 | } else { 44 | return 'time${amount > 1 ? 'r' : ''}'; 45 | } 46 | } 47 | 48 | @override 49 | String minute(int amount, [bool abbreviated = true]) { 50 | if (abbreviated) { 51 | return 'm'; 52 | } else { 53 | return 'minutt${amount > 1 ? 'er' : ''}'; 54 | } 55 | } 56 | 57 | @override 58 | String second(int amount, [bool abbreviated = true]) { 59 | if (abbreviated) { 60 | return 's'; 61 | } else { 62 | return 'sekund${amount > 1 ? 'er' : ''}'; 63 | } 64 | } 65 | 66 | @override 67 | String millisecond(int amount, [bool abbreviated = true]) { 68 | if (abbreviated) { 69 | return 'ms'; 70 | } else { 71 | return 'millisekund${amount > 1 ? 'er' : ''}'; 72 | } 73 | } 74 | 75 | @override 76 | String microseconds(int amount, [bool abbreviated = true]) { 77 | if (abbreviated) { 78 | return 'us'; 79 | } else { 80 | return 'mikrosekund${amount > 1 ? 'er' : ''}'; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /lib/src/locale/polish.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class PolishDurationLocale extends DurationLocale { 4 | const PolishDurationLocale(); 5 | 6 | /// Based on https://github.com/unicode-org/cldr/blob/master/common/supplemental/plurals.xml 7 | /// 'other' case is skipped as it doesn't affect the duration strings. 8 | String _polishStr(int amount, String one, String few, String many) { 9 | if (amount == 1) return one; 10 | 11 | final int lastDigit = amount % 10; 12 | final int lastTwoDigits = amount % 100; 13 | if (lastDigit >= 2 && 14 | lastDigit <= 4 && 15 | !(lastTwoDigits >= 12 && lastTwoDigits <= 14)) return few; 16 | 17 | return many; 18 | } 19 | 20 | @override 21 | String year(int amount, [bool abbreviated = true]) { 22 | if (abbreviated) { 23 | return _polishStr(amount, 'r', 'l', 'l'); 24 | } else { 25 | return _polishStr(amount, 'rok', 'lata', 'lat'); 26 | } 27 | } 28 | 29 | @override 30 | String month(int amount, [bool abbreviated = true]) { 31 | if (abbreviated) { 32 | return 'mc'; 33 | } else { 34 | return _polishStr(amount, 'miesiąc', 'miesiące', 'miesięcy'); 35 | } 36 | } 37 | 38 | @override 39 | String week(int amount, [bool abbreviated = true]) { 40 | if (abbreviated) { 41 | return 't'; 42 | } else { 43 | return _polishStr(amount, 'tydzień', 'tygodnie', 'tygodni'); 44 | } 45 | } 46 | 47 | @override 48 | String day(int amount, [bool abbreviated = true]) { 49 | if (abbreviated) { 50 | return 'd'; 51 | } else { 52 | return _polishStr(amount, 'dzień', 'dni', 'dni'); 53 | } 54 | } 55 | 56 | @override 57 | String hour(int amount, [bool abbreviated = true]) { 58 | if (abbreviated) { 59 | return 'h'; 60 | } else { 61 | return _polishStr(amount, 'godzina', 'godziny', 'godzin'); 62 | } 63 | } 64 | 65 | @override 66 | String minute(int amount, [bool abbreviated = true]) { 67 | if (abbreviated) { 68 | return 'm'; 69 | } else { 70 | return _polishStr(amount, 'minuta', 'minuty', 'minut'); 71 | } 72 | } 73 | 74 | @override 75 | String second(int amount, [bool abbreviated = true]) { 76 | if (abbreviated) { 77 | return 's'; 78 | } else { 79 | return _polishStr(amount, 'sekunda', 'sekundy', 'sekund'); 80 | } 81 | } 82 | 83 | @override 84 | String millisecond(int amount, [bool abbreviated = true]) { 85 | if (abbreviated) { 86 | return 'ms'; 87 | } else { 88 | return _polishStr(amount, 'milisekunda', 'milisekundy', 'milisekund'); 89 | } 90 | } 91 | 92 | @override 93 | String microseconds(int amount, [bool abbreviated = true]) { 94 | if (abbreviated) { 95 | return 'us'; 96 | } else { 97 | return _polishStr(amount, 'mikrosekunda', 'mikrosekundy', 'mikrosekund'); 98 | } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /lib/src/locale/portuguese_br.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class PortugueseBRDurationLanguage extends DurationLocale { 4 | const PortugueseBRDurationLanguage(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'a'; 10 | } else { 11 | return 'ano${amount > 1 ? 's' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'mês'; 19 | } else { 20 | return 'mês${amount > 1 ? 'es' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'sem'; 28 | } else { 29 | return 'semana${amount > 1 ? 's' : ''}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'd'; 37 | } else { 38 | return 'día${amount > 1 ? 's' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'hora${amount > 1 ? 's' : ''}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'm'; 55 | } else { 56 | return 'minuto${amount > 1 ? 's' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'segundo${amount > 1 ? 's' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'milisegundo${amount > 1 ? 's' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'microsegundo${amount > 1 ? 's' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/romanian.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class RomanianDurationLocale extends DurationLocale { 4 | const RomanianDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'a'; 10 | } else { 11 | return 'an${amount > 1 ? 'i' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'l'; 19 | } else { 20 | return 'lun${amount > 1 ? 'i' : 'ă'}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 's'; 28 | } else { 29 | return 'săptămân${amount > 1 ? 'i' : 'ă'}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'z'; 37 | } else { 38 | return 'zi${amount > 1 ? 'le' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'or${amount > 1 ? 'e' : 'ă'}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'm'; 55 | } else { 56 | return 'minut${amount > 1 ? 'e' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'secund${amount > 1 ? 'e' : 'ă'}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'millisecund${amount > 1 ? 'e' : 'ă'}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'microsecund${amount > 1 ? 'e' : 'ă'}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/russian.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class RussianDurationLanguage extends DurationLocale { 4 | const RussianDurationLanguage(); 5 | 6 | String _russianStr(int amount, String first, String second, String third) { 7 | final int lastDigit = amount % 10; 8 | if (lastDigit == 0 || 9 | (amount >= 11 && amount <= 14) || 10 | amount == 111 || 11 | amount == 913) return third; 12 | if (lastDigit == 1) return first; 13 | if (lastDigit <= 4) return second; 14 | return third; 15 | } 16 | 17 | @override 18 | String year(int amount, [bool abbreviated = true]) { 19 | if (abbreviated) { 20 | return 'г'; 21 | } else { 22 | return _russianStr(amount, 'год', 'года', 'лет'); 23 | } 24 | } 25 | 26 | @override 27 | String month(int amount, [bool abbreviated = true]) { 28 | if (abbreviated) { 29 | return 'мес'; 30 | } else { 31 | return _russianStr(amount, 'месяц', 'месяца', 'месяцев'); 32 | } 33 | } 34 | 35 | @override 36 | String week(int amount, [bool abbreviated = true]) { 37 | if (abbreviated) { 38 | return 'нед'; 39 | } else { 40 | return _russianStr(amount, 'неделя', 'недели', 'недель'); 41 | } 42 | } 43 | 44 | @override 45 | String day(int amount, [bool abbreviated = true]) { 46 | if (abbreviated) { 47 | return 'д'; 48 | } else { 49 | return _russianStr(amount, 'день', 'дня', 'дней'); 50 | } 51 | } 52 | 53 | @override 54 | String hour(int amount, [bool abbreviated = true]) { 55 | if (abbreviated) { 56 | return 'ч'; 57 | } else { 58 | return _russianStr(amount, 'час', 'часа', 'часов'); 59 | } 60 | } 61 | 62 | @override 63 | String minute(int amount, [bool abbreviated = true]) { 64 | if (abbreviated) { 65 | return 'м'; 66 | } else { 67 | return _russianStr(amount, 'минута', 'минуты', 'минут'); 68 | } 69 | } 70 | 71 | @override 72 | String second(int amount, [bool abbreviated = true]) { 73 | if (abbreviated) { 74 | return 'c'; 75 | } else { 76 | return _russianStr(amount, 'секунда', 'секунды', 'секунд'); 77 | } 78 | } 79 | 80 | @override 81 | String millisecond(int amount, [bool abbreviated = true]) { 82 | if (abbreviated) { 83 | return 'мс'; 84 | } else { 85 | return _russianStr(amount, 'миллисекунда', 'миллисекунды', 'миллисекунд'); 86 | } 87 | } 88 | 89 | @override 90 | String microseconds(int amount, [bool abbreviated = true]) { 91 | if (abbreviated) { 92 | return 'мкс'; 93 | } else { 94 | return _russianStr(amount, 'микросекунда', 'микросекунды', 'микросекунд'); 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /lib/src/locale/spanish.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class SpanishDurationLanguage extends DurationLocale { 4 | const SpanishDurationLanguage(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'y'; 10 | } else { 11 | return 'año${amount > 1 ? 's' : ''}'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'mon'; 19 | } else { 20 | return 'mes${amount > 1 ? 's' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'w'; 28 | } else { 29 | return 'semana${amount > 1 ? 's' : ''}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'd'; 37 | } else { 38 | return 'día${amount > 1 ? 's' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'hora${amount > 1 ? 's' : ''}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'm'; 55 | } else { 56 | return 'minuto${amount > 1 ? 's' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'segundo${amount > 1 ? 's' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'milisegundo${amount > 1 ? 's' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'microsegundo${amount > 1 ? 's' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/swedish.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class SwedishDurationLanguage extends DurationLocale { 4 | const SwedishDurationLanguage(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'y'; 10 | } else { 11 | return 'år'; 12 | } 13 | } 14 | 15 | @override 16 | String month(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 'mon'; 19 | } else { 20 | return 'månad${amount > 1 ? 'er' : ''}'; 21 | } 22 | } 23 | 24 | @override 25 | String week(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'w'; 28 | } else { 29 | return 'veck${amount > 1 ? 'or' : 'a'}'; 30 | } 31 | } 32 | 33 | @override 34 | String day(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'd'; 37 | } else { 38 | return 'dag${amount > 1 ? 'ar' : ''}'; 39 | } 40 | } 41 | 42 | @override 43 | String hour(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'h'; 46 | } else { 47 | return 'timm${amount > 1 ? 'ar' : 'e'}'; 48 | } 49 | } 50 | 51 | @override 52 | String minute(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'm'; 55 | } else { 56 | return 'minut${amount > 1 ? 'er' : ''}'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'sekund${amount > 1 ? 'er' : ''}'; 66 | } 67 | } 68 | 69 | @override 70 | String millisecond(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'ms'; 73 | } else { 74 | return 'millisekund${amount > 1 ? 'er' : ''}'; 75 | } 76 | } 77 | 78 | @override 79 | String microseconds(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'us'; 82 | } else { 83 | return 'mikrosekund${amount > 1 ? 'er' : ''}'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/thai.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class ThaiDurationLocale extends DurationLocale { 4 | const ThaiDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | return 'ปี'; 9 | } 10 | 11 | @override 12 | String month(int amount, [bool abbreviated = true]) { 13 | return 'เดือน'; 14 | } 15 | 16 | @override 17 | String week(int amount, [bool abbreviated = true]) { 18 | return 'สัปดาห์'; 19 | } 20 | 21 | @override 22 | String day(int amount, [bool abbreviated = true]) { 23 | return 'วัน'; 24 | } 25 | 26 | @override 27 | String hour(int amount, [bool abbreviated = true]) { 28 | if (abbreviated) { 29 | return 'ชม.'; 30 | } else { 31 | return 'ชั่วโมง'; 32 | } 33 | } 34 | 35 | @override 36 | String minute(int amount, [bool abbreviated = true]) { 37 | if (abbreviated) { 38 | return 'น.'; 39 | } else { 40 | return 'นาที'; 41 | } 42 | } 43 | 44 | @override 45 | String second(int amount, [bool abbreviated = true]) { 46 | return 'วินาที'; 47 | } 48 | 49 | @override 50 | String millisecond(int amount, [bool abbreviated = true]) { 51 | return 'มิลลิวินาที'; 52 | } 53 | 54 | @override 55 | String microseconds(int amount, [bool abbreviated = true]) { 56 | return 'ไมโครวินาที'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /lib/src/locale/turkish.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class TurkishDurationLocale extends DurationLocale { 4 | const TurkishDurationLocale(); 5 | 6 | @override 7 | String day(int amount, [bool abbreviated = true]) { 8 | if (abbreviated) { 9 | return 'g'; 10 | } else { 11 | return 'gün'; 12 | } 13 | } 14 | 15 | @override 16 | String hour(int amount, [bool abbreviated = true]) { 17 | if (abbreviated) { 18 | return 's'; 19 | } else { 20 | return 'saat'; 21 | } 22 | } 23 | 24 | @override 25 | String microseconds(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'µs'; 28 | } else { 29 | return 'mikrosaniye'; 30 | } 31 | } 32 | 33 | @override 34 | String millisecond(int amount, [bool abbreviated = true]) { 35 | if (abbreviated) { 36 | return 'ms'; 37 | } else { 38 | return 'milisaniye'; 39 | } 40 | } 41 | 42 | @override 43 | String minute(int amount, [bool abbreviated = true]) { 44 | if (abbreviated) { 45 | return 'd'; 46 | } else { 47 | return 'dakika'; 48 | } 49 | } 50 | 51 | @override 52 | String month(int amount, [bool abbreviated = true]) { 53 | if (abbreviated) { 54 | return 'a'; 55 | } else { 56 | return 'ay'; 57 | } 58 | } 59 | 60 | @override 61 | String second(int amount, [bool abbreviated = true]) { 62 | if (abbreviated) { 63 | return 's'; 64 | } else { 65 | return 'saniye'; 66 | } 67 | } 68 | 69 | @override 70 | String week(int amount, [bool abbreviated = true]) { 71 | if (abbreviated) { 72 | return 'h'; 73 | } else { 74 | return 'hafta'; 75 | } 76 | } 77 | 78 | @override 79 | String year(int amount, [bool abbreviated = true]) { 80 | if (abbreviated) { 81 | return 'y'; 82 | } else { 83 | return 'yıl'; 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /lib/src/locale/ukrainian.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class UkrainianDurationLocale extends DurationLocale { 4 | const UkrainianDurationLocale(); 5 | 6 | String _pickNoun(int amount, String nominativeSingle, String genitiveSingle, 7 | String genitivePlural) { 8 | final lastTwoDigits = amount % 100; 9 | final lastDigit = amount % 10; 10 | 11 | if (lastTwoDigits >= 11 && lastTwoDigits <= 20) { 12 | return genitivePlural; 13 | } 14 | 15 | if (lastDigit == 1) { 16 | return nominativeSingle; 17 | } else if (lastDigit >= 2 && lastDigit <= 4) { 18 | return genitiveSingle; 19 | } else { 20 | return genitivePlural; 21 | } 22 | } 23 | 24 | @override 25 | String year(int amount, [bool abbreviated = true]) { 26 | if (abbreviated) { 27 | return 'р'; 28 | } 29 | return _pickNoun(amount, 'рік', 'роки', 'років'); 30 | } 31 | 32 | @override 33 | String month(int amount, [bool abbreviated = true]) { 34 | if (abbreviated) { 35 | return 'міс'; 36 | } 37 | return _pickNoun(amount, 'місяць', 'місяці', 'місяців'); 38 | } 39 | 40 | @override 41 | String week(int amount, [bool abbreviated = true]) { 42 | if (abbreviated) { 43 | return 'тижд'; 44 | } 45 | return _pickNoun(amount, 'тиждень', 'тижні', 'тижнів'); 46 | } 47 | 48 | @override 49 | String day(int amount, [bool abbreviated = true]) { 50 | if (abbreviated) { 51 | return 'д'; 52 | } 53 | return _pickNoun(amount, 'день', 'дні', 'днів'); 54 | } 55 | 56 | @override 57 | String hour(int amount, [bool abbreviated = true]) { 58 | if (abbreviated) { 59 | return 'год'; 60 | } 61 | return _pickNoun(amount, 'година', 'години', 'годин'); 62 | } 63 | 64 | @override 65 | String minute(int amount, [bool abbreviated = true]) { 66 | if (abbreviated) { 67 | return 'хв'; 68 | } 69 | return _pickNoun(amount, 'хвилина', 'хвилини', 'хвилин'); 70 | } 71 | 72 | @override 73 | String second(int amount, [bool abbreviated = true]) { 74 | if (abbreviated) { 75 | return 'с'; 76 | } 77 | return _pickNoun(amount, 'секунда', 'секунди', 'секунд'); 78 | } 79 | 80 | @override 81 | String millisecond(int amount, [bool abbreviated = true]) { 82 | if (abbreviated) { 83 | return 'мс'; 84 | } 85 | return _pickNoun(amount, 'мілісекунда', 'мілісекунди', 'мілісекунд'); 86 | } 87 | 88 | @override 89 | String microseconds(int amount, [bool abbreviated = true]) { 90 | if (abbreviated) { 91 | return 'мкс'; 92 | } 93 | return _pickNoun(amount, 'мікросекунда', 'мікросекунди', 'мікросекунд'); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /lib/src/locale/vietnamese.dart: -------------------------------------------------------------------------------- 1 | part of duration.locale; 2 | 3 | class VietnameseDurationLocale extends DurationLocale { 4 | const VietnameseDurationLocale(); 5 | 6 | @override 7 | String year(int amount, [bool abbreviated = true]) { 8 | return 'năm'; 9 | } 10 | 11 | @override 12 | String month(int amount, [bool abbreviated = true]) { 13 | return 'tháng'; 14 | } 15 | 16 | @override 17 | String week(int amount, [bool abbreviated = true]) { 18 | return 'tuần'; 19 | } 20 | 21 | @override 22 | String day(int amount, [bool abbreviated = true]) { 23 | return 'ngày'; 24 | } 25 | 26 | @override 27 | String hour(int amount, [bool abbreviated = true]) { 28 | return 'giờ'; 29 | } 30 | 31 | @override 32 | String minute(int amount, [bool abbreviated = true]) { 33 | return 'phút'; 34 | } 35 | 36 | @override 37 | String second(int amount, [bool abbreviated = true]) { 38 | return 'giây'; 39 | } 40 | 41 | @override 42 | String millisecond(int amount, [bool abbreviated = true]) { 43 | return 'mili giây'; 44 | } 45 | 46 | @override 47 | String microseconds(int amount, [bool abbreviated = true]) { 48 | return 'micro giây'; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/src/milliseconds.dart: -------------------------------------------------------------------------------- 1 | import 'locale/locale.dart'; 2 | 3 | /// Pretty format [duration] in terms of milliseconds. 4 | /// 5 | /// If [terse] is true, microseconds are ignored. 6 | /// Use [language] to configure which locale to print for. 7 | /// Use [abbreviated] to control if the units should be abbreviated. 8 | String prettyMilliseconds(Duration duration, 9 | {bool terse = false, 10 | DurationLocale language = const EnglishDurationLocale(), 11 | String separator = ' ', 12 | bool abbreviated = false}) { 13 | if (duration.inMilliseconds > 0) { 14 | final int us = duration.inMicroseconds % 1000; 15 | if (us == 0 || terse) { 16 | // If no microseconds are present, print "xxxxx ms" 17 | final sb = StringBuffer(); 18 | sb.write(duration.inMilliseconds); 19 | sb.write(separator); 20 | sb.write(language.millisecond(duration.inMilliseconds, abbreviated)); 21 | return sb.toString(); 22 | } else { 23 | // If microseconds are present, print "xxx.yy ms" 24 | final sb = StringBuffer(); 25 | sb.write(duration.inMilliseconds); 26 | sb.write('.'); 27 | sb.write(_digits(us, 3)); 28 | sb.write(separator); 29 | sb.write(language.millisecond(duration.inMilliseconds, abbreviated)); 30 | return sb.toString(); 31 | } 32 | } else { 33 | // If only microseconds are present, print "yyy us" 34 | final sb = StringBuffer(); 35 | sb.write(duration.inMicroseconds); 36 | sb.write(separator); 37 | sb.write(language.microseconds(duration.inMicroseconds, abbreviated)); 38 | return sb.toString(); 39 | } 40 | } 41 | 42 | /// Pretty format [duration] in terms of seconds. 43 | /// 44 | /// If [terse] is true, milliseconds and microseconds are ignored. 45 | /// Use [language] to configure which locale to print for. 46 | /// Use [abbreviated] to control if the units should be abbreviated. 47 | String prettySeconds(Duration duration, 48 | {bool terse = false, 49 | DurationLocale language = const EnglishDurationLocale(), 50 | String separator = ' ', 51 | String delimiter = ' ', 52 | bool abbreviated = false}) { 53 | if (duration.inSeconds > 0) { 54 | if (duration.inMilliseconds == 0 || terse) { 55 | final sb = StringBuffer(); 56 | sb.write(duration.inSeconds); 57 | sb.write(separator); 58 | sb.write(language.second(duration.inSeconds, abbreviated)); 59 | return sb.toString(); 60 | } else if (duration.inMicroseconds == 0) { 61 | final sb = StringBuffer(); 62 | sb.write(duration.inSeconds); 63 | sb.write('.'); 64 | sb.write(_digits(duration.inMilliseconds % 1000, 3)); 65 | sb.write(separator); 66 | sb.write(language.second(duration.inSeconds, abbreviated)); 67 | return sb.toString(); 68 | } else { 69 | final sb = StringBuffer(); 70 | sb.write(duration.inSeconds); 71 | sb.write(separator); 72 | sb.write(language.second(duration.inSeconds, abbreviated)); 73 | sb.write(delimiter); 74 | sb.write(duration.inMilliseconds % 1000); 75 | sb.write('.'); 76 | sb.write(_digits(duration.inMicroseconds % 1000, 3)); 77 | sb.write(separator); 78 | sb.write( 79 | language.millisecond(duration.inMilliseconds % 1000, abbreviated)); 80 | return sb.toString(); 81 | } 82 | } else { 83 | return prettyMilliseconds(duration, 84 | terse: terse, 85 | language: language, 86 | separator: separator, 87 | abbreviated: abbreviated); 88 | } 89 | } 90 | 91 | String _digits(int data, int digits) { 92 | final String ret = data.toString(); 93 | if (ret.length < digits) return '0' * (digits - ret.length) + ret; 94 | return ret; 95 | } 96 | -------------------------------------------------------------------------------- /lib/src/parse/parse.dart: -------------------------------------------------------------------------------- 1 | /// Parses duration string formatted by [prettyDuration] into [Duration] (in abbreviated mode). 2 | /// [separator] defines the string that splits duration components in the string. 3 | /// 4 | /// Example: 5 | /// parseDuration('2w 5d 23h 59m 59s 999ms 999us'); 6 | Duration parseDuration(String input, {String separator = ','}) { 7 | bool isNegative = false; 8 | if (input.startsWith('-')) { 9 | isNegative = true; 10 | input = input.substring(1); 11 | } else if (input.startsWith('+')) { 12 | input = input.substring(1); 13 | } 14 | 15 | final parts = input.split(separator).map((t) => t.trim()).toList(); 16 | 17 | int? weeks; 18 | int? days; 19 | int? hours; 20 | int? minutes; 21 | int? seconds; 22 | int? milliseconds; 23 | int? microseconds; 24 | 25 | for (String part in parts) { 26 | final match = RegExp(r'^(\d+)(w|d|h|min|m|s|ms|us)$').matchAsPrefix(part); 27 | if (match == null) throw FormatException('Invalid duration format'); 28 | 29 | int value = int.parse(match.group(1)!); 30 | String? unit = match.group(2); 31 | 32 | switch (unit) { 33 | case 'w': 34 | if (weeks != null) { 35 | throw FormatException('Weeks specified multiple times'); 36 | } 37 | weeks = value; 38 | break; 39 | case 'd': 40 | if (days != null) { 41 | throw FormatException('Days specified multiple times'); 42 | } 43 | days = value; 44 | break; 45 | case 'h': 46 | if (hours != null) { 47 | throw FormatException('Hours specified multiple times'); 48 | } 49 | hours = value; 50 | break; 51 | case 'min': 52 | case 'm': 53 | if (minutes != null) { 54 | throw FormatException('Minutes specified multiple times'); 55 | } 56 | minutes = value; 57 | break; 58 | case 's': 59 | if (seconds != null) { 60 | throw FormatException('Seconds specified multiple times'); 61 | } 62 | seconds = value; 63 | break; 64 | case 'ms': 65 | if (milliseconds != null) { 66 | throw FormatException('Milliseconds specified multiple times'); 67 | } 68 | milliseconds = value; 69 | break; 70 | case 'us': 71 | if (microseconds != null) { 72 | throw FormatException('Microseconds specified multiple times'); 73 | } 74 | microseconds = value; 75 | break; 76 | default: 77 | throw FormatException('Invalid duration unit $unit'); 78 | } 79 | } 80 | 81 | var ret = Duration( 82 | days: (days ?? 0) + (weeks ?? 0) * 7, 83 | hours: hours ?? 0, 84 | minutes: minutes ?? 0, 85 | seconds: seconds ?? 0, 86 | milliseconds: milliseconds ?? 0, 87 | microseconds: microseconds ?? 0); 88 | return isNegative ? -ret : ret; 89 | } 90 | 91 | /// Parses duration string formatted by Duration.toString() to [Duration]. 92 | /// The string should be of form hours:minutes:seconds.microseconds 93 | /// 94 | /// Example: 95 | /// parseTime('245:09:08.007006'); 96 | Duration parseTime(String input) { 97 | bool isNegative = false; 98 | if (input.startsWith('-')) { 99 | isNegative = true; 100 | input = input.substring(1); 101 | } else if (input.startsWith('+')) { 102 | input = input.substring(1); 103 | } 104 | 105 | final parts = input.split(':'); 106 | 107 | if (parts.length != 3) throw FormatException('Invalid time format'); 108 | 109 | int days; 110 | int hours; 111 | int minutes; 112 | int seconds; 113 | int milliseconds; 114 | int microseconds; 115 | 116 | { 117 | final p = parts[2].split('.'); 118 | 119 | if (p.length != 2) throw FormatException('Invalid time format'); 120 | 121 | // If fractional seconds is passed, but less than 6 digits 122 | // Pad out to the right so we can calculate the ms/us correctly 123 | final p2 = int.parse(p[1].padRight(6, '0')); 124 | microseconds = p2 % 1000; 125 | milliseconds = p2 ~/ 1000; 126 | 127 | seconds = int.parse(p[0]); 128 | } 129 | 130 | minutes = int.parse(parts[1]); 131 | 132 | { 133 | int p = int.parse(parts[0]); 134 | hours = p % 24; 135 | days = p ~/ 24; 136 | } 137 | 138 | // TODO verify that there are no negative parts 139 | 140 | var ret = Duration( 141 | days: days, 142 | hours: hours, 143 | minutes: minutes, 144 | seconds: seconds, 145 | milliseconds: milliseconds, 146 | microseconds: microseconds); 147 | 148 | return isNegative ? -ret : ret; 149 | } 150 | 151 | Duration? tryParseDuration(String input) { 152 | try { 153 | return parseDuration(input); 154 | } catch (_) { 155 | return null; 156 | } 157 | } 158 | 159 | Duration? tryParseTime(String input) { 160 | try { 161 | return parseTime(input); 162 | } catch (_) { 163 | return null; 164 | } 165 | } 166 | 167 | Duration? tryParseDurationAny(String input) { 168 | try { 169 | return parseDuration(input); 170 | } catch (_) { 171 | try { 172 | return parseTime(input); 173 | } catch (e) { 174 | return null; 175 | } 176 | } 177 | } 178 | -------------------------------------------------------------------------------- /lib/src/tersity.dart: -------------------------------------------------------------------------------- 1 | /// Enum used to control tersity of [prettyDuration]. 2 | enum DurationTersity { 3 | week(1), 4 | day(7), 5 | hour(24), 6 | minute(60), 7 | second(60), 8 | millisecond(1000), 9 | microsecond(1000); 10 | 11 | const DurationTersity(this.mod); 12 | 13 | final int mod; 14 | 15 | int get _id => values.length - index; 16 | 17 | @override 18 | String toString() => name; 19 | 20 | bool operator >(DurationTersity other) => _id > other._id; 21 | bool operator <(DurationTersity other) => _id < other._id; 22 | bool operator >=(DurationTersity other) => _id >= other._id; 23 | bool operator <=(DurationTersity other) => _id <= other._id; 24 | } 25 | 26 | extension DurExt on Duration { 27 | int get inWeeks => inDays ~/ 7; 28 | 29 | int inUnit(DurationTersity unit) { 30 | switch (unit) { 31 | case DurationTersity.week: 32 | return inWeeks; 33 | case DurationTersity.day: 34 | return inDays; 35 | case DurationTersity.hour: 36 | return inHours; 37 | case DurationTersity.minute: 38 | return inMinutes; 39 | case DurationTersity.second: 40 | return inSeconds; 41 | case DurationTersity.millisecond: 42 | return inMilliseconds; 43 | case DurationTersity.microsecond: 44 | return inMicroseconds; 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /lib/src/time_const.dart: -------------------------------------------------------------------------------- 1 | /// Duration of a microsecond 2 | const Duration aMicrosecond = Duration(microseconds: 1); 3 | 4 | /// Duration of a millisecond 5 | const Duration aMillisecond = Duration(milliseconds: 1); 6 | 7 | /// Duration of a second 8 | const Duration aSecond = Duration(seconds: 1); 9 | 10 | /// Duration of a minute 11 | const Duration aMinute = Duration(minutes: 1); 12 | 13 | /// Duration of an hour 14 | const Duration anHour = Duration(hours: 1); 15 | 16 | /// Duration of a day 17 | const Duration aDay = Duration(days: 1); 18 | 19 | /// Duration of a week 20 | const Duration aWeek = Duration(days: 7); 21 | 22 | // Creates a [Duration] with given [milliseconds] 23 | Duration us(int microseconds) => Duration(microseconds: microseconds); 24 | 25 | // Creates a [Duration] with given [milliseconds] 26 | Duration ms(int milliseconds) => Duration(milliseconds: milliseconds); 27 | 28 | // Creates a [Duration] with given [seconds] 29 | Duration seconds(int seconds) => Duration(seconds: seconds); 30 | 31 | // Creates a [Duration] with given [minutes] 32 | Duration minutes(int minutes) => Duration(minutes: minutes); 33 | 34 | // Creates a [Duration] with given [minutes] 35 | Duration hours(int hours) => Duration(hours: hours); 36 | 37 | // Creates a [Duration] with given [days] 38 | Duration days(int days) => Duration(days: days); 39 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: duration 2 | description: Utilities to make working with 'Duration's easier. Formats duration in human readable form and also parses duration in human readable form to Dart's Duration. 3 | version: 4.0.3 4 | homepage: https://github.com/desktop-dart/duration 5 | 6 | environment: 7 | sdk: '>=2.17.0 <4.0.0' 8 | 9 | dev_dependencies: 10 | test: ^1.16.7 11 | lints: ^2.1.1 12 | -------------------------------------------------------------------------------- /test/locale/ukrainian_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:duration/duration.dart'; 2 | import 'package:duration/locale.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() { 6 | group('Ukrainian locale test', () { 7 | const locale = UkrainianDurationLocale(); 8 | test('Nominative Singular - number ends with 1', () { 9 | const dur1 = Duration( 10 | days: 8, 11 | hours: 1, 12 | minutes: 1, 13 | seconds: 1, 14 | milliseconds: 1, 15 | microseconds: 1); 16 | const dur21 = Duration( 17 | days: 21, 18 | hours: 21, 19 | minutes: 21, 20 | seconds: 21, 21 | milliseconds: 21, 22 | microseconds: 21); 23 | const dur201 = Duration( 24 | days: 201, 25 | hours: 21, 26 | minutes: 21, 27 | seconds: 21, 28 | milliseconds: 201, 29 | microseconds: 201); 30 | const dur21W = Duration(days: 21 * 7); 31 | const dur201W = Duration(days: 201 * 7); 32 | 33 | expect( 34 | prettyDuration(dur1, 35 | locale: locale, tersity: DurationTersity.microsecond), 36 | '1 тиждень 1 день 1 година 1 хвилина 1 секунда 1 мілісекунда 1 мікросекунда', 37 | ); 38 | expect( 39 | prettyDuration(dur21, 40 | locale: locale, 41 | upperTersity: DurationTersity.day, 42 | tersity: DurationTersity.microsecond), 43 | '21 день 21 година 21 хвилина 21 секунда 21 мілісекунда 21 мікросекунда', 44 | ); 45 | expect( 46 | prettyDuration(dur201, 47 | locale: locale, 48 | upperTersity: DurationTersity.day, 49 | tersity: DurationTersity.microsecond), 50 | '201 день 21 година 21 хвилина 21 секунда 201 мілісекунда 201 мікросекунда', 51 | ); 52 | expect(prettyDuration(dur21W, locale: locale, maxUnits: 1), '21 тиждень'); 53 | expect( 54 | prettyDuration(dur201W, locale: locale, maxUnits: 1), '201 тиждень'); 55 | }); 56 | test('Genitive singular - number ends with 2,3 or 4', () { 57 | const dur2 = Duration( 58 | days: 16, 59 | hours: 2, 60 | minutes: 2, 61 | seconds: 2, 62 | milliseconds: 2, 63 | microseconds: 2); 64 | const dur4 = Duration( 65 | days: 32, 66 | hours: 4, 67 | minutes: 4, 68 | seconds: 4, 69 | milliseconds: 4, 70 | microseconds: 4); 71 | expect( 72 | prettyDuration(dur2, 73 | locale: locale, tersity: DurationTersity.microsecond), 74 | '2 тижні 2 дні 2 години 2 хвилини 2 секунди 2 мілісекунди 2 мікросекунди', 75 | ); 76 | expect( 77 | prettyDuration(dur4, 78 | locale: locale, tersity: DurationTersity.microsecond), 79 | '4 тижні 4 дні 4 години 4 хвилини 4 секунди 4 мілісекунди 4 мікросекунди', 80 | ); 81 | }); 82 | test('Genitive plural - number ends with 5,6,7,8,9,0', () { 83 | const dur0 = Duration.zero; 84 | const dur5 = Duration( 85 | days: 40, 86 | hours: 5, 87 | minutes: 5, 88 | seconds: 5, 89 | milliseconds: 5, 90 | microseconds: 5); 91 | const dur9 = Duration( 92 | days: 9, 93 | hours: 9, 94 | minutes: 9, 95 | seconds: 9, 96 | milliseconds: 9, 97 | microseconds: 9); 98 | const dur29 = Duration( 99 | days: 29, 100 | hours: 9, 101 | minutes: 29, 102 | seconds: 29, 103 | milliseconds: 29, 104 | microseconds: 29); 105 | const dur209 = Duration( 106 | days: 209, 107 | hours: 9, 108 | minutes: 29, 109 | seconds: 29, 110 | milliseconds: 209, 111 | microseconds: 209); 112 | 113 | expect( 114 | prettyDuration(dur0, 115 | locale: locale, tersity: DurationTersity.microsecond), 116 | '0 мікросекунд', 117 | ); 118 | expect( 119 | prettyDuration(dur5, 120 | locale: locale, tersity: DurationTersity.microsecond), 121 | '5 тижнів 5 днів 5 годин 5 хвилин 5 секунд 5 мілісекунд 5 мікросекунд', 122 | ); 123 | expect( 124 | prettyDuration(dur9, 125 | locale: locale, 126 | upperTersity: DurationTersity.day, 127 | tersity: DurationTersity.microsecond), 128 | '9 днів 9 годин 9 хвилин 9 секунд 9 мілісекунд 9 мікросекунд', 129 | ); 130 | expect( 131 | prettyDuration(dur29, 132 | locale: locale, 133 | upperTersity: DurationTersity.day, 134 | tersity: DurationTersity.microsecond), 135 | '29 днів 9 годин 29 хвилин 29 секунд 29 мілісекунд 29 мікросекунд', 136 | ); 137 | expect( 138 | prettyDuration(dur209, 139 | locale: locale, 140 | upperTersity: DurationTersity.day, 141 | tersity: DurationTersity.microsecond), 142 | '209 днів 9 годин 29 хвилин 29 секунд 209 мілісекунд 209 мікросекунд', 143 | ); 144 | }); 145 | 146 | test('Genitive plural edge case - number is equal to or ends with 11 to 20', 147 | () { 148 | expect(prettyDuration(const Duration(seconds: 11), locale: locale), 149 | '11 секунд'); 150 | expect(prettyDuration(const Duration(seconds: 12), locale: locale), 151 | '12 секунд'); 152 | expect(prettyDuration(const Duration(seconds: 13), locale: locale), 153 | '13 секунд'); 154 | expect(prettyDuration(const Duration(seconds: 14), locale: locale), 155 | '14 секунд'); 156 | expect(prettyDuration(const Duration(seconds: 15), locale: locale), 157 | '15 секунд'); 158 | expect(prettyDuration(const Duration(seconds: 16), locale: locale), 159 | '16 секунд'); 160 | expect(prettyDuration(const Duration(seconds: 17), locale: locale), 161 | '17 секунд'); 162 | expect(prettyDuration(const Duration(seconds: 18), locale: locale), 163 | '18 секунд'); 164 | expect(prettyDuration(const Duration(seconds: 19), locale: locale), 165 | '19 секунд'); 166 | expect(prettyDuration(const Duration(seconds: 20), locale: locale), 167 | '20 секунд'); 168 | }); 169 | }); 170 | } 171 | -------------------------------------------------------------------------------- /test/locale_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:duration/duration.dart'; 2 | import 'package:duration/locale.dart'; 3 | import 'package:test/test.dart'; 4 | 5 | void main() { 6 | group('test by language', () { 7 | test('English', () { 8 | const locale = EnglishDurationLocale(); 9 | const dur = Duration(days: 5, hours: 23); 10 | expect( 11 | prettyDuration(dur, locale: locale), 12 | '5 days 23 hours', 13 | ); 14 | expect( 15 | prettyDuration(dur, locale: locale, abbreviated: true), 16 | '5d, 23h', 17 | ); 18 | expect( 19 | prettyDuration(dur, locale: locale, abbreviated: true, delimiter: ' '), 20 | '5 d 23 h', 21 | ); 22 | }); 23 | 24 | test('Chinese', () { 25 | const locale = ChineseSimplifiedDurationLocale(); 26 | const dur = Duration(days: 5, hours: 23); 27 | expect( 28 | prettyDuration(dur, locale: locale), 29 | '5日 23小时', 30 | ); 31 | expect( 32 | prettyDuration(dur, locale: locale, abbreviated: true), 33 | '5日, 23小时', 34 | ); 35 | expect( 36 | prettyDuration(dur, locale: locale, abbreviated: true, delimiter: ' '), 37 | '5日 23小时', 38 | ); 39 | }); 40 | 41 | test('Japanese', () { 42 | const locale = JapaneseDurationLocale(); 43 | const dur = Duration(days: 5, hours: 23); 44 | expect( 45 | prettyDuration(dur, locale: locale), 46 | '5日 23時間', 47 | ); 48 | expect( 49 | prettyDuration(dur, locale: locale, abbreviated: true), 50 | '5日, 23時間', 51 | ); 52 | expect( 53 | prettyDuration(dur, locale: locale, abbreviated: true, delimiter: ' '), 54 | '5日 23時間', 55 | ); 56 | }); 57 | 58 | test('Korean', () { 59 | const locale = KoreanDurationLocale(); 60 | const dur = Duration(days: 5, hours: 23); 61 | expect( 62 | prettyDuration(dur, locale: locale), 63 | '5일 23시간', 64 | ); 65 | expect( 66 | prettyDuration(dur, locale: locale, abbreviated: true), 67 | '5일, 23시간', 68 | ); 69 | expect( 70 | prettyDuration(dur, locale: locale, abbreviated: true, delimiter: ' '), 71 | '5일 23시간', 72 | ); 73 | }); 74 | }); 75 | } 76 | -------------------------------------------------------------------------------- /test/parse/time_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:duration/duration.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() { 5 | group('parseTime', () { 6 | test('time', () { 7 | expect(parseTime('245:09:08.007006').toString(), '245:09:08.007006'); 8 | }); 9 | test('time without microsecods', () { 10 | expect(parseTime('245:09:08.12').toString(), '245:09:08.120000'); 11 | }); 12 | test('Negative', () { 13 | expect(parseTime('-245:09:08.007006').toString(), '-245:09:08.007006'); 14 | }); 15 | }); 16 | 17 | group('parseDuration', () { 18 | test('duration', () { 19 | expect((parseDuration('20d,10h,30m')).toString(), '490:30:00.000000'); 20 | }); 21 | 22 | test('duration weeks', () { 23 | expect((parseDuration('2w,20d,10h,30m')).toString(), '826:30:00.000000'); 24 | }); 25 | test('Negative', () { 26 | expect(parseDuration('-20d,10h,30m').toString(), '-490:30:00.000000'); 27 | }); 28 | }); 29 | } 30 | -------------------------------------------------------------------------------- /test/pretty_duration_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:duration/duration.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() { 5 | group('prettyDuration', () { 6 | test('default', () { 7 | { 8 | final dur = Duration(microseconds: 0); 9 | expect(prettyDuration(dur), '0 seconds'); 10 | } 11 | 12 | { 13 | final dur = Duration(microseconds: 999); 14 | expect(prettyDuration(dur, tersity: DurationTersity.microsecond), 15 | '999 microseconds'); 16 | } 17 | 18 | { 19 | final dur = Duration(microseconds: 1000); 20 | expect(prettyDuration(dur, tersity: DurationTersity.microsecond), 21 | '1 millisecond'); 22 | } 23 | 24 | { 25 | final dur = Duration(milliseconds: 5); 26 | expect(prettyDuration(dur, tersity: DurationTersity.microsecond), 27 | '5 milliseconds'); 28 | } 29 | 30 | { 31 | final dur = Duration(milliseconds: 999); 32 | expect(prettyDuration(dur, tersity: DurationTersity.microsecond), 33 | '999 milliseconds'); 34 | } 35 | 36 | { 37 | final dur = Duration(milliseconds: 1000); 38 | expect(prettyDuration(dur), '1 second'); 39 | } 40 | 41 | { 42 | final dur = Duration(seconds: 5); 43 | expect(prettyDuration(dur), '5 seconds'); 44 | } 45 | 46 | { 47 | final dur = Duration(seconds: 59); 48 | expect(prettyDuration(dur), '59 seconds'); 49 | } 50 | 51 | { 52 | final dur = Duration(seconds: 60); 53 | expect(prettyDuration(dur), '1 minute'); 54 | } 55 | 56 | { 57 | final dur = Duration(minutes: 5); 58 | expect(prettyDuration(dur), '5 minutes'); 59 | } 60 | 61 | { 62 | final dur = Duration(minutes: 59); 63 | expect(prettyDuration(dur), '59 minutes'); 64 | } 65 | 66 | { 67 | final dur = Duration(minutes: 60); 68 | expect(prettyDuration(dur), '1 hour'); 69 | } 70 | 71 | { 72 | final dur = Duration(hours: 5); 73 | expect(prettyDuration(dur), '5 hours'); 74 | } 75 | 76 | { 77 | final dur = Duration(hours: 23); 78 | expect(prettyDuration(dur), '23 hours'); 79 | } 80 | 81 | { 82 | final dur = Duration(hours: 24); 83 | expect(prettyDuration(dur), '1 day'); 84 | } 85 | 86 | { 87 | final dur = Duration(days: 5); 88 | expect(prettyDuration(dur), '5 days'); 89 | } 90 | 91 | { 92 | final dur = Duration(days: 7); 93 | expect(prettyDuration(dur), '1 week'); 94 | } 95 | 96 | { 97 | final dur = Duration(days: 14); 98 | expect(prettyDuration(dur), '2 weeks'); 99 | } 100 | }); 101 | 102 | test('Abbreviated', () { 103 | { 104 | final dur = Duration(microseconds: 0); 105 | expect( 106 | prettyDuration(dur, 107 | abbreviated: true, tersity: DurationTersity.microsecond), 108 | '0us'); 109 | } 110 | 111 | { 112 | final dur = Duration(microseconds: 999); 113 | expect( 114 | prettyDuration(dur, 115 | abbreviated: true, tersity: DurationTersity.microsecond), 116 | '999us'); 117 | } 118 | 119 | { 120 | final dur = Duration(microseconds: 1000); 121 | expect( 122 | prettyDuration(dur, 123 | abbreviated: true, tersity: DurationTersity.microsecond), 124 | '1ms'); 125 | } 126 | 127 | { 128 | final dur = Duration(milliseconds: 5); 129 | expect( 130 | prettyDuration(dur, 131 | abbreviated: true, tersity: DurationTersity.microsecond), 132 | '5ms'); 133 | } 134 | 135 | { 136 | final dur = Duration(milliseconds: 999); 137 | expect( 138 | prettyDuration(dur, 139 | abbreviated: true, tersity: DurationTersity.microsecond), 140 | '999ms'); 141 | } 142 | 143 | { 144 | final dur = Duration(milliseconds: 1000); 145 | expect(prettyDuration(dur, abbreviated: true), '1s'); 146 | } 147 | 148 | { 149 | final dur = Duration(seconds: 5); 150 | expect(prettyDuration(dur, abbreviated: true), '5s'); 151 | } 152 | 153 | { 154 | final dur = Duration(seconds: 59); 155 | expect(prettyDuration(dur, abbreviated: true), '59s'); 156 | } 157 | 158 | { 159 | final dur = Duration(seconds: 60); 160 | expect(prettyDuration(dur, abbreviated: true), '1min'); 161 | } 162 | 163 | { 164 | final dur = Duration(minutes: 5); 165 | expect(prettyDuration(dur, abbreviated: true), '5min'); 166 | } 167 | 168 | { 169 | final dur = Duration(minutes: 59); 170 | expect(prettyDuration(dur, abbreviated: true), '59min'); 171 | } 172 | 173 | { 174 | final dur = Duration(minutes: 60); 175 | expect(prettyDuration(dur, abbreviated: true), '1h'); 176 | } 177 | 178 | { 179 | final dur = Duration(hours: 5); 180 | expect(prettyDuration(dur, abbreviated: true), '5h'); 181 | } 182 | 183 | { 184 | final dur = Duration(hours: 23); 185 | expect(prettyDuration(dur, abbreviated: true), '23h'); 186 | } 187 | 188 | { 189 | final dur = Duration(hours: 24); 190 | expect(prettyDuration(dur, abbreviated: true), '1d'); 191 | } 192 | 193 | { 194 | final dur = Duration(days: 5); 195 | expect(prettyDuration(dur, abbreviated: true), '5d'); 196 | } 197 | 198 | { 199 | final dur = Duration(days: 7); 200 | expect(prettyDuration(dur, abbreviated: true), '1w'); 201 | } 202 | 203 | { 204 | final dur = Duration(days: 14); 205 | expect(prettyDuration(dur, abbreviated: true), '2w'); 206 | } 207 | }); 208 | 209 | test('Delimiter', () { 210 | expect( 211 | prettyDuration(aMinute * 10, delimiter: ', '), 212 | '10 minutes', 213 | ); 214 | 215 | expect( 216 | prettyDuration(aMinute * 10 + aSecond * 5, delimiter: ', '), 217 | '10 minutes, 5 seconds', 218 | ); 219 | 220 | expect( 221 | prettyDuration(anHour * 15 + aMinute * 10 + aSecond * 5, 222 | delimiter: ', '), 223 | '15 hours, 10 minutes, 5 seconds', 224 | ); 225 | }); 226 | 227 | test('Spacer', () { 228 | expect( 229 | prettyDuration(aMinute * 10, spacer: '_'), 230 | '10_minutes', 231 | ); 232 | 233 | expect( 234 | prettyDuration(aMinute * 10 + aSecond * 5, spacer: '_'), 235 | '10_minutes 5_seconds', 236 | ); 237 | 238 | expect( 239 | prettyDuration(anHour * 15 + aMinute * 10 + aSecond * 5, spacer: '_'), 240 | '15_hours 10_minutes 5_seconds', 241 | ); 242 | }); 243 | 244 | test('Conjugation', () { 245 | expect( 246 | prettyDuration(aMinute * 10, conjunction: ' and '), 247 | '10 minutes', 248 | ); 249 | 250 | expect( 251 | prettyDuration(aMinute * 10 + aSecond * 5, conjunction: ' and '), 252 | '10 minutes and 5 seconds', 253 | ); 254 | 255 | expect( 256 | prettyDuration(anHour * 15 + aMinute * 10 + aSecond * 5, 257 | conjunction: ' and '), 258 | '15 hours 10 minutes and 5 seconds', 259 | ); 260 | 261 | expect( 262 | prettyDuration(anHour * 15 + aMinute * 10 + aSecond * 5, 263 | conjunction: ' and ', delimiter: ', '), 264 | '15 hours, 10 minutes and 5 seconds', 265 | ); 266 | }); 267 | 268 | test('Terse', () { 269 | final dur = Duration( 270 | days: 5, 271 | hours: 23, 272 | minutes: 59, 273 | seconds: 59, 274 | milliseconds: 999, 275 | microseconds: 999); 276 | 277 | expect(prettyDuration(dur, tersity: DurationTersity.day), '5 days'); 278 | 279 | expect(prettyDuration(dur, tersity: DurationTersity.hour), 280 | '5 days 23 hours'); 281 | 282 | expect(prettyDuration(dur, tersity: DurationTersity.minute), 283 | '5 days 23 hours 59 minutes'); 284 | 285 | expect(prettyDuration(dur, tersity: DurationTersity.second), 286 | '5 days 23 hours 59 minutes 59 seconds'); 287 | 288 | expect(prettyDuration(dur, tersity: DurationTersity.millisecond), 289 | '5 days 23 hours 59 minutes 59 seconds 999 milliseconds'); 290 | 291 | expect(prettyDuration(dur, tersity: DurationTersity.microsecond), 292 | '5 days 23 hours 59 minutes 59 seconds 999 milliseconds 999 microseconds'); 293 | }); 294 | 295 | test('upperTersity', () { 296 | { 297 | final dur = Duration(hours: 25); 298 | expect(prettyDuration(dur, upperTersity: DurationTersity.hour), 299 | '25 hours'); 300 | } 301 | 302 | { 303 | final dur = Duration(days: 2, hours: 2); 304 | expect(prettyDuration(dur, upperTersity: DurationTersity.minute), 305 | '3000 minutes'); 306 | } 307 | }); 308 | 309 | test('maxUnits', () { 310 | final dur = Duration(days: 5, hours: 23, minutes: 15, seconds: 25); 311 | 312 | expect(prettyDuration(dur, maxUnits: 1), '5 days'); 313 | expect(prettyDuration(dur, maxUnits: 3), '5 days 23 hours 15 minutes'); 314 | expect(prettyDuration(dur, maxUnits: 3, conjunction: ' and '), 315 | '5 days 23 hours and 15 minutes'); 316 | }); 317 | 318 | test('MinusDurations', () { 319 | expect( 320 | (-(aMinute * 10)).pretty(), 321 | '-10 minutes', 322 | ); 323 | expect( 324 | (-(aMinute * 10 + aSecond * 15)).pretty(), 325 | '-10 minutes 15 seconds', 326 | ); 327 | expect( 328 | (-(aMinute * 10 + aSecond * 15)).pretty(maxUnits: 1), 329 | '-10 minutes', 330 | ); 331 | }); 332 | }); 333 | } 334 | -------------------------------------------------------------------------------- /test/pretty_second_test.dart: -------------------------------------------------------------------------------- 1 | import 'package:duration/duration.dart'; 2 | import 'package:test/test.dart'; 3 | 4 | void main() { 5 | group('prettySecond', () { 6 | test('default', () { 7 | { 8 | final dur = Duration(microseconds: 0); 9 | expect(prettySeconds(dur), '0 microseconds'); 10 | } 11 | 12 | { 13 | final dur = Duration(microseconds: 999); 14 | expect(prettySeconds(dur), '999 microseconds'); 15 | } 16 | 17 | { 18 | final dur = Duration(microseconds: 1000); 19 | expect(prettySeconds(dur), '1 millisecond'); 20 | } 21 | 22 | { 23 | final dur = Duration(microseconds: 5123); 24 | expect(prettySeconds(dur), '5.123 milliseconds'); 25 | } 26 | 27 | { 28 | final dur = Duration(microseconds: 999555); 29 | expect(prettySeconds(dur), '999.555 milliseconds'); 30 | } 31 | 32 | { 33 | final dur = Duration(microseconds: 5123456); 34 | expect(prettySeconds(dur), '5 seconds 123.456 milliseconds'); 35 | } 36 | 37 | { 38 | final dur = Duration(microseconds: 124123456); 39 | expect(prettySeconds(dur), '124 seconds 123.456 milliseconds'); 40 | } 41 | }); 42 | test('Abbreviated', () { 43 | { 44 | final dur = Duration(microseconds: 0); 45 | expect(prettySeconds(dur, abbreviated: true), '0 us'); 46 | } 47 | 48 | { 49 | final dur = Duration(microseconds: 999); 50 | expect(prettySeconds(dur, abbreviated: true), '999 us'); 51 | } 52 | 53 | { 54 | final dur = Duration(microseconds: 1000); 55 | expect(prettySeconds(dur, abbreviated: true), '1 ms'); 56 | } 57 | 58 | { 59 | final dur = Duration(microseconds: 5123); 60 | expect(prettySeconds(dur, abbreviated: true), '5.123 ms'); 61 | } 62 | 63 | { 64 | final dur = Duration(microseconds: 999555); 65 | expect(prettySeconds(dur, abbreviated: true), '999.555 ms'); 66 | } 67 | 68 | { 69 | final dur = Duration(microseconds: 5123456); 70 | expect(prettySeconds(dur, abbreviated: true), '5 s 123.456 ms'); 71 | } 72 | 73 | { 74 | final dur = Duration(microseconds: 124123456); 75 | expect(prettySeconds(dur, abbreviated: true), '124 s 123.456 ms'); 76 | } 77 | }); 78 | test('terse', () { 79 | { 80 | final dur = Duration(microseconds: 0); 81 | expect(prettySeconds(dur, terse: true), '0 microseconds'); 82 | } 83 | 84 | { 85 | final dur = Duration(microseconds: 999); 86 | expect(prettySeconds(dur, terse: true), '999 microseconds'); 87 | } 88 | 89 | { 90 | final dur = Duration(microseconds: 1000); 91 | expect(prettySeconds(dur, terse: true), '1 millisecond'); 92 | } 93 | 94 | { 95 | final dur = Duration(microseconds: 5123); 96 | expect(prettySeconds(dur, terse: true), '5 milliseconds'); 97 | } 98 | 99 | { 100 | final dur = Duration(microseconds: 999555); 101 | expect(prettySeconds(dur, terse: true), '999 milliseconds'); 102 | } 103 | 104 | { 105 | final dur = Duration(microseconds: 5123456); 106 | expect(prettySeconds(dur, terse: true), '5 seconds'); 107 | } 108 | 109 | { 110 | final dur = Duration(microseconds: 124123456); 111 | expect(prettySeconds(dur, terse: true), '124 seconds'); 112 | } 113 | }); 114 | }); 115 | } 116 | --------------------------------------------------------------------------------