• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import 'package:flutter_tools/src/base/time.dart';
6
7import '../src/common.dart';
8
9void main() {
10  group(SystemClock, () {
11    test('can set a fixed time', () {
12      final SystemClock clock = SystemClock.fixed(DateTime(1991, 8, 23));
13      expect(clock.now(), DateTime(1991, 8, 23));
14    });
15
16    test('can find a time ago', () {
17      final SystemClock clock = SystemClock.fixed(DateTime(1991, 8, 23));
18      expect(clock.ago(const Duration(days: 10)), DateTime(1991, 8, 13));
19    });
20  });
21
22  group('formatting', () {
23    test('can round-trip formatted time', () {
24      final DateTime time = DateTime(1991, 7, 31);
25      expect(time.isUtc, isFalse);
26      // formatDateTime() adds a timezone offset to DateTime.toString().
27      final String formattedTime = formatDateTime(time);
28      // If a date time string has a timezone offset, DateTime.tryParse()
29      // converts the parsed time to UTC.
30      final DateTime parsedTime = DateTime.tryParse(formattedTime);
31      expect(parsedTime, isNotNull);
32      expect(parsedTime.isUtc, isTrue);
33      // Convert the parsed time (which should be utc) to the local timezone and
34      // compare against the original time which is in the local timezone. They
35      // should be the same.
36      expect(parsedTime.toLocal(), equals(time));
37    });
38  });
39}
40