Skip to content

Commit 0b31ce8

Browse files
committed
Fix 24H to 12H time format conversion in timeOfDayToString()
1 parent 2ef5c6e commit 0b31ce8

File tree

4 files changed

+137
-23
lines changed

4 files changed

+137
-23
lines changed

lib/utils.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,25 @@ String openingHoursToString(OpeningHours hours) {
3131

3232
/// Convert [TimeOfDay] to [String] for display.
3333
/// Truncates ":00" if minute is 0
34+
/// For the algorithm to convert from 24H to 12H times,
35+
/// see https://www.wikihow.com/Convert-from-24-Hour-to-12-Hour-Time
3436
String timeOfDayToString(TimeOfDay time) {
3537
String _addLeadingZeroIfNeeded(int value) {
3638
if (value < 10) return '0$value';
3739
return value.toString();
3840
}
3941

42+
String minuteToString(int minutes) =>
43+
(time.minute == 0 ? "" : ":" + _addLeadingZeroIfNeeded(time.minute));
44+
4045
if (time.hour > 12) {
41-
return (time.hour - 12).toString() +
42-
(time.minute == 0 ? "" : ":" + _addLeadingZeroIfNeeded(time.minute)) +
43-
"PM";
46+
return (time.hour - 12).toString() + minuteToString(time.minute) + "PM";
47+
} else if (time.hour == 12) {
48+
return time.hour.toString() + minuteToString(time.minute) + "PM";
49+
} else if (time.hour == 0) {
50+
return "12" + minuteToString(time.minute) + "AM";
4451
} else {
45-
return time.hour.toString() +
46-
(time.minute == 0 ? "" : ":" + _addLeadingZeroIfNeeded(time.minute)) +
47-
"AM";
52+
return time.hour.toString() + minuteToString(time.minute) + "AM";
4853
}
4954
}
5055

pubspec.lock

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ packages:
77
name: _fe_analyzer_shared
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "36.0.0"
10+
version: "31.0.0"
1111
analyzer:
1212
dependency: transitive
1313
description:
1414
name: analyzer
1515
url: "https://pub.dartlang.org"
1616
source: hosted
17-
version: "3.3.1"
17+
version: "2.8.0"
1818
archive:
1919
dependency: transitive
2020
description:
@@ -49,7 +49,7 @@ packages:
4949
name: build
5050
url: "https://pub.dartlang.org"
5151
source: hosted
52-
version: "2.2.1"
52+
version: "2.3.0"
5353
build_config:
5454
dependency: transitive
5555
description:
@@ -63,7 +63,7 @@ packages:
6363
name: build_daemon
6464
url: "https://pub.dartlang.org"
6565
source: hosted
66-
version: "3.0.1"
66+
version: "3.1.0"
6767
build_resolvers:
6868
dependency: transitive
6969
description:
@@ -77,7 +77,7 @@ packages:
7777
name: build_runner
7878
url: "https://pub.dartlang.org"
7979
source: hosted
80-
version: "2.1.7"
80+
version: "2.1.10"
8181
build_runner_core:
8282
dependency: transitive
8383
description:
@@ -98,7 +98,7 @@ packages:
9898
name: built_value
9999
url: "https://pub.dartlang.org"
100100
source: hosted
101-
version: "8.1.4"
101+
version: "8.2.0"
102102
characters:
103103
dependency: transitive
104104
description:
@@ -120,6 +120,13 @@ packages:
120120
url: "https://pub.dartlang.org"
121121
source: hosted
122122
version: "2.0.1"
123+
cli_util:
124+
dependency: transitive
125+
description:
126+
name: cli_util
127+
url: "https://pub.dartlang.org"
128+
source: hosted
129+
version: "0.3.5"
123130
clock:
124131
dependency: transitive
125132
description:
@@ -148,6 +155,13 @@ packages:
148155
url: "https://pub.dartlang.org"
149156
source: hosted
150157
version: "3.0.1"
158+
coverage:
159+
dependency: transitive
160+
description:
161+
name: coverage
162+
url: "https://pub.dartlang.org"
163+
source: hosted
164+
version: "1.0.3"
151165
crypto:
152166
dependency: transitive
153167
description:
@@ -161,7 +175,7 @@ packages:
161175
name: dart_style
162176
url: "https://pub.dartlang.org"
163177
source: hosted
164-
version: "2.2.2"
178+
version: "2.2.1"
165179
fake_async:
166180
dependency: transitive
167181
description:
@@ -224,7 +238,7 @@ packages:
224238
name: flutter_native_splash
225239
url: "https://pub.dartlang.org"
226240
source: hosted
227-
version: "2.1.3+1"
241+
version: "2.1.5"
228242
flutter_test:
229243
dependency: "direct dev"
230244
description: flutter
@@ -241,7 +255,7 @@ packages:
241255
name: freezed
242256
url: "https://pub.dartlang.org"
243257
source: hosted
244-
version: "1.1.1"
258+
version: "1.1.0"
245259
freezed_annotation:
246260
dependency: "direct main"
247261
description:
@@ -269,14 +283,14 @@ packages:
269283
name: geolocator_android
270284
url: "https://pub.dartlang.org"
271285
source: hosted
272-
version: "3.1.5"
286+
version: "3.1.6"
273287
geolocator_apple:
274288
dependency: transitive
275289
description:
276290
name: geolocator_apple
277291
url: "https://pub.dartlang.org"
278292
source: hosted
279-
version: "2.1.1+1"
293+
version: "2.1.2"
280294
geolocator_platform_interface:
281295
dependency: transitive
282296
description:
@@ -381,7 +395,14 @@ packages:
381395
name: json_serializable
382396
url: "https://pub.dartlang.org"
383397
source: hosted
384-
version: "6.1.5"
398+
version: "6.1.6"
399+
lint:
400+
dependency: transitive
401+
description:
402+
name: lint
403+
url: "https://pub.dartlang.org"
404+
source: hosted
405+
version: "1.8.2"
385406
lints:
386407
dependency: transitive
387408
description:
@@ -424,6 +445,13 @@ packages:
424445
url: "https://pub.dartlang.org"
425446
source: hosted
426447
version: "1.0.1"
448+
node_preamble:
449+
dependency: transitive
450+
description:
451+
name: node_preamble
452+
url: "https://pub.dartlang.org"
453+
source: hosted
454+
version: "2.0.1"
427455
package_config:
428456
dependency: transitive
429457
description:
@@ -598,7 +626,21 @@ packages:
598626
name: shelf
599627
url: "https://pub.dartlang.org"
600628
source: hosted
601-
version: "1.2.0"
629+
version: "1.3.0"
630+
shelf_packages_handler:
631+
dependency: transitive
632+
description:
633+
name: shelf_packages_handler
634+
url: "https://pub.dartlang.org"
635+
source: hosted
636+
version: "3.0.0"
637+
shelf_static:
638+
dependency: transitive
639+
description:
640+
name: shelf_static
641+
url: "https://pub.dartlang.org"
642+
source: hosted
643+
version: "1.1.0"
602644
shelf_web_socket:
603645
dependency: transitive
604646
description:
@@ -617,14 +659,28 @@ packages:
617659
name: source_gen
618660
url: "https://pub.dartlang.org"
619661
source: hosted
620-
version: "1.2.1"
662+
version: "1.2.2"
621663
source_helper:
622664
dependency: transitive
623665
description:
624666
name: source_helper
625667
url: "https://pub.dartlang.org"
626668
source: hosted
627-
version: "1.3.1"
669+
version: "1.3.2"
670+
source_map_stack_trace:
671+
dependency: transitive
672+
description:
673+
name: source_map_stack_trace
674+
url: "https://pub.dartlang.org"
675+
source: hosted
676+
version: "2.1.0"
677+
source_maps:
678+
dependency: transitive
679+
description:
680+
name: source_maps
681+
url: "https://pub.dartlang.org"
682+
source: hosted
683+
version: "0.10.10"
628684
source_span:
629685
dependency: transitive
630686
description:
@@ -674,13 +730,27 @@ packages:
674730
url: "https://pub.dartlang.org"
675731
source: hosted
676732
version: "1.2.0"
733+
test:
734+
dependency: "direct dev"
735+
description:
736+
name: test
737+
url: "https://pub.dartlang.org"
738+
source: hosted
739+
version: "1.19.5"
677740
test_api:
678741
dependency: transitive
679742
description:
680743
name: test_api
681744
url: "https://pub.dartlang.org"
682745
source: hosted
683746
version: "0.4.8"
747+
test_core:
748+
dependency: transitive
749+
description:
750+
name: test_core
751+
url: "https://pub.dartlang.org"
752+
source: hosted
753+
version: "0.4.9"
684754
time_range_picker:
685755
dependency: "direct main"
686756
description:
@@ -716,6 +786,13 @@ packages:
716786
url: "https://pub.dartlang.org"
717787
source: hosted
718788
version: "2.1.1"
789+
vm_service:
790+
dependency: transitive
791+
description:
792+
name: vm_service
793+
url: "https://pub.dartlang.org"
794+
source: hosted
795+
version: "7.5.0"
719796
watcher:
720797
dependency: transitive
721798
description:
@@ -730,13 +807,20 @@ packages:
730807
url: "https://pub.dartlang.org"
731808
source: hosted
732809
version: "2.1.0"
810+
webkit_inspection_protocol:
811+
dependency: transitive
812+
description:
813+
name: webkit_inspection_protocol
814+
url: "https://pub.dartlang.org"
815+
source: hosted
816+
version: "1.0.0"
733817
win32:
734818
dependency: transitive
735819
description:
736820
name: win32
737821
url: "https://pub.dartlang.org"
738822
source: hosted
739-
version: "2.4.1"
823+
version: "2.5.1"
740824
xdg_directories:
741825
dependency: transitive
742826
description:

pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ dev_dependencies:
2525
sdk: flutter
2626
flutter_lints: ^1.0.0
2727
build_runner:
28-
freezed: ^1.1.1
28+
freezed:
2929
flutter_launcher_icons: "^0.9.2"
3030
flutter_launcher_name:
3131
git:
3232
url: https://github.com/AlienKevin/flutter_launcher_name
3333
ref: "0.11.1"
34+
test:
3435

3536
flutter:
3637
uses-material-design: false

test/utils_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:mstudy/utils.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
test('timeOfDayToString', () {
7+
expect(timeOfDayToString(const TimeOfDay(hour: 0, minute: 0)), "12AM");
8+
expect(timeOfDayToString(const TimeOfDay(hour: 0, minute: 59)), "12:59AM");
9+
10+
expect(timeOfDayToString(const TimeOfDay(hour: 1, minute: 0)), "1AM");
11+
expect(timeOfDayToString(const TimeOfDay(hour: 8, minute: 0)), "8AM");
12+
expect(timeOfDayToString(const TimeOfDay(hour: 9, minute: 30)), "9:30AM");
13+
expect(timeOfDayToString(const TimeOfDay(hour: 11, minute: 59)), "11:59AM");
14+
15+
expect(timeOfDayToString(const TimeOfDay(hour: 12, minute: 0)), "12PM");
16+
expect(timeOfDayToString(const TimeOfDay(hour: 12, minute: 59)), "12:59PM");
17+
18+
expect(timeOfDayToString(const TimeOfDay(hour: 13, minute: 0)), "1PM");
19+
expect(timeOfDayToString(const TimeOfDay(hour: 14, minute: 30)), "2:30PM");
20+
expect(timeOfDayToString(const TimeOfDay(hour: 20, minute: 0)), "8PM");
21+
expect(timeOfDayToString(const TimeOfDay(hour: 22, minute: 59)), "10:59PM");
22+
expect(timeOfDayToString(const TimeOfDay(hour: 23, minute: 59)), "11:59PM");
23+
});
24+
}

0 commit comments

Comments
 (0)