Skip to content

Commit

Permalink
date_conversions using DatePeriods
Browse files Browse the repository at this point in the history
  • Loading branch information
s-baumann committed Mar 3, 2019
1 parent ea78211 commit 4b5758d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/MultivariateFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Optim

# This includes functions for use of MultivariateFunctions with dates.
include("date_conversions.jl")
export years_between, years_from_global_base
export years_between, years_from_global_base, period_length
# The abstract type MultivariateFunction and all structs are implemented here.
# In addition we have operator reversals and some supporting functions.
include("0_structs_and_generic_reversals.jl")
Expand Down
19 changes: 13 additions & 6 deletions src/date_conversions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ const global_base_date = Date(2000,1,1)
const global_base_date_as_day = convert(Dates.Day, global_base_date)

"""
years_between(a::Date, b::Date)
years_between(a::Dates.Day, b::Dates.Day)
years_between(until::Date, from::Date)
years_between(until::Dates.Day, from::Dates.Day)
Returns the number of years between two dates. For the purposes of this calculation
there are 365.2422 days in a year.
"""
function years_between(a::Date, b::Date)
return (Dates.days(a) -Dates.days(b))/ days_per_year
function years_between(until::Date, from::Date)
return (Dates.days(until) -Dates.days(from))/ days_per_year
end
function years_between(a::Dates.Day, b::Dates.Day)
return (convert(Int, a)-convert(Int, b))/ days_per_year
function years_between(until::Dates.Day, from::Dates.Day)
return (convert(Int, until)-convert(Int, from))/ days_per_year
end

"""
Expand All @@ -30,3 +30,10 @@ end
function years_from_global_base(a::Dates.Day)
return years_between(a, global_base_date_as_day)
end

"""
Period length is designed to convert TimePeriod objects to a float in a consistent way to years_from_global_base
"""
function period_length(a::Dates.DatePeriod, base::Date = global_base_date)
return years_between(base+a, base)
end

0 comments on commit 4b5758d

Please sign in to comment.