/
month_cnv.jl
84 lines (65 loc) · 2.16 KB
/
month_cnv.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This file is a part of AstroLib.jl. License is MIT "Expat".
# Copyright (C) 2016 Mosè Giordano.
"""
month_cnv(number[, shor=true, up=true, low=true]) -> month_name
month_cnv(name) -> number
### Purpose ###
Convert between a month English name and the equivalent number.
### Explanation ###
For example, converts from "January" to 1 or vice-versa.
### Arguments ###
The functions has two methods, one with numeric input (and three possible
boolean keywords) and the other one with string input.
Numeric input arguments:
* `number`: the number of the month to be converted to month name.
* `short` (optional boolean keyword): if true, the abbreviated (3-character)
name of the month will be returned, e.g. "Apr" or "Oct". Default is false.
* `up` (optional boolean keyword): if true, the name of the month will be all in
upper case, e.g. "APRIL" or "OCTOBER". Default is false.
* `low` (optional boolean keyword): if true, the name of the month will be all
in lower case, e.g. "april" or "october". Default is false.
String input argument:
* `name`: month name to be converted to month number.
### Output ###
The month name or month number, depending on the input. For numeric input, the
format of the month name is influenced by the optional keywords.
### Example ###
```jldoctest
julia> using AstroLib
julia> month_cnv.(["janua", "SEP", "aUgUsT"])
3-element Array{Int64,1}:
1
9
8
julia> month_cnv.([2, 12, 6], short=true, low=true)
3-element Array{String,1}:
"feb"
"dec"
"jun"
```
"""
function month_cnv(number::Integer; short::Bool=false,
up::Bool=false, low::Bool=false)
if short
name = Dates.ENGLISH.months_abbr[number]
else
name = Dates.ENGLISH.months[number]
end
if up
name = uppercase(name)
elseif low
name = lowercase(name)
end
return name
end
function month_cnv(name::AbstractString)
name = strip(name)
if length(name) >= 3
output::Integer = getfield(Dates, Symbol(uppercasefirst(lowercase(name[1:3]))))
return output
else
# Do the same as original AstroLib: return -1 for input string shorter
# than 3.
return -1
end
end