protontime 2.0.0
protontime: ^2.0.0 copied to clipboard
A library useful for creating fuzzy timestamps. (e.g. "2 min ago")
protontime #
protontime
is a dart library that converts a date into a humanized text. Instead of showing a date 2024-01-01 01:01
with protontime
you can display something like "now", "5 sec ago", "an hour ago", etc
A Flutter plugin for managing timestamps.
Android | iOS | Linux | macOS | Web | Windows | |
---|---|---|---|---|---|---|
Support | SDK 16+ | 12.0+ | Any | 10.14+ | Any | Windows 10+ |
Installation #
With Dart:
dart pub add protontime
With Flutter:
flutter pub add protontime
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get):
dependencies:
protontime: ^latest
Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.
Import it #
Now in your Dart code, you can use:
import 'package:protontime/protontime.dart';
Usage #
The easiest way to use this library via top-level function format(date)
:
Quick Usage #
DateTime
import 'package:protontime/protontime.dart';
main() {
Duration fiveMin = Duration(days: 5); // 120:00:00.000000
DateTime fiveMinAdd = DateTime.now().add(fiveMin); // 2024-03-02 03:00:05.270034
DateTime fiveMinSub = DateTime.now().subtract(fiveMin); // 2024-02-21 03:00:05.277179
final resultPast = Protontime.format(fiveMinSub); // 5 days ago
final resultFuture = Protontime.format(fiveMinAdd); // 5 days from now
print("Past Time: $resultPast"); // 5 days ago
print('Future Time: $resultFuture'); // 5 days from now
}
Timestamp
import 'package:protontime/protontime.dart';
main() {
DateTime timestamp = DateTime.parse("2024-01-01T00:00:00.000000Z");
final resultTimestamp = Protontime.format(timestamp); // 5 days ago
print("Timestamp Time: $resultTimestamp"); // 5 days ago
}
Basic Usage (Short Messages
) #
import 'package:protontime/protontime.dart';
main() {
Duration fiveMin = Duration(days: 5); // 120:00:00.000000
DateTime fiveMinAdd = DateTime.now().add(fiveMin); // 2024-03-02 03:00:05.270034
DateTime fiveMinSub = DateTime.now().subtract(fiveMin); // 2024-02-21 03:00:05.277179
final resultPastShort = Protontime.format(
fiveMinSub,
short: true,
); // 5d
final resultFutureShort = Protontime.format(
fiveMinAdd,
short: true,
); // 5d
print("Past Short Time: $resultPast"); // 5d
print('Future Short Time: $resultFuture'); // 5d
}
Language #
The simplest method to format timestamps using local language is with protontime
.
import 'package:protontime/protontime.dart';
main() {
Duration fiveMin = Duration(days: 5); // 120:00:00.000000
DateTime fiveMinAdd = DateTime.now().add(fiveMin); // 2024-03-02 03:00:05.270034
DateTime fiveMinSub = DateTime.now().subtract(fiveMin); // 2024-02-21 03:00:05.277179
final resultEsp = Protontime.format(
fiveMinSub,
language: 'es',
); // hace 5 días
final resultEspShort = Protontime.format(
fiveMinAdd,
language: 'es',
short: true,
); // 5 días
print("Local Lanugage: $resultEsp"); // hace 5 días
print('Local Lanugage Short: $resultEspShort'); // 5 días
}
Supported Language #
Supported languages include Amharic, Arabic, Azerbaijani, and many more.
Flag | Language | Language Code |
---|---|---|
![]() |
Amharic | am |
![]() |
Arabic | ar |
![]() |
Azerbaijani | az |
![]() |
Belarusian | be |
![]() |
Bosnian | bs |
![]() |
Catalan | ca |
![]() |
Czech | cs |
![]() |
Danish | da |
![]() |
German | de |
![]() |
Dhivehi | dv |
![]() |
English | en |
![]() |
Spanish | es |
![]() |
Estonian | et |
![]() |
Persian | fa |
![]() |
Finnish | fi |
![]() |
French | fr |
![]() |
Greek | gr |
![]() |
Hebrew | he |
![]() |
Hindi | hi |
![]() |
Hungarian | hu |
![]() |
Indonesian | id |
![]() |
Italian | it |
![]() |
Japanese | ja |
![]() |
Khmer | km |
![]() |
Korean | ko |
![]() |
Kurdish | ku |
![]() |
Latvian | lv |
![]() |
Mongolian | mn |
![]() |
Malay | ms_my |
![]() |
Norwegian Bokmål | nb_no |
![]() |
Dutch | nl |
![]() |
Norwegian Nynorsk | nn_no |
![]() |
Polish | pl |
![]() |
Portuguese (Brazil) | pt_br |
![]() |
Romanian | ro |
![]() |
Russian | ru |
![]() |
Kinyarwanda | rw |
![]() |
Serbian | sr |
![]() |
Swedish | sv |
![]() |
Tamil | ta |
![]() |
Thai | th |
![]() |
Turkmen | tk |
![]() |
Turkish | tr |
![]() |
Ukrainian | uk |
![]() |
Urdu | ur |
![]() |
Vietnamese | vi |
![]() |
Chinese (Simplified) | zh_ch |
![]() |
Chinese (Traditional) | zh |
Scope #
While there have been numerous requests for additional complex features, I aim to maintain this library's simplicity to minimize the need for ongoing maintenance.
The focus of this library should be
- Offering a singular
format
function that converts adate
into a human-readableformat
. - Allowing users to incorporate their own languages or override existing ones as desired through customizable abstractions.
- Offer languages contributed by the community for users to incorporate as needed, avoiding the inclusion of all languages by default.
- The library should be dependency-free.