@@ -175,17 +175,17 @@ Delim(d::Char) = Delim{Char, 1}(d)
175
175
Delim (d:: String ) = Delim {String, length(d)} (d)
176
176
177
177
@inline function tryparsenext {N} (d:: Delim{Char, N} , str, i:: Int , len)
178
- R = Nullable{Int64 }
178
+ R = Nullable{Bool }
179
179
for j= 1 : N
180
180
i > len && return (R (), i)
181
181
c, i = next (str, i)
182
182
c != d. d && return (R (), i)
183
183
end
184
- return R (0 ), i
184
+ return R (true ), i
185
185
end
186
186
187
187
@inline function tryparsenext {N} (d:: Delim{String, N} , str, i:: Int , len)
188
- R = Nullable{Int64 }
188
+ R = Nullable{Bool }
189
189
i1 = i
190
190
i2 = start (d. d)
191
191
for j = 1 : N
@@ -198,15 +198,15 @@ end
198
198
return R (), i1
199
199
end
200
200
end
201
- return R (0 ), i1
201
+ return R (true ), i1
202
202
end
203
203
204
204
@inline function format (io, d:: Delim , dt, locale)
205
205
write (io, d. d)
206
206
end
207
207
208
208
function _show_content {N} (io:: IO , d:: Delim{Char, N} )
209
- if d. d in keys (SLOT_RULE )
209
+ if d. d in keys (CONVERSION_SPECIFIERS )
210
210
for i = 1 : N
211
211
write (io, ' \\ ' , d. d)
212
212
end
219
219
220
220
function _show_content (io:: IO , d:: Delim )
221
221
for c in d. d
222
- if c in keys (SLOT_RULE )
222
+ if c in keys (CONVERSION_SPECIFIERS )
223
223
write (io, ' \\ ' )
224
224
end
225
225
write (io, c)
236
236
237
237
abstract type DayOfWeekToken end # special addition to Period types
238
238
239
- # mapping format specifiers to period types
240
- const SLOT_RULE = Dict {Char, Type} (
239
+ # Map conversion specifiers or character codes to tokens.
240
+ # Note: Allow addition of new character codes added by packages
241
+ const CONVERSION_SPECIFIERS = Dict {Char, Type} (
241
242
' y' => Year,
242
243
' Y' => Year,
243
244
' m' => Month,
@@ -252,13 +253,26 @@ const SLOT_RULE = Dict{Char, Type}(
252
253
' s' => Millisecond,
253
254
)
254
255
255
- slot_order (:: Type{Date} ) = (Year, Month, Day)
256
- slot_order (:: Type{DateTime} ) = (Year, Month, Day, Hour, Minute, Second, Millisecond)
257
-
258
- slot_defaults (:: Type{Date} ) = map (Int64, (1 , 1 , 1 ))
259
- slot_defaults (:: Type{DateTime} ) = map (Int64, (1 , 1 , 1 , 0 , 0 , 0 , 0 ))
256
+ # Default values are needed when a conversion specifier is used in a DateFormat for parsing
257
+ # and we have reached the end of the input string.
258
+ # Note: Allow `Any` value as a default to support extensibility
259
+ const CONVERSION_DEFAULTS = Dict {Type, Any} (
260
+ Year => Int64 (1 ),
261
+ Month => Int64 (1 ),
262
+ DayOfWeekToken => Int64 (0 ),
263
+ Day => Int64 (1 ),
264
+ Hour => Int64 (0 ),
265
+ Minute => Int64 (0 ),
266
+ Second => Int64 (0 ),
267
+ Millisecond => Int64 (0 ),
268
+ )
260
269
261
- slot_types {T<:TimeType} (:: Type{T} ) = typeof (slot_defaults (T))
270
+ # Specifies the required fields in order to parse a TimeType
271
+ # Note: Allows for addition of new TimeTypes
272
+ const CONVERSION_TRANSLATIONS = Dict {Type{<:TimeType}, Tuple} (
273
+ Date => (Year, Month, Day),
274
+ DateTime => (Year, Month, Day, Hour, Minute, Second, Millisecond),
275
+ )
262
276
263
277
"""
264
278
DateFormat(format::AbstractString, locale="english") -> DateFormat
@@ -300,13 +314,13 @@ function DateFormat(f::AbstractString, locale::DateLocale=ENGLISH)
300
314
prev = ()
301
315
prev_offset = 1
302
316
303
- letters = String (collect (keys (Base . Dates . SLOT_RULE )))
317
+ letters = String (collect (keys (CONVERSION_SPECIFIERS )))
304
318
for m in eachmatch (Regex (" (?<!\\\\ )([\\ Q$letters \\ E])\\ 1*" ), f)
305
319
tran = replace (f[prev_offset: m. offset - 1 ], r" \\ (.)" , s "\1 " )
306
320
307
321
if ! isempty (prev)
308
322
letter, width = prev
309
- typ = SLOT_RULE [letter]
323
+ typ = CONVERSION_SPECIFIERS [letter]
310
324
311
325
push! (tokens, DatePart {letter} (width, isempty (tran)))
312
326
end
@@ -326,7 +340,7 @@ function DateFormat(f::AbstractString, locale::DateLocale=ENGLISH)
326
340
327
341
if ! isempty (prev)
328
342
letter, width = prev
329
- typ = SLOT_RULE [letter]
343
+ typ = CONVERSION_SPECIFIERS [letter]
330
344
331
345
push! (tokens, DatePart {letter} (width, false ))
332
346
end
@@ -368,6 +382,9 @@ const ISODateTimeFormat = DateFormat("yyyy-mm-dd\\THH:MM:SS.s")
368
382
const ISODateFormat = DateFormat (" yyyy-mm-dd" )
369
383
const RFC1123Format = DateFormat (" e, dd u yyyy HH:MM:SS" )
370
384
385
+ default_format (:: Type{DateTime} ) = ISODateTimeFormat
386
+ default_format (:: Type{Date} ) = ISODateFormat
387
+
371
388
# ## API
372
389
"""
373
390
DateTime(dt::AbstractString, format::AbstractString; locale="english") -> DateTime
0 commit comments