clock 1.0.1

  • Readme
  • Changelog
  • Installing
  • 76

This package provides a Clock class which encapsulates the notion of the "current time" and provides easy access to points relative to the current time. Different Clocks can have a different notion of the current time, and the default top-level clock's notion can be swapped out to reliably test timing-dependent code.

For example, you can use clock in your libraries like this:

// run_with_timing.dart
import 'package:clock/clock.dart';

/// Runs [callback] and prints how long it took.
T runWithTiming<T>(T callback()) {
  var stopwatch = clock.stopwatch()..start();
  var result = callback();
  print("It took ${stopwatch.elapsed}!");
  return result;

...and then test your code using the fake_async package, which automatically overrides the current clock:

// run_with_timing_test.dart
import 'run_with_timing.dart';

import 'package:fake_async/fake_async.dart';
import 'package:test/test.dart';

void main() {
  test("runWithTiming() prints the elapsed time", () {
    new FakeAsync().run((async) {
      expect(() {
        runWithTiming(() {
          async.elapse(new Duration(seconds: 10));
      }, prints("It took 0:00:10.000000!"));

1.0.1 #

  • Update to lowercase Dart core library constants.

1.0.0 #

This release contains the Clock class that was defined in quiver. It's backwards-compatible with the quiver version, and mostly backwards-compatible with the old version of the clock package.

New Features #

  • A top-level clock field has been added that provides a default Clock implementation. It can be controlled by the withClock() function. It should generally be used in preference to manual dependency-injection, since it will work with the fake_async package.

  • A Clock.stopwatch() method has been added that creates a Stopwatch that uses the clock as its source of time.

Changes Relative to clock 0.1 #

  • The top-level new getter and getStopwatch() methods are deprecated. and clock.stopwatch() should be used instead.

  • Clock.getStopwatch() is deprecated. Clock.stopwatch() should be used instead.

  • The isFinal argument to withClock() is deprecated.

  • new Clock() now takes an optional positional argument that returns the current time as a DateTime instead of its old arguments.

  • is now a method rather than a getter.

Use this package as a library

1. Depend on it

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

  clock: ^1.0.1

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:clock/clock.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

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

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Health suggestions

Fix lib/src/clock.dart. (-9.08 points)

Analysis of lib/src/clock.dart reported 19 hints, including:

line 37 col 41: 'systemTime' is deprecated and shouldn't be used. Use new instead..

line 55 col 20: Use = to separate a named parameter from its default value.

line 56 col 20: Use = to separate a named parameter from its default value.

line 57 col 22: Use = to separate a named parameter from its default value.

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

Fix lib/src/default.dart. (-2.48 points)

Analysis of lib/src/default.dart reported 5 hints:

line 21 col 19: Unnecessary new keyword.

line 25 col 21: Unnecessary new keyword.

line 38 col 9: 'isFinal' is deprecated and shouldn't be used.

line 40 col 68: Use = to separate a named parameter from its default value.

line 42 col 11: Unnecessary new keyword.

Fix lib/src/stopwatch.dart. (-1 points)

Analysis of lib/src/stopwatch.dart reported 2 hints:

line 22 col 20: Unnecessary new keyword.

line 43 col 27: Unnecessary new keyword.

Fix lib/clock.dart. (-0.50 points)

Analysis of lib/clock.dart reported 1 hint:

line 26 col 26: Unnecessary new keyword.

Fix lib/src/utils.dart. (-0.50 points)

Analysis of lib/src/utils.dart reported 1 hint:

line 25 col 22: Avoid const keyword.

Maintenance suggestions

Package is getting outdated. (-89.59 points)

The package was last published 98 weeks ago.

The package description is too short. (-17 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and clock.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.36.0 <3.0.0
meta >=0.9.0 <2.0.0 1.1.8
Dev dependencies
test ^0.12.28