Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
MichelJuillard
Periods.jl
Commits
a292ba76
Verified
Commit
a292ba76
authored
Mar 31, 2021
by
Stéphane Adjemian
Browse files
Add doc strings.
parent
dbc13272
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/SimplePeriods.jl
View file @
a292ba76
@enum
Frequency
Year
Semester
Quarter
Month
Week
Business
Day
Undated
"""
Base year used to compute the `ordinal` field of a Period type.
"""
BaseYear
=
1970
"Composite type Period has fields `ordinal` and `frequency`."
struct
Period
<:
AbstractPeriod
"""
Number of periods since year BaseYear.
"""
ordinal
::
Int64
"""
Frequency of the period, possible values are:
- `Year`,
- `Semester`,
- `Quarter`,
- `Month`,
- ` Week`,
- `Day`, and
- `Undated` (no frequency)
"""
frequency
::
Frequency
end
#
# Constructors
#
"""
Period(year)
Constructor for periods with unspecified frequency.
"""
function
Period
(
arg1
::
Integer
)
Period
(
arg1
,
Undated
)
end
"""
Period(year, frequency)
Constructor for yearly periods, if second argument's value is `Year`, or periods with unspecified frequency if
second argument's value is `Undated`.
"""
function
Period
(
arg1
::
Integer
,
frequency
::
Frequency
)
if
frequency
==
Year
ordinal
=
arg1
-
BaseYear
...
...
@@ -18,23 +54,29 @@ function Period(arg1::Integer, frequency::Frequency)
Period
(
ordinal
,
frequency
)
end
"""
Period(year, subperiod, frequency)
Constructor for bi-annual (third argument's value is `Semester`), quarterly (third argument's value is `Quarter`),
monthly (third argument's value is `Month`) or weekly (third argument's value is `Week`) periods.
"""
function
Period
(
arg1
::
Integer
,
arg2
::
Integer
,
frequency
::
Frequency
)
if
frequency
==
Year
ordinal
=
arg1
-
BaseYear
elseif
frequency
==
Semester
@assert
(
arg2
in
1
:
2
)
@assert
(
arg2
in
1
:
2
)
"subperiod (semester) has to be equal to 1 or 2."
ordinal
=
2
*
(
arg1
-
BaseYear
)
+
arg2
-
1
elseif
frequency
==
Quarter
@assert
(
arg2
in
1
:
4
)
@assert
(
arg2
in
1
:
4
)
"subperiod (quarter) has to be an integer between 1 and 4."
ordinal
=
4
*
(
arg1
-
BaseYear
)
+
arg2
-
1
elseif
frequency
==
Month
@assert
(
arg2
in
1
:
12
)
@assert
(
arg2
in
1
:
12
)
"subperiod (month) has to be an integer between 1 and 12."
ordinal
=
12
*
(
arg1
-
BaseYear
)
+
arg2
-
1
elseif
frequency
==
Week
@assert
(
arg2
<=
weeksinyear
(
arg1
))
@assert
(
arg2
<=
weeksinyear
(
arg1
))
"subperiod (week) has to be an integer between 1 and
$
(weeksinyear(arg1))."
ordinal
=
numberofweeks
(
arg1
,
arg2
)
else
if
frequency
==
Undated
ordinal
=
arg1
else
error
(
"Possible values for the last argument are `Month`"
)
end
Period
(
ordinal
,
frequency
)
end
...
...
@@ -106,9 +148,25 @@ function numberofweeks(y::Integer, w::Integer)
end
import
Base
.
copy
"""
copy(p)
Return a copy of Period type p.
"""
copy
(
p
::
Period
)
=
Period
(
p
.
ordinal
,
p
.
frequency
)
import
Base
.:+
"""
+(p, k)
Add k subperiods to Period type p.
"""
+
(
p1
::
Period
,
k
::
Integer
)
=
Period
(
p1
.
ordinal
+
k
,
p1
.
frequency
)
import
Base
.:-
"""
+(p, k)
Substract k subperiods to Period type p.
"""
-
(
p1
::
Period
,
k
::
Integer
)
=
Period
(
p1
.
ordinal
-
k
,
p1
.
frequency
)
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment