@@ -257,31 +257,12 @@ end
257
257
258
258
# ISO, YMD
259
259
_DEFAULT_TYPE_MAP[:timestamptz ] = ZonedDateTime
260
- const TIMESTAMPTZ_ZDT_FORMATS = (
260
+ const TIMESTAMPTZ_FORMATS = (
261
261
dateformat " y-m-d HH:MM:SSz" ,
262
262
dateformat " y-m-d HH:MM:SS.sz" ,
263
263
dateformat " y-m-d HH:MM:SS.ssz" ,
264
264
dateformat " y-m-d HH:MM:SS.sssz" ,
265
265
)
266
- const TIMESTAMPTZ_UTC_FORMATS = (
267
- dateformat " y-m-d HH:MM:SS" ,
268
- dateformat " y-m-d HH:MM:SS.s" ,
269
- dateformat " y-m-d HH:MM:SS.ss" ,
270
- dateformat " y-m-d HH:MM:SS.sss" ,
271
- )
272
-
273
- timestamptz_formats (:: Type{ZonedDateTime} ) = TIMESTAMPTZ_ZDT_FORMATS
274
- timestamptz_formats (:: Type{UTCDateTime} ) = TIMESTAMPTZ_UTC_FORMATS
275
-
276
- function _pqparse (:: Type{T} , str:: AbstractString ) where T<: Union{UTCDateTime, ZonedDateTime}
277
- formats = timestamptz_formats (T)
278
- for fmt in formats[1 : (end - 1 )]
279
- parsed = tryparse (T, str, fmt)
280
- parsed != = nothing && return parsed
281
- end
282
-
283
- return parse (T, _trunc_seconds (str), formats[end ])
284
- end
285
266
286
267
function pqparse (:: Type{ZonedDateTime} , str:: AbstractString )
287
268
if str == " infinity"
@@ -292,7 +273,12 @@ function pqparse(::Type{ZonedDateTime}, str::AbstractString)
292
273
return ZonedDateTime (typemin (DateTime), tz " UTC" )
293
274
end
294
275
295
- return _pqparse (ZonedDateTime, str)
276
+ for fmt in TIMESTAMPTZ_FORMATS[1 : (end - 1 )]
277
+ parsed = tryparse (ZonedDateTime, str, fmt)
278
+ parsed != = nothing && return parsed
279
+ end
280
+
281
+ return parse (ZonedDateTime, _trunc_seconds (str), TIMESTAMPTZ_FORMATS[end ])
296
282
end
297
283
298
284
function pqparse (:: Type{UTCDateTime} , str:: AbstractString )
@@ -304,11 +290,9 @@ function pqparse(::Type{UTCDateTime}, str::AbstractString)
304
290
return UTCDateTime (typemin (DateTime))
305
291
end
306
292
307
- # Postgres should give us strings ending with +00, +00:00, -00:00
308
- # We use the regex below to strip these character off before parsing, iff,
309
- # the values after the `-`/`+` are `0` or `:`. This means parsing will fail if
310
- # we're asked to parse a non-UTC string like +04:00.
311
- return _pqparse (UTCDateTime, replace (str, r" [-|\+ ][0|:]*$" => " " ))
293
+ # Postgres should always give us strings ending with +00 if our timezone is set to UTC
294
+ # which is the default
295
+ return parse (UTCDateTime, _trunc_seconds (replace (str, " +00" => " " )), TIMESTAMP_FORMAT)
312
296
end
313
297
314
298
_DEFAULT_TYPE_MAP[:date ] = Date
@@ -363,7 +347,7 @@ function Base.parse(::Type{ZonedDateTime}, pqv::PQValue{PQ_SYSTEM_TYPES[:int8]})
363
347
end
364
348
365
349
function Base. parse (:: Type{UTCDateTime} , pqv:: PQValue{PQ_SYSTEM_TYPES[:int8]} )
366
- return UTCDateTime (unix2datetime ( parse (Int64 , pqv) ))
350
+ return UTCDateTime (parse (DateTime , pqv))
367
351
end
368
352
369
353
# All postgresql timestamptz are stored in UTC time with the epoch of 2000-01-01.
@@ -387,16 +371,7 @@ function pqparse(::Type{ZonedDateTime}, ptr::Ptr{UInt8})
387
371
end
388
372
389
373
function pqparse (:: Type{UTCDateTime} , ptr:: Ptr{UInt8} )
390
- value = ntoh (unsafe_load (Ptr {Int64} (ptr)))
391
- if value == typemax (Int64)
392
- depwarn_timetype_inf ()
393
- return UTCDateTime (typemax (DateTime))
394
- elseif value == typemin (Int64)
395
- depwarn_timetype_inf ()
396
- return UTCDateTime (typemin (DateTime))
397
- end
398
- dt = POSTGRES_EPOCH_DATETIME + Microsecond (value)
399
- return UTCDateTime (dt)
374
+ return UTCDateTime (pqparse (DateTime, ptr))
400
375
end
401
376
402
377
function pqparse (:: Type{DateTime} , ptr:: Ptr{UInt8} )
@@ -408,7 +383,7 @@ function pqparse(::Type{DateTime}, ptr::Ptr{UInt8})
408
383
depwarn_timetype_inf ()
409
384
return typemin (DateTime)
410
385
end
411
- return POSTGRES_EPOCH_DATETIME + Microsecond (ntoh ( unsafe_load ( Ptr {Int64} (ptr))) )
386
+ return POSTGRES_EPOCH_DATETIME + Microsecond (value )
412
387
end
413
388
414
389
function pqparse (:: Type{Date} , ptr:: Ptr{UInt8} )
0 commit comments