wojtekmach / calendar_interval Goto Github PK
View Code? Open in Web Editor NEWFunctions for working with calendar intervals
License: Other
Functions for working with calendar intervals
License: Other
They will take calendar_interval.first
and return %Date{} and %Date.Range{} respectively.
Calendar.Interval.new/3
is currently a private function however it has valid public use cases where creating an interval between two arbitrary naive_date_times with a given precision is a requirement. Consider making it public?
It is sometimes convenient to find "next Monday" or "last Friday", I think we can neatly achieve it with this API:
# next Monday
iex> CalendarInterval.next(~I"2020-07-09", &Date.day_of_week(&1.first) == 1)
~I"2020-07-13"
As I think you know, I have a package that provides calendars other than Calendar.ISO
. Would you consider a PR that adds support for non Calendar.ISO
calendars to calendar_interval
?
I can see if would require a bit of refactoring but I don't think anything especially traumatic.
iex> ~I"2018" |> I.nest(:minute) |> Enum.at(-1) # => ~I"2019-12-31 23:58"
next(~I"2018/2019") #=> ~I"2020"
or it should be #=> ~I"2020/2021"
nest/2
and enclosing/2
Today we have:
iex> Enum.to_list(~I"2020-10/11")
[~I"2020-10", ~I"2020-11/12"]
and consequently we have:
iex> CalendarInterval.relation(~I"2020-10/11", ~I"2020-10/12")
:equal
which is wrong, we should have:
iex> Enum.to_list(~I"2020-10/11")
[~I"2020-10", ~I"2020-11"]
iex(3)> ~I"2018-01-15 10:00/2018-01-14 09:00"
~I"2018-01-15 10:00/14 09:00"
iex(4)> ~I"2018-01-15 09:00/2018-01-16 09:00"
~I"2018-01-15 09:00/16 09:00"
iex(5)> CalendarInterval.relation(v(3), v(4))
:preceds # should be :overlaps
Currently the precision is an atom:
iex> ~I[2019-01-01].precision
:day
and so if there's a function that just wants to print, say, day from a given interval it needs to check it like that:
def print_day(%CalendarInterval{precision: precision} = i when precision in [:day, :minute, :second, {:microsecond, 0}, {:microsecond, 1}, ..., {:microsecond, 6}] do
IO.puts "#{i.day}"
end
def print_day(%CalendarInterval{} =i) do
raise ArgumentError, "#{inspect(i)} does not have :day precision"
end
An idea is to store which time components we have instead:
iex> ~I[2019-01-01].components
#MapSet<[:year, :month, :day]>
def print_day(%CalendarInterval{} = i) do
if :day in i.components do
IO.puts "#{i.day}"
else
raise ArgumentError, "#{inspect(i)} does not have :day precision"
end
end
This is how https://github.com/domainlanguage/time-count handles it.
it simply calls utc_now(:day)
.
Currently we can only compute the relation of intervals with the same precision:
CalendarInterval.relation(~I"2020", ~I"2020-01/12")
** (FunctionClauseError) no function clause matching in CalendarInterval.relation/2
this restriction seems arbitrary and I think conceptually comparing these makes sense and we should have:
iex> CalendarInterval.relation(~I"2020", ~I"2020-01/12")
:equal
iex> CalendarInterval.relation(~I"2020", ~I"2020-01/11")
:started_by
# etc
wdyt @kipcole9?
Ref #6 (comment)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.