Add replace for TimeTypes
Supports using Periods or Time to replace fields in a given DateTime.
Addresses part of: https://github.com/JuliaLang/julia/issues/24814
cc: @quinnj
Yeah, I'm inclined to think DateTime(dt::DateTime, p::Period) would be better than replace. The latter is currently only defined for collections, so that wouldn't be very logical. Any drawbacks to using the constructor instead?
If dates and times used properties, you could write this as dt.minute = x.
I'm not a fan of this, since the generic replace function takes old=>new update pairs, which this doesn't. This is more like a non-mutating update, which we do need better APIs, for, but I don't think this is it. The constructor would be reasonable.
Date and time components can be set with the excellent Accessors.jl.
julia> using Dates, Accessors
julia> d = Date(2024, 12, 31)
2024-12-31
julia> @set month(d) = 1
2024-01-31
julia> t = DateTime(2024, 12, 31, 23, 59, 59)
2024-12-31T23:59:59
julia> @set minute(t) = 4
2024-12-31T23:04:59
Great point about Accessors.jl, that's really nice.
My personal view would be that this alleviates the need for this PR -- I also am not happy about its use of replace.