timeago 1.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

timeago #

A library useful for creating fuzzy timestamps. (e.g. "5 minutes ago")

Pub

Build Status

Usage #

The easiest way to use this library via top-level function timeAgo(date):

main() async {
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));

    print(timeAgo(fifteenAgo)); // 15 minutes ago
}

If you are using it in multiple places and want a little more control you can create your own instance:

import 'package:timeago/timeago.dart';

main() async {
    TimeAgo ta = new TimeAgo();
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));
    final fifteenFromNow = new DateTime.now().add(new Duration(minutes: 15));
    
    print(ta.format(fifteenAgo)); // 15 minutes ago
    print(ta.format(fifteenFromNow, until: true)); // 15 minutes from now
    
    // Change locale
    ta.locale = 'es';
    
    print(ta.format(fifteenAgo)); // hace 15 minutos
    print(ta.format(fifteenFromNow, until: true)); // dentro de 15 minutos
    
}

Using on the Browser(lazy load localizations) #

While you can use TimeAgo as previously described in the browser it will load all the current localization messages at once. To lazyload localizations use import 'package:timeago/browser_timeago.dart'; instead:

import 'package:timeago/browser_timeago.dart';

main() async {
    TimeAgo ta = new TimeAgo();
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));
    final fifteenFromNow = new DateTime.now().add(new Duration(minutes: 15));
    
    print(ta.format(fifteenAgo)); // 15 minutes ago
    print(ta.format(fifteenFromNow, until: true)); // 15 minutes from now
    
    
    // Lazy Load locale messages, only need to do this once per locale.
    await ta.initializeLocale("es");
    
    // Change locale
    ta.locale = 'es';
    
    print(ta.format(fifteenAgo)); // hace 15 minutos
    print(ta.format(fifteenFromNow, until: true)); // dentro de 15 minutos
    
}

Live Demo #

Here

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

1.3.0 #

  • Dart 2.0 support

1.2.6 #

  • Fixes to German translation

1.2.6 #

  • Adds Russian translation thanks to @asstet

1.2.5 #

  • Adds Italian translation thanks to @nicofossa

1.2.4 #

  • Adds Farsi translation thanks to @siavash-mohseni

1.2.3 #

  • Fixes Portuguese not working in non deferred import

1.2.2 #

  • Adds Portuguese translation thanks to @jeanpimentel

1.2.1 #

  • Adds German translation thanks to @btastic

1.2.0 #

  • Split import declaration into browser(lazy load) and server.

For browser(lazy load) use: import 'package:timeago/browser_timeago.dart'; For Flutter/server use: import 'package:timeago/browser_timeago.dart';

Breaking: TimeAgo.initializeLocale is no longer a static method

1.1.0 #

  • Adds Chinese and Simplified Chinese thanks to @oraclebox

1.0.2 #

  • Update intl dependency

1.0.0 #

  • API change for better clarity

0.1.2 #

  • Increase intl dependency to accept <0.15.0

0.1.1 #

  • Add fr and ja locales

0.1.0 #

  • Update messages to be more concise

0.0.1 #

  • Add en, en_short, es, and es_short locales

example/main.dart

// ignore: implementation_imports
import 'dart:async' show Timer;
import 'dart:html';
import 'package:timeago/browser_timeago.dart';

final mainContainer = querySelector("#main");
final listContainer = querySelector("#list");

main() async {
  final loadedTime = new DateTime.now();

  // Preload spanish messages
  defaultTimeAgo.initializeLocale('es');

  final updateMainContainer = () {
    final now = new DateTime.now();
    final difference = now.difference(loadedTime);
    mainContainer.text = timeAgo(now.subtract(difference));
  };

  querySelectorAll(".locale-link").onClick.listen((event) async {
    final el = event.target as AnchorElement;

    // Initialize selected locale
    defaultTimeAgo.initializeLocale(el.text);

    // Set locale for the default time ago object
    defaultTimeAgo.locale = el.text;

    // Recreate list items
    listContainer.innerHtml = "";
    createListItems();
  });

  updateMainContainer();
  createListItems();

  new Timer.periodic(new Duration(seconds: 1), (_) => updateMainContainer());
}

addItem(String text) {
  listContainer.append(new LIElement()..text = text);
}

createListItems() {
  final currentTime = new DateTime.now();
  addItem(
      timeAgo(currentTime.subtract(new Duration(microseconds: 1 * 44 * 1000))));
  addItem(timeAgo(currentTime.subtract(new Duration(minutes: 1))));
  addItem(timeAgo(currentTime.subtract(new Duration(minutes: 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(minutes: 50))));
  addItem(timeAgo(currentTime.subtract(new Duration(hours: 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 1))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 30))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 30 * 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 365))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 365 * 5))));

  addItem("-");

  addItem(
      timeUntil(currentTime.add(new Duration(microseconds: 1 * 44 * 1000))));
  addItem(timeUntil(currentTime.add(new Duration(minutes: 1))));
  addItem(timeUntil(currentTime.add(new Duration(minutes: 5))));
  addItem(timeUntil(currentTime.add(new Duration(minutes: 50))));
  addItem(timeUntil(currentTime.add(new Duration(hours: 5))));
  addItem(timeUntil(currentTime.add(new Duration(days: 1))));
  addItem(timeUntil(currentTime.add(new Duration(days: 5))));
  addItem(timeUntil(currentTime.add(new Duration(days: 30))));
  addItem(timeUntil(currentTime.add(new Duration(days: 30 * 5))));
  addItem(timeUntil(currentTime.add(new Duration(days: 365))));
  addItem(timeUntil(currentTime.add(new Duration(days: 365 * 5))));
}

timeUntil(DateTime date) {
  return timeAgo(date, until: true);
}

Use this package as a library

1. Depend on it

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


dependencies:
  timeago: ^1.3.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:timeago/timeago.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
98
Health:
Code health derived from static analysis. [more]
77
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
25
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/src/i18/messages_all_non_deferred.dart. (-7.71 points)

Analysis of lib/src/i18/messages_all_non_deferred.dart reported 16 hints, including:

line 24 col 15: Unnecessary new keyword.

line 25 col 21: Unnecessary new keyword.

line 26 col 15: Unnecessary new keyword.

line 27 col 21: Unnecessary new keyword.

line 28 col 15: Unnecessary new keyword.

Fix lib/src/base_timeago.dart. (-6.31 points)

Analysis of lib/src/base_timeago.dart reported 13 hints, including:

line 47 col 58: Use = to separate a named parameter from its default value.

line 50 col 19: Unnecessary new keyword.

line 73 col 9: DO use curly braces for all flow control structures.

line 75 col 9: DO use curly braces for all flow control structures.

line 77 col 9: DO use curly braces for all flow control structures.

Fix lib/src/i18/messages_all.dart. (-1.49 points)

Analysis of lib/src/i18/messages_all.dart reported 3 hints:

line 77 col 28: Unnecessary new keyword.

line 79 col 43: Unnecessary new keyword.

line 88 col 15: Avoid empty catch blocks.

Fix additional 16 files with analysis or formatting issues. (-10 points)

Additional issues in the following files:

  • lib/browser_timeago.dart (2 hints)
  • lib/src/i18/pt_BR_messages.dart (2 hints)
  • lib/src/i18/pt_BR_short_messages.dart (2 hints)
  • lib/src/i18/zh_CN_messages.dart (2 hints)
  • lib/timeago.dart (2 hints)
  • lib/src/i18/de_messages.dart (1 hint)
  • lib/src/i18/en_short_messages.dart (1 hint)
  • lib/src/i18/es_messages.dart (1 hint)
  • lib/src/i18/es_short_messages.dart (1 hint)
  • lib/src/i18/fa_messages.dart (1 hint)
  • lib/src/i18/fr_messages.dart (1 hint)
  • lib/src/i18/it_messages.dart (1 hint)
  • lib/src/i18/ja_messages.dart (1 hint)
  • lib/src/i18/ru_messages.dart (1 hint)
  • lib/src/i18/zh_messages.dart (1 hint)
  • lib/src/all_messages.dart (Run dartfmt to format lib/src/all_messages.dart.)

Maintenance issues and suggestions

Make sure dartdoc successfully runs on your package's source files. (-10 points)

exitCode: 255 stdout: parsing /tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/browser_timeago.dart... parsing /tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart... parsing /tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/base_timeago.dart... parsing /dart-sdk/lib/async/async.dart... parsing /dart-sdk/lib/collection/collection.dart... parsing /dart-sdk/lib/internal/internal.dart... parsing /dart-sdk/lib/convert/convert.dart... parsing /dart-sdk/lib/typed_data/typed_data.dart... parsing /dart-sdk/lib/core/core.dart... parsing /dart-sdk/lib/math/math.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/intl.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/date_symbols.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/number_symbols.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/number_symbols_data.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/date_format_internal.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/intl_helpers.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/plural_rules.dart... parsing /tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/all_messages.dart... parsing /tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/i18/messages_all.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/message_lookup_by_library.dart... [...] parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/internal_style.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/style.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/style/posix.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/parsed_path.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/style/url.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/utils.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/style/windows.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/path_exception.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/path_map.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/path-1.6.4/lib/src/path_set.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/data/dates/locale_list.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/file_data_reader.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/lazy_locale_data.dart... parsing /tmp/pub-cache-dirBYHQKF/hosted/pub.dartlang.org/intl-0.15.8/lib/src/http_request_data_reader.dart... Initialized dartdoc with 88 libraries in 36.6 seconds documenting timeago Generating docs for library timeago from package:timeago/browser_timeago.dart... Generating docs for library timeago from package:timeago/timeago.dart... Validating docs... Documented 2 public libraries in 1.9 seconds stderr: Documenting timeago... warning: timeago has no library level documentation comments, from timeago: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/browser_timeago.dart:1:9) warning: no canonical library found for timeago.TimeAgo.hashCode, not linking, from timeago.TimeAgo.hashCode: (file:///dart-sdk/lib/core/object.dart:94:20) warning: no canonical library found for timeago.TimeAgo.initializeMessages, not linking, from timeago.TimeAgo.initializeMessages: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/base_timeago.dart:11:22) warning: no canonical library found for timeago.TimeAgo.locale, not linking, from timeago.TimeAgo.locale: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/base_timeago.dart:17:10) warning: no canonical library found for timeago.TimeAgo.runtimeType, not linking, from timeago.TimeAgo.runtimeType: (file:///dart-sdk/lib/core/object.dart:116:21) warning: no canonical library found for timeago.TimeAgo.format, not linking, from timeago.TimeAgo.format: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/base_timeago.dart:47:10) warning: no canonical library found for timeago.TimeAgo.initializeLocale, not linking, from timeago.TimeAgo.initializeLocale: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/src/base_timeago.dart:22:16) warning: no canonical library found for timeago.TimeAgo.noSuchMethod, not linking, from timeago.TimeAgo.noSuchMethod: (file:///dart-sdk/lib/core/object.dart:111:20) warning: no canonical library found for timeago.TimeAgo.toString, not linking, from timeago.TimeAgo.toString: (file:///dart-sdk/lib/core/object.dart:99:19) warning: no canonical library found for timeago.TimeAgo.==, not linking, from timeago.TimeAgo.==: (file:///dart-sdk/lib/core/object.dart:62:26) warning: timeago has no library level documentation comments, from timeago: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart:1:9) error: failed to write file at: timeago/timeago-library.html for symbol timeago: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart:1:9) conflicting with file already generated by timeago: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/browser_timeago.dart:1:9) Dartdoc generates a path and filename to write to for each symbol. timeago conflicts with another symbol in the generated path, and therefore can not be written out. Changing the name, library name, or class name (if appropriate) of one of the conflicting items can resolve the conflict. Alternatively, use the @nodoc tag in one symbol's documentation comments to hide it. error: failed to write file at: timeago/TimeAgo-class.html, for symbol timeago.TimeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart:6:7) error: failed to write file at: timeago/TimeAgo/TimeAgo.html for symbol timeago.TimeAgo.TimeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart:7:3) conflicting with file already generated by timeago.TimeAgo.TimeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/browser_timeago.dart:8:3) error: failed to write file at: timeago/defaultTimeAgo.html for symbol timeago.defaultTimeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart:10:7) conflicting with file already generated by timeago.defaultTimeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/browser_timeago.dart:11:7) error: failed to write file at: timeago/timeAgo.html for symbol timeago.timeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/timeago.dart:15:8) conflicting with file already generated by timeago.timeAgo: (file:///tmp/pub-dartlang-dartdocBZBKBG/pkg/lib/browser_timeago.dart:16:8) found 11 warnings and 5 errors Unhandled exception: dartdoc encountered 5 errors while processing. #0 Dartdoc.generateDocs (package:dartdoc/dartdoc.dart:202:7)

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (intl).

Package is getting outdated. (-55.07 points)

The package was last published 80 weeks ago.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.23.0 <3.0.0
intl ^0.15.1 0.15.8 0.16.1
Transitive dependencies
path 1.6.4
Dev dependencies
test ^1.3.0