-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
formatter.cr
285 lines (222 loc) · 5.43 KB
/
formatter.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
require "./pattern"
struct Time::Format
# :nodoc:
struct Formatter
include Pattern
getter io : IO
getter time : Time
def initialize(@time : Time, @io : IO)
end
def year : Nil
pad4(time.year, '0')
end
def year_modulo_100 : Nil
pad2(time.year % 100, '0')
end
def year_divided_by_100 : Nil
io << time.year // 100
end
def full_or_short_year : Nil
year
end
def calendar_week_year : Nil
pad4(time.calendar_week[0], '0')
end
def calendar_week_year_modulo100 : Nil
pad2(time.calendar_week[0] % 100, '0')
end
def month : Nil
io << time.month
end
def month_zero_padded : Nil
pad2 time.month, '0'
end
def month_blank_padded : Nil
pad2 time.month, ' '
end
def month_name : Nil
io << get_month_name
end
def month_name_upcase : Nil
io << get_month_name.upcase
end
def short_month_name : Nil
io << get_short_month_name
end
def short_month_name_upcase : Nil
io << get_short_month_name.upcase
end
def calendar_week_week : Nil
pad2(time.calendar_week[1], '0')
end
def day_of_month : Nil
io << time.day
end
def day_of_month_zero_padded : Nil
pad2 time.day, '0'
end
def day_of_month_blank_padded : Nil
pad2 time.day, ' '
end
def day_name : Nil
io << get_day_name
end
def day_name_upcase : Nil
io << get_day_name.upcase
end
def short_day_name : Nil
io << get_short_day_name
end
def short_day_name_upcase : Nil
io << get_short_day_name.upcase
end
def short_day_name_with_comma? : Nil
short_day_name
char ','
whitespace
end
def day_of_year_zero_padded : Nil
pad3 time.day_of_year, '0'
end
def hour_24_zero_padded : Nil
pad2 time.hour, '0'
end
def hour_24_blank_padded : Nil
pad2 time.hour, ' '
end
def hour_12_zero_padded : Nil
h = (time.hour % 12)
pad2 (h == 0 ? 12 : h), '0'
end
def hour_12_blank_padded : Nil
h = (time.hour % 12)
pad2 (h == 0 ? 12 : h), ' '
end
def minute : Nil
pad2 time.minute, '0'
end
def second : Nil
pad2 time.second, '0'
end
def milliseconds : Nil
pad3 time.millisecond, '0'
end
def microseconds : Nil
pad6 time.nanosecond // 1000, '0'
end
def nanoseconds : Nil
pad9 time.nanosecond, '0'
end
def second_fraction : Nil
nanoseconds
end
def second_fraction?(fraction_digits : Int = 9) : Nil
case fraction_digits
when 0
when 3 then char '.'; milliseconds
when 6 then char '.'; microseconds
when 9 then char '.'; nanoseconds
else
raise ArgumentError.new("Invalid fraction digits: #{fraction_digits}")
end
end
def am_pm : Nil
io << (time.hour < 12 ? "am" : "pm")
end
def am_pm_upcase : Nil
io << (time.hour < 12 ? "AM" : "PM")
end
def day_of_week_monday_1_7 : Nil
io << time.day_of_week.value
end
def day_of_week_sunday_0_6 : Nil
io << time.day_of_week.value % 7
end
def unix_seconds : Nil
io << time.to_unix
end
def time_zone(with_seconds = false) : Nil
time_zone_offset(format_seconds: with_seconds)
end
def time_zone_z_or_offset(**options) : Nil
if time.utc?
io << 'Z'
else
time_zone_offset(**options)
end
end
def time_zone_offset(force_colon = false, allow_colon = true, format_seconds = false, parse_seconds = true)
time.zone.format(io, with_colon: force_colon, with_seconds: format_seconds)
end
def time_zone_colon(with_seconds = false) : Nil
time_zone_offset(force_colon: true, format_seconds: with_seconds)
end
def time_zone_colon_with_seconds : Nil
time_zone_colon(with_seconds: true)
end
def time_zone_gmt : Nil
io << "GMT"
end
def time_zone_rfc2822 : Nil
time_zone_offset(allow_colon: false)
end
def time_zone_gmt_or_rfc2822(**options) : Nil
if time.utc? || time.location.name.in?("UT", "GMT")
time_zone_gmt
else
time_zone_rfc2822
end
end
def time_zone_name(zone = false) : Nil
if zone
io << time.zone.name
else
io << time.location
end
end
def char(char, *alternatives) : Nil
io << char
end
def char?(char, *alternatives)
char(char, *alternatives)
end
def whitespace : Nil
io << ' '
end
def get_month_name
MONTH_NAMES[time.month - 1]
end
def get_short_month_name
get_month_name[0, 3]
end
def get_day_name
DAY_NAMES[time.day_of_week.value % 7]
end
def get_short_day_name
get_day_name[0, 3]
end
def pad2(value, padding) : Nil
io << padding if value < 10
io << value
end
def pad3(value, padding) : Nil
io << padding if value < 100
pad2 value, padding
end
def pad4(value, padding) : Nil
io << padding if value < 1000
pad3 value, padding
end
def pad6(value, padding) : Nil
io << padding if value < 100000
io << padding if value < 10000
pad4 value, padding
end
def pad9(value, padding) : Nil
io << padding if value < 100000000
io << padding if value < 10000000
io << padding if value < 1000000
pad6 value, padding
end
end
end