Skip to content

Commit 652c3a6

Browse files
committed
Support for additional time format for ZonedDateTime
1 parent 10f5b7e commit 652c3a6

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PostgresORM"
22
uuid = "748b5efa-ed57-4836-b183-a38105a77fdd"
33
authors = ["Vincent Laugier <vincent.laugier@gmail.com>"]
4-
version = "0.4.0"
4+
version = "0.5.0"
55

66
[deps]
77
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

src/PostgresORMUtil/utils.jl

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ function vector_of_integers2vector_of_enums(
179179
end
180180

181181
function string2zoneddatetime(str)
182-
# eg. "2019-09-03T11:00:00.000Z"
182+
183+
# Attempt 1: eg. "2019-09-03T11:00:00.000Z"
183184
date_match_GMT =
184185
match(r"^([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}Z)$", str)
185-
186186
if !isnothing(date_match_GMT)
187187
# "2019-07-24T00:41:49.732Z" becomes "2019-07-24T00:41:49.732"
188188
date_match_remove_endingZ =
@@ -192,10 +192,28 @@ function string2zoneddatetime(str)
192192
ZonedDateTime(DateTime(date_match_remove_endingZ.match),
193193
TimeZone("UTC"))
194194
return utc_zdt
195-
else
196-
return nothing
197195
end
198196

197+
# Attempt 2: eg. "2022-04-09T18:06:26+02:00"
198+
date_match_with_tz1 =
199+
match(r"^([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{2}:[0-9]{2})$", str)
200+
if !isnothing(date_match_with_tz1)
201+
zdt =
202+
ZonedDateTime(date_match_with_tz1.match,"yyyy-mm-ddTHH:MM:SSzzzz")
203+
return zdt
204+
end
205+
206+
# Attempt 3: eg. "2022-04-08T02:30:22.668+02:00"
207+
date_match_with_tz1 =
208+
match(r"^([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3}\+[0-9]{2}:[0-9]{2})$", str)
209+
if !isnothing(date_match_with_tz1)
210+
zdt =
211+
ZonedDateTime(date_match_with_tz1.match,"yyyy-mm-ddTHH:MM:SS.ssszzzz")
212+
return zdt
213+
end
214+
215+
return nothing
216+
199217
end
200218

201219
function int2enum(enumType::DataType,enum_int::Missing)

test/PostgresORMUtil/runtests-utils.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ end
9292
end
9393

9494
@testset "Test utils.jl - function `string2zoneddatetime`" begin
95-
PostgresORMUtil.string2zoneddatetime("2019-09-03T11:00:00.000Z")
95+
PostgresORMUtil.string2zoneddatetime("2019-09-03T11:00:00.000Z") |> string |> ZonedDateTime
96+
PostgresORMUtil.string2zoneddatetime("2022-04-09T18:06:26+01:30")
97+
PostgresORMUtil.string2zoneddatetime("2022-04-08T02:30:22.668+03:00")
9698
end
9799

98100
@testset "Test utils.jl - function `postgresql_string_array_2_string_vector`" begin

test/runtests-prerequisite.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ using PostgresORM
1313
using PostgresORM.PostgresORMUtil
1414
using PostgresORM.PostgresORMUtil.Pluralize
1515
using PostgresORM.Tool
16+
using TimeZones

0 commit comments

Comments
 (0)