Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Accessors for Day and higher #478

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

amsingh17
Copy link

During a development project, I noticed that I was not able to run Day (or higher) on ZonedDateTime objects. The fix in this PR addresses that issue. Below is a minimum working example to show the issue

               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.9.3 (2023-08-24)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> using TimeZones, Dates

julia> x = ZonedDateTime(today(), tz"UTC")
2024-09-23T00:00:00+00:00

julia> Hour(x)
0 hours

julia> Day(x)
ERROR: MethodError: no method matching Day(::ZonedDateTime)

Closest candidates are:
  (::Type{T})(::Period) where T<:Period
   @ Dates /Applications/Julia-1.9.app/Contents/Resources/julia/share/julia/stdlib/v1.9/Dates/src/periods.jl:384
  Day(::Number)
   @ Dates /Applications/Julia-1.9.app/Contents/Resources/julia/share/julia/stdlib/v1.9/Dates/src/types.jl:55
  Day(::AbstractString)
   @ Dates /Applications/Julia-1.9.app/Contents/Resources/julia/share/julia/stdlib/v1.9/Dates/src/periods.jl:21
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[3]:1


Copy link

codecov bot commented Sep 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.76%. Comparing base (2bc8f50) to head (52e7edb).
Report is 16 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #478      +/-   ##
==========================================
- Coverage   92.79%   92.76%   -0.04%     
==========================================
  Files          39       40       +1     
  Lines        1818     1838      +20     
==========================================
+ Hits         1687     1705      +18     
- Misses        131      133       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@omus omus left a comment

Choose a reason for hiding this comment

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

Can you add tests?

@omus
Copy link
Member

omus commented Sep 25, 2024

I originally added this accessor code when Dates.jl narrowed the types used for these sub-day accessors: 61d8299.

@amsingh17
Copy link
Author

@omus I've added a block of tests to use the Period of a ZonedDateTime. Open to suggestions on expanding additional coverage. Thanks!

Copy link
Member

@omus omus left a comment

Choose a reason for hiding this comment

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

Better to not access the internal fields

test/accessors.jl Outdated Show resolved Hide resolved
Co-authored-by: Curtis Vogt <curtis.vogt@gmail.com>
test/accessors.jl Outdated Show resolved Hide resolved
test/accessors.jl Outdated Show resolved Hide resolved
@@ -9,7 +9,7 @@ timezone(zdt::ZonedDateTime) = zdt.timezone

Dates.days(zdt::ZonedDateTime) = days(DateTime(zdt))

for period in (:Hour, :Minute, :Second, :Millisecond)
for period in (:Year, :Quarter, :Month, :Week, :Day, :Hour, :Minute, :Second, :Millisecond)
accessor = Symbol(lowercase(string(period)))
Copy link
Author

Choose a reason for hiding this comment

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

@omus Looks like the associated accessor for Quarter is Dates.quarterofyear which is causing the latest test failure

Copy link
Member

Choose a reason for hiding this comment

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

Ah, we can remove :Quarter from here and define those methods manually

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.

2 participants