From d36e9c3be3947ccb7ac3851295d8041ce091c98e Mon Sep 17 00:00:00 2001 From: Fabian Gans Date: Thu, 30 Oct 2025 15:46:04 +0100 Subject: [PATCH 1/4] fix datetime concversion bug --- src/DatasetAPI/Datasets.jl | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/DatasetAPI/Datasets.jl b/src/DatasetAPI/Datasets.jl index 8ad7112ef..681ea867d 100644 --- a/src/DatasetAPI/Datasets.jl +++ b/src/DatasetAPI/Datasets.jl @@ -3,10 +3,10 @@ module Datasets import ..Cubes: Cubes, YAXArray, concatenatecubes, CleanMe, subsetcube, copy_diskarray, setchunks, caxes, readcubedata, cubesize, formatbytes using ...YAXArrays: YAXArrays, YAXDefaults, findAxis using DataStructures: OrderedDict, counter -using Dates: Day, Hour, Minute, Second, Month, Year, Date, DateTime, TimeType, AbstractDateTime, Period +using Dates: Day, Hour, Minute, Second, Month, Year, Date, DateTime, TimeType, AbstractDateTime, Period, Dates using Statistics: mean using IntervalSets: Interval, (..) -using CFTime: timedecode, timeencode, DateTimeNoLeap, DateTime360Day, DateTimeAllLeap +using CFTime: timedecode, timeencode, DateTimeNoLeap, DateTime360Day, DateTimeAllLeap, CFTime using YAXArrayBase using YAXArrayBase: iscontdimval, add_var using DiskArrayTools: CFDiskArray, diskstack @@ -273,6 +273,12 @@ function collectdims(g) outd end +function round_datetime(dt::CFTime.DateTimeProlepticGregorian) + origin = CFTime._origin_period(dt) + ms = Dates.Millisecond(round(Int, (dt.instant + origin + CFTime.DATETIME_OFFSET).duration)) + return DateTime(CFTime.UTInstant{Dates.Millisecond}(ms)) +end + function toaxis(dimname, g, offs, len) axname = Symbol(dimname) if !haskey(g, dimname) @@ -282,7 +288,9 @@ function toaxis(dimname, g, offs, len) aratts = get_var_attrs(g, dimname) if match(r"^(days)|(hours)|(seconds)|(months) since",lowercase(get(aratts,"units",""))) !== nothing tsteps = try - timedecode(ar[:], aratts["units"], lowercase(get(aratts, "calendar", "standard"))) + dec = timedecode(ar[:], aratts["units"], lowercase(get(aratts, "calendar", "standard")), prefer_datetime=false) + + round_datetime.(dec) catch ar[:] end From 547cc3921c07e239f875a57c3e4dd5df0ceb4b48 Mon Sep 17 00:00:00 2001 From: Fabian Gans Date: Thu, 30 Oct 2025 17:39:18 +0100 Subject: [PATCH 2/4] make it work for all calendars --- src/DatasetAPI/Datasets.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/DatasetAPI/Datasets.jl b/src/DatasetAPI/Datasets.jl index 681ea867d..28587a144 100644 --- a/src/DatasetAPI/Datasets.jl +++ b/src/DatasetAPI/Datasets.jl @@ -273,7 +273,7 @@ function collectdims(g) outd end -function round_datetime(dt::CFTime.DateTimeProlepticGregorian) +function round_datetime(dt) origin = CFTime._origin_period(dt) ms = Dates.Millisecond(round(Int, (dt.instant + origin + CFTime.DATETIME_OFFSET).duration)) return DateTime(CFTime.UTInstant{Dates.Millisecond}(ms)) @@ -289,7 +289,6 @@ function toaxis(dimname, g, offs, len) if match(r"^(days)|(hours)|(seconds)|(months) since",lowercase(get(aratts,"units",""))) !== nothing tsteps = try dec = timedecode(ar[:], aratts["units"], lowercase(get(aratts, "calendar", "standard")), prefer_datetime=false) - round_datetime.(dec) catch ar[:] From 712a1411b2cee4068645912ea00013bc0a2e6dc8 Mon Sep 17 00:00:00 2001 From: Felix Cremer Date: Mon, 3 Nov 2025 11:53:48 +0100 Subject: [PATCH 3/4] Only round if we want to convert to datetime --- src/DatasetAPI/Datasets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DatasetAPI/Datasets.jl b/src/DatasetAPI/Datasets.jl index 28587a144..0ee84eba2 100644 --- a/src/DatasetAPI/Datasets.jl +++ b/src/DatasetAPI/Datasets.jl @@ -289,7 +289,7 @@ function toaxis(dimname, g, offs, len) if match(r"^(days)|(hours)|(seconds)|(months) since",lowercase(get(aratts,"units",""))) !== nothing tsteps = try dec = timedecode(ar[:], aratts["units"], lowercase(get(aratts, "calendar", "standard")), prefer_datetime=false) - round_datetime.(dec) + prefer_datetime && round_datetime.(dec) catch ar[:] end From afd160e2fbecc0c87c0da73c4ce9f3c86b104880 Mon Sep 17 00:00:00 2001 From: Fabian Gans Date: Mon, 3 Nov 2025 15:01:39 +0100 Subject: [PATCH 4/4] Update src/DatasetAPI/Datasets.jl --- src/DatasetAPI/Datasets.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DatasetAPI/Datasets.jl b/src/DatasetAPI/Datasets.jl index 0ee84eba2..28587a144 100644 --- a/src/DatasetAPI/Datasets.jl +++ b/src/DatasetAPI/Datasets.jl @@ -289,7 +289,7 @@ function toaxis(dimname, g, offs, len) if match(r"^(days)|(hours)|(seconds)|(months) since",lowercase(get(aratts,"units",""))) !== nothing tsteps = try dec = timedecode(ar[:], aratts["units"], lowercase(get(aratts, "calendar", "standard")), prefer_datetime=false) - prefer_datetime && round_datetime.(dec) + round_datetime.(dec) catch ar[:] end