time_formatter 1.0.0+4

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new59

Time Formatter in Dart #

This library takes UNIX (milliseconds) timestamps and converts them to pretty, human-readable time formats.

Using #

Add the package to your pubspec.yaml

dependencies:
  time_formatter: ^1.0.0

Import the library in a .dart file

import 'package:time_formatter/time_formatter.dart';

Format an arbitrary UNIX timestamp (int type, milliseconds since epoch) with formatTime()

String formatted = formatTime(1548979724964)

Format #

Here's a mini truth table for the format of the value returned by the function:

  • < 1 second : "Just now"
  • < 60 seconds : "X seconds" (2-59)
  • < 2 minutes : "1 minute"
  • < 60 minutes : "X minutes" (2-59)
  • < 2 hours : "1 hour"
  • < 24 hours : "X hours" (2-23)
  • < 2 days : "1 day"
  • < 7 days : "X days" (2-6)
  • < 2 weeks : "1 week"
  • < 28 days : "X weeks" (2-3)
  • < 30.44 * 1.5 days : "1 month"
  • < 365 - 15.22 days : "X months" (2-12)
  • < 730 days : "1 year"
  • Rest : "X years"

I decided to use truncate() for everything except months, as they can yield scenarios where something was stamped 59 days ago, but it still returns "1 month" (for example). Monthly timestamps are rounded to the nearest month.

Contributing #

If you spot a bug or would like to provide additional functionality, you are more than welcome to submit an issue or pull request.

License #

GPL 2.0, as per recommendation.

1.0.0+4 #

  • Add dartdoc entries
  • Add CHANGELOG.md
  • Run dartfmt

1.0.0+3 #

  • Add LICENSE

1.0.0+2 #

  • Add test cases and examples

1.0.0+1 #

  • Initial release, with all provided functions

example/example.dart

import 'package:time_formatter/time_formatter.dart';

String formatted = formatTime(
    DateTime.now().subtract(Duration(hours: 2)).millisecondsSinceEpoch);

void main() {
  print(formatted);
  // 2 hours
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  time_formatter: ^1.0.0+4

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:time_formatter/time_formatter.dart';
  
Version Uploaded Documentation Archive
1.0.0+4 Feb 1, 2019 Go to the documentation of time_formatter 1.0.0+4 Download time_formatter 1.0.0+4 archive
1.0.0+3 Feb 1, 2019 Go to the documentation of time_formatter 1.0.0+3 Download time_formatter 1.0.0+3 archive
1.0.0+2 Feb 1, 2019 Go to the documentation of time_formatter 1.0.0+2 Download time_formatter 1.0.0+2 archive
1.0.0+1 Feb 1, 2019 Go to the documentation of time_formatter 1.0.0+1 Download time_formatter 1.0.0+1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
18
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
59
Learn more about scoring.

We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13+1

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:time_formatter/time_formatter.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
Dev dependencies
test ^1.5.3