Skip to content

Commit 50cd066

Browse files
committed
Added unit tests for date functions
1 parent 1829685 commit 50cd066

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using Revise, Hedgehog2, Test, Dates, Distributions
22

33
include("unit/payoff.jl")
44
include("unit/vol_surface.jl")
5+
include("unit/date_functions.jl")
56
include("black_scholes.jl")
67
include("binomial_tree.jl")
78
include("carr_madan.jl")

test/unit/date_functions.jl

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Test
2+
using Dates
3+
4+
@testset "Time Conversion and ACT/365 Utilities" begin
5+
# Setup
6+
d1 = Date(2020, 1, 1)
7+
d2 = Date(2021, 1, 1)
8+
dt1 = DateTime(d1)
9+
dt2 = DateTime(d2)
10+
ticks1 = to_ticks(d1)
11+
ticks2 = to_ticks(d2)
12+
13+
# to_ticks
14+
@test to_ticks(d1) == Dates.datetime2epochms(DateTime(d1))
15+
@test to_ticks(dt1) == Dates.datetime2epochms(dt1)
16+
@test to_ticks(123456789.0) === 123456789.0
17+
18+
# yearfrac between dates
19+
yf = yearfrac(d1, d2)
20+
@test isapprox(yf, 1.0; atol=1e-8)
21+
22+
# yearfrac with ticks
23+
@test isapprox(yearfrac(ticks1, ticks2), 1.0; atol=1e-8)
24+
25+
# yearfrac with Period
26+
@test isapprox(yearfrac(Month(12)), 1.0; atol=1e-8)
27+
28+
# add_yearfrac (Real, Real)
29+
t0 = 1_580_000_000_000.0 # some ms timestamp
30+
t1 = add_yearfrac(t0, 1.0)
31+
@test isapprox((t1 - t0), MILLISECONDS_IN_YEAR_365; atol=1e-3)
32+
33+
# add_yearfrac (DateTime)
34+
ref_date = DateTime(2020, 1, 1)
35+
shifted = add_yearfrac(ref_date, 1.0)
36+
expected = ref_date + Day(365)
37+
@test Date(shifted) == Date(expected)
38+
end

0 commit comments

Comments
 (0)