Skip to content

Conversation

@oxinabox
Copy link
Contributor

I got really tired of writing Date(DateTime(zdt)) and ZonedDateTime(DateTime(date))

Fixes #222 and #209

julia> zdt = ZonedDateTime(Date(2011, 6, 1), tz"America/Winnipeg")
2011-06-01T00:00:00-05:00

julia> Date(zdt)
2011-06-01

julia> DateTime(Date(2011, 6, 1))
2011-06-01T00:00:00

Returns an equivalent `Date` without any `TimeZone`, or time information.
"""
Dates.Date(zdt::ZonedDateTime) = Date(DateTime(zdt))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been intending on dropping DateTime(::ZonedDateTime) in favor of localtime(::Type{DateTime}, ::ZonedDateTime) or something similar. This is definitely a lossy operation and it's important to communicate the implicit time zone.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would really dislike that.
Often I just have a collection of DateTime,
one per day.
and I want to plot them (cf #184),
or I want to display them in a table,
and I just want to get rid of extra information.
I don't actually care about the time or even the exact date often.

I think it pretty clear to the user that it is losing information.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at that issue again Date(zdt, UTC) / Date(zdt, Local) isn't bad

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy to see this change when we resolve #125 for DateTimes.
Til then this match the API we have

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't want to take on the change yourself I can make the remainder of the changes. I don't think I would merge this as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do want to merge as is, because the status quo of matching how DateTime works is known.
All other things are not known and we might want to do a little design and thinking.

OTOH:

  • Date(zdt, Local) and Date(zdt, UTC) // DateTime(zdt, Local) and DateTime(zdt, UTC) do sound right to me though.
  • A bunch of places that do DateTime(ztd) will need to be changed due to deprecation. and any places that do Date(DateTime(ztd)) will be a subset of those. So doing both at once would mean we can go staight to Date(ztd, Local).

I think I am convinced.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I am convinced

Convinced enough to do the work? ;). As I said I can do the leg work if you prefer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how deep I would have to go,
so if you know exactly what you have to change be my guest.

Otherwise this might sit til after neurips

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting til after NeurIPS might be fine though.
We've lived this long

@codecov-io
Copy link

codecov-io commented Nov 21, 2019

Codecov Report

Merging #231 into master will increase coverage by 0.02%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #231      +/-   ##
==========================================
+ Coverage   93.97%   93.99%   +0.02%     
==========================================
  Files          29       29              
  Lines        1112     1116       +4     
==========================================
+ Hits         1045     1049       +4     
  Misses         67       67
Impacted Files Coverage Δ
src/TimeZones.jl 100% <ø> (ø) ⬆️
src/discovery.jl 87.5% <ø> (ø) ⬆️
src/types/zoneddatetime.jl 93.44% <100%> (+0.1%) ⬆️
src/conversions.jl 100% <100%> (ø) ⬆️
src/arithmetic.jl 82.35% <100%> (ø) ⬆️
src/rounding.jl 100% <100%> (ø) ⬆️
src/io.jl 96.29% <100%> (ø) ⬆️
src/accessors.jl 80% <100%> (-5.72%) ⬇️
src/adjusters.jl 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ed6288e...56da670. Read the comment docs.

@omus
Copy link
Member

omus commented Dec 3, 2019

Alright, I've made the updates and bumped the version to 1.0.0 (it's time)


function zdt2unix(::Type{T}, zdt::ZonedDateTime) where T<:Integer
floor(T, datetime2unix(utc(zdt)))
floor(T, datetime2unix(DateTime(zdt, UTC)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
floor(T, datetime2unix(DateTime(zdt, UTC)))
return floor(T, datetime2unix(DateTime(zdt, UTC)))


function zdt2unix(zdt::ZonedDateTime)
datetime2unix(utc(zdt))
datetime2unix(DateTime(zdt, UTC))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
datetime2unix(DateTime(zdt, UTC))
return datetime2unix(DateTime(zdt, UTC))

end

function julian2zdt(jd::Real)
ZonedDateTime(julian2datetime(jd), utc_tz, from_utc=true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ZonedDateTime(julian2datetime(jd), utc_tz, from_utc=true)
return ZonedDateTime(julian2datetime(jd), utc_tz, from_utc=true)


function zdt2julian(::Type{T}, zdt::ZonedDateTime) where T<:Real
convert(T, datetime2julian(utc(zdt)))
convert(T, datetime2julian(DateTime(zdt, UTC)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
convert(T, datetime2julian(DateTime(zdt, UTC)))
return convert(T, datetime2julian(DateTime(zdt, UTC)))


function zdt2julian(::Type{T}, zdt::ZonedDateTime) where T<:Integer
floor(T, datetime2julian(utc(zdt)))
floor(T, datetime2julian(DateTime(zdt, UTC)))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
floor(T, datetime2julian(DateTime(zdt, UTC)))
return floor(T, datetime2julian(DateTime(zdt, UTC)))


function zdt2julian(zdt::ZonedDateTime)
datetime2julian(utc(zdt))
datetime2julian(DateTime(zdt, UTC))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
datetime2julian(DateTime(zdt, UTC))
return datetime2julian(DateTime(zdt, UTC))

@omus
Copy link
Member

omus commented Dec 13, 2019

I squashed some commits in preparation for merging. A couple of things to note:

  • Adding return statements will not be done in this PR as I think that is a separate conversation
  • We're bumping to version 1.0 here as there haven't been any major API changes for a while and the ones I have planned can always go into 2.0

"""
Time(zdt::ZonedDateTime, ::Union{Type{Local}, Type{UTC}}) -> Time
Create an implicit local/UTC `Time` from the given `ZonedDateTime`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couild we add a bit more text?

Suggested change
Create an implicit local/UTC `Time` from the given `ZonedDateTime`.
Create an implicit local/UTC `Time` from the given `ZonedDateTime`.
- `Local` means a time local to the timezone in the `ZonedDateTime`, i.e. just discarding the timezone information. (**Not** local to the system time of the user invoking the code).
- `UTC` means to shift to the `UTC` timezone and then discard all timezone information.

Maybe also this particular text should be put in to a string constant and interpolatred in at a few places?
Like in Date and DateTime as well?

Probably could be written better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote the docstrings to be more sensible

Copy link
Contributor Author

@oxinabox oxinabox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than a bit of documentation this LGTM.

@omus
Copy link
Member

omus commented Jan 23, 2020

Quick note: I'm still planning on doing the 1.0 version change as part of this but since this adds a deprecation I'll be doing a 0.11 release first so I can drop the deprecation as part of 1.0


export TimeZone, @tz_str, istimezone, FixedTimeZone, VariableTimeZone, ZonedDateTime,
DateTime, TimeError, AmbiguousTimeError, NonExistentTimeError, UnhandledTimeError,
DateTime, Date, Time, UTC, Local, TimeError, AmbiguousTimeError, NonExistentTimeError,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could these be broken down into logical groupings (macros, errors, etc.) or alphabetically?

Copy link
Member

@omus omus Jan 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do some organization. What do you think of grouping by the file in which these are defined (and having a comment indicating the file)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be too granular, but:

export @tz_str
export AmbiguousTimeError, NonExistentTimeError, TimeError
export Date, DateTime, Local, Time, TimeZone, UTC, VariableTimeZone, ZonedDateTime
export isdatetime

I think it's less about what the groupings are but more that there is some order to them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that there is an ordering to this but it's not as obvious as I'd like. A couple of key things I would like to communicate as part of the exports:

  • Quite a few of these are re-exports from Dates which are just here in case someone only does using TimeZones
  • There are some types that are closely related like UTC and Local which I think should be sequential

I'll post an revised version in a sec

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I'm currently thinking:

export
    TimeZone, FixedTimeZone, VariableTimeZone, @tz_str, istimezone, localzone
    UTC, Local,
    ZonedDateTime, timezone,

    # Exceptions
    TimeError, AmbiguousTimeError, NonExistentTimeError, UnhandledTimeError,

    # Discovery tools
    timezone_names, all_timezones, timezones_from_abbr, timezone_abbrs,
    next_transition_instant, show_next_transition,

    # Extended functionality from Dates. Note: Some of these exports have already been done
    # but are listed here for completeness.
    TimeZone, UTC,
    DateTime, Date, Time,
    now, today,
    year, month, week, day, dayofmonth, hour, minute, second, millisecond
    yearmonthday, yearmonth, monthday,
    firstdayofyear, firstdayofquarter, firstdayofmonth, firstdayofweek,
    lastdayofyear, lastdayofquarter, lastdayofmonth, lastdayofweek

I'm unsure on what is better at this point and possibly this kind of logical grouping should just be in the documentation? In any case I think this could be made into an issue and not hold up this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, this isn't a dealbreaker for the PR going through and can be discussed afterwards.

Side-note; does exporting functionality multiple times have any consequences to it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does exporting functionality multiple times have any consequences to it?

No it shouldn't

@omus omus merged commit f9d7443 into JuliaTime:master Jan 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Date(::ZonedDateTime) method

5 participants