2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
+ import '../../datetime_format.dart' ;
6
+ import '../bindings/lib.g.dart' as icu;
5
7
import '../data.dart' ;
8
+ import '../data_4x.dart' ;
6
9
import '../locale/locale.dart' ;
10
+ import '../locale/locale_4x.dart' ;
7
11
import 'datetime_format_impl.dart' ;
8
- import 'datetime_format_options.dart' ;
9
12
10
13
DateTimeFormatImpl getDateTimeFormatter4X (
11
14
Locale locale,
@@ -15,10 +18,143 @@ DateTimeFormatImpl getDateTimeFormatter4X(
15
18
DateTimeFormat4X (locale, data, options);
16
19
17
20
class DateTimeFormat4X extends DateTimeFormatImpl {
18
- DateTimeFormat4X (super .locale, Data data, super .options);
21
+ final icu.DateTimeFormatter ? _dateTimeFormatter;
22
+ final icu.DateFormatter ? _dateFormatter;
23
+ final icu.TimeFormatter ? _timeFormatter;
24
+ final icu.ZonedDateTimeFormatter ? _zonedDateTimeFormatter;
25
+ final icu.DataProvider _data;
26
+
27
+ DateTimeFormat4X (super .locale, Data data, super .options)
28
+ : _data = data.to4X (),
29
+ _dateTimeFormatter = _setDateTimeFormatter (options, data, locale),
30
+ _timeFormatter = options.timeFormatStyle != null
31
+ ? icu.TimeFormatter .withLength (
32
+ data.to4X (),
33
+ locale.to4X (),
34
+ options.dateFormatStyle? .timeTo4xOptions () ??
35
+ icu.TimeLength .short,
36
+ )
37
+ : null ,
38
+ _dateFormatter = _setDateFormatter (options, data, locale),
39
+ _zonedDateTimeFormatter = options.timeZone != null
40
+ ? icu.ZonedDateTimeFormatter .withLengths (
41
+ data.to4X (),
42
+ locale.to4X (),
43
+ options.dateFormatStyle? .dateTo4xOptions () ??
44
+ icu.DateLength .short, //TODO: Check defaults
45
+ options.timeFormatStyle? .timeTo4xOptions () ??
46
+ icu.TimeLength .short, //TODO: Check defaults
47
+ )
48
+ : null ;
49
+
50
+ static icu.DateTimeFormatter ? _setDateTimeFormatter (
51
+ DateTimeFormatOptions options,
52
+ Data data,
53
+ Locale locale,
54
+ ) {
55
+ final dateFormatStyle = options.dateFormatStyle;
56
+ final timeFormatStyle = options.timeFormatStyle;
57
+
58
+ if (dateFormatStyle == null || timeFormatStyle == null ) {
59
+ return null ;
60
+ }
61
+
62
+ return icu.DateTimeFormatter .withLengths (
63
+ data.to4X (),
64
+ locale.to4X (),
65
+ dateFormatStyle.dateTo4xOptions (),
66
+ timeFormatStyle.timeTo4xOptions (),
67
+ );
68
+ }
69
+
70
+ static icu.DateFormatter ? _setDateFormatter (
71
+ DateTimeFormatOptions options,
72
+ Data data,
73
+ Locale locale,
74
+ ) {
75
+ final dateFormatStyle = options.dateFormatStyle;
76
+ final timeFormatStyle = options.timeFormatStyle;
77
+
78
+ if (dateFormatStyle == null && timeFormatStyle != null ) {
79
+ return null ;
80
+ }
81
+
82
+ return icu.DateFormatter .withLength (
83
+ data.to4X (),
84
+ locale.to4X (),
85
+ dateFormatStyle? .dateTo4xOptions () ?? icu.DateLength .short,
86
+ );
87
+ }
19
88
20
89
@override
21
90
String formatImpl (DateTime datetime) {
22
- throw UnimplementedError ('Insert diplomat bindings here' );
91
+ final calendarKind = options.calendar? .to4x () ?? icu.AnyCalendarKind .iso;
92
+ final isoDateTime = icu.DateTime .fromIsoInCalendar (
93
+ datetime.year,
94
+ datetime.month,
95
+ datetime.day,
96
+ datetime.hour,
97
+ datetime.minute,
98
+ datetime.second,
99
+ datetime.microsecond * 1000 ,
100
+ icu.Calendar .forKind (_data, calendarKind),
101
+ );
102
+ if (_zonedDateTimeFormatter != null ) {
103
+ final ianaToBcp47Mapper = icu.IanaToBcp47Mapper (_data);
104
+ final timeZone = icu.CustomTimeZone .empty ()
105
+ ..trySetIanaTimeZoneId (ianaToBcp47Mapper, options.timeZone! );
106
+ return _zonedDateTimeFormatter.formatDatetimeWithCustomTimeZone (
107
+ isoDateTime,
108
+ timeZone,
109
+ );
110
+ } else if (_dateTimeFormatter != null ) {
111
+ return _dateTimeFormatter.formatDatetime (isoDateTime);
112
+ } else if (_dateFormatter != null ) {
113
+ return _dateFormatter.formatDatetime (isoDateTime);
114
+ } else if (_timeFormatter != null ) {
115
+ return _timeFormatter.formatDatetime (isoDateTime);
116
+ } else {
117
+ throw UnimplementedError (
118
+ 'Custom skeletons are not yet supported in ICU4X. '
119
+ 'Either date or time formatting has to be enabled.' );
120
+ }
23
121
}
24
122
}
123
+
124
+ extension on TimeFormatStyle {
125
+ icu.TimeLength timeTo4xOptions () => switch (this ) {
126
+ TimeFormatStyle .full => icu.TimeLength .full,
127
+ TimeFormatStyle .long => icu.TimeLength .long,
128
+ TimeFormatStyle .medium => icu.TimeLength .medium,
129
+ TimeFormatStyle .short => icu.TimeLength .short,
130
+ };
131
+ icu.DateLength dateTo4xOptions () => switch (this ) {
132
+ TimeFormatStyle .full => icu.DateLength .full,
133
+ TimeFormatStyle .long => icu.DateLength .long,
134
+ TimeFormatStyle .medium => icu.DateLength .medium,
135
+ TimeFormatStyle .short => icu.DateLength .short,
136
+ };
137
+ }
138
+
139
+ extension on Calendar {
140
+ icu.AnyCalendarKind to4x () => switch (this ) {
141
+ Calendar .buddhist => icu.AnyCalendarKind .buddhist,
142
+ Calendar .chinese => icu.AnyCalendarKind .chinese,
143
+ Calendar .coptic => icu.AnyCalendarKind .coptic,
144
+ Calendar .dangi => icu.AnyCalendarKind .dangi,
145
+ Calendar .ethioaa => icu.AnyCalendarKind .ethiopianAmeteAlem,
146
+ Calendar .ethiopic => icu.AnyCalendarKind .ethiopian,
147
+ Calendar .gregory => icu.AnyCalendarKind .gregorian,
148
+ Calendar .hebrew => icu.AnyCalendarKind .hebrew,
149
+ Calendar .indian => icu.AnyCalendarKind .indian,
150
+ Calendar .islamic => icu.AnyCalendarKind .islamicObservational,
151
+ Calendar .islamicUmalqura => icu.AnyCalendarKind .islamicUmmAlQura,
152
+ Calendar .islamicTbla => icu.AnyCalendarKind .islamicTabular,
153
+ Calendar .islamicCivil => icu.AnyCalendarKind .islamicCivil,
154
+ Calendar .islamicRgsa => icu.AnyCalendarKind .islamicObservational,
155
+ Calendar .iso8601 => icu.AnyCalendarKind .iso,
156
+ Calendar .japanese => icu.AnyCalendarKind .japanese,
157
+ Calendar .persian => icu.AnyCalendarKind .persian,
158
+ Calendar .roc => icu.AnyCalendarKind .roc,
159
+ };
160
+ }
0 commit comments