Skip to content

Commit 23044d0

Browse files
committed
feature: Support for nano-, and pico-seconds
1 parent 9c48445 commit 23044d0

File tree

2 files changed

+59
-7
lines changed

2 files changed

+59
-7
lines changed

lib/src/time.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class TimeInterpreter extends MeasurementInterpreter<Time> {
1818
/// conversions are acceptable, via the [Time.ofDuration] and [Time.asDuration]
1919
/// methods.
2020
class Time extends Measurement<Time> {
21+
/// The [TimeInterpreter] for picoseconds.
22+
static final TimeInterpreter picoseconds = TimeInterpreter._(1e12);
23+
24+
/// The [TimeInterpreter] for nanoseconds.
25+
static final TimeInterpreter nanoseconds = TimeInterpreter._(1e9);
26+
2127
/// The [TimeInterpreter] for microseconds.
2228
static final TimeInterpreter microseconds = TimeInterpreter._(1e6);
2329

@@ -63,6 +69,8 @@ class Time extends Measurement<Time> {
6369

6470
/// Constructs a [Time] representing the sum of partial amounts.
6571
Time.of({
72+
final num picoseconds = 0,
73+
final num nanoseconds = 0,
6674
final num microseconds = 0,
6775
final num milliseconds = 0,
6876
final num seconds = 0,
@@ -71,14 +79,26 @@ class Time extends Measurement<Time> {
7179
final num days = 0,
7280
final Precision precision = Precision.max,
7381
}) : this._(
74-
Time.microseconds._from(microseconds) +
82+
Time.picoseconds._from(picoseconds) +
83+
Time.nanoseconds._from(nanoseconds) +
84+
Time.microseconds._from(microseconds) +
7585
Time.milliseconds._from(milliseconds) +
7686
Time.seconds._from(seconds) +
7787
Time.minutes._from(minutes) +
7888
Time.hours._from(hours) +
7989
Time.days._from(days),
8090
precision);
8191

92+
/// Constructs a [Time] from a picosecond amount.
93+
Time.ofPicoseconds(final double picoseconds,
94+
{final Precision precision = Precision.max})
95+
: this._(Time.picoseconds._from(picoseconds), precision);
96+
97+
/// Constructs a [Time] from a nanosecond amount.
98+
Time.ofNanoseconds(final double nanoseconds,
99+
{final Precision precision = Precision.max})
100+
: this._(Time.nanoseconds._from(nanoseconds), precision);
101+
82102
/// Constructs a [Time] from a microsecond amount.
83103
Time.ofMicroseconds(final double microseconds,
84104
{final Precision precision = Precision.max})
@@ -117,6 +137,12 @@ class Time extends Measurement<Time> {
117137
/// Note that any granularity below microseconds will be lost.
118138
Duration get asDuration => Duration(microseconds: asMicroseconds.toInt());
119139

140+
/// Interprets this as a number of microseconds.
141+
double get asPicoseconds => _preciseOf(picoseconds);
142+
143+
/// Interprets this as a number of microseconds.
144+
double get asNanoseconds => _preciseOf(nanoseconds);
145+
120146
/// Interprets this as a number of microseconds.
121147
double get asMicroseconds => _preciseOf(microseconds);
122148

test/time_test.dart

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,18 @@ void main() {
155155
hours: 2,
156156
minutes: 3,
157157
seconds: 4,
158-
milliseconds: 5,
159-
microseconds: 6,
160-
precision: Precision(13),
158+
milliseconds: 567,
159+
microseconds: 890,
160+
nanoseconds: 123,
161+
picoseconds: 456,
162+
precision: Precision(18),
161163
);
162164

163165
// when
164166
final result = time.asSeconds;
165167

166168
// then
167-
expect(result, 93784.005006);
169+
expect(result, 93784.567890123456);
168170
});
169171
});
170172
group('ofMicroseconds', () {
@@ -240,6 +242,30 @@ void main() {
240242
});
241243
});
242244

245+
group('asPicoseconds', () {
246+
test('converts to unit', () {
247+
// given
248+
final time = Time.ofSeconds(1.23456, precision: Precision(5));
249+
250+
// when
251+
final result = time.asPicoseconds;
252+
253+
// then
254+
expect(result, 1.2346e12);
255+
});
256+
});
257+
group('asNanoseconds', () {
258+
test('converts to unit', () {
259+
// given
260+
final time = Time.ofSeconds(1.23456, precision: Precision(5));
261+
262+
// when
263+
final result = time.asNanoseconds;
264+
265+
// then
266+
expect(result, 1.2346e9);
267+
});
268+
});
243269
group('asMicroseconds', () {
244270
test('converts to unit', () {
245271
// given
@@ -249,7 +275,7 @@ void main() {
249275
final result = time.asMicroseconds;
250276

251277
// then
252-
expect(result, 1234600.0);
278+
expect(result, 1.2346e6);
253279
});
254280
});
255281
group('asMilliseconds', () {
@@ -261,7 +287,7 @@ void main() {
261287
final result = time.asMilliseconds;
262288

263289
// then
264-
expect(result, 1234.6);
290+
expect(result, 1.2346e3);
265291
});
266292
});
267293
group('asSeconds', () {

0 commit comments

Comments
 (0)