package lunar

  1. Overview
  2. Docs

Source file month.ml

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
(* Copyright (c) 2026, Cargocut and the Lunar developers.
   All rights reserved.

   SPDX-License-Identifier: BSD-3-Clause *)

type t =
  | Jan
  | Feb
  | Mar
  | Apr
  | May
  | Jun
  | Jul
  | Aug
  | Sep
  | Oct
  | Nov
  | Dec

type error =
  | Invalid_month_number of int
  | Invalid_month_string of string

let all = [ Jan; Feb; Mar; Apr; May; Jun; Jul; Aug; Sep; Oct; Nov; Dec ]

let to_int = function
  | Jan -> 1
  | Feb -> 2
  | Mar -> 3
  | Apr -> 4
  | May -> 5
  | Jun -> 6
  | Jul -> 7
  | Aug -> 8
  | Sep -> 9
  | Oct -> 10
  | Nov -> 11
  | Dec -> 12
;;

let from_int = function
  | 1 -> Ok Jan
  | 2 -> Ok Feb
  | 3 -> Ok Mar
  | 4 -> Ok Apr
  | 5 -> Ok May
  | 6 -> Ok Jun
  | 7 -> Ok Jul
  | 8 -> Ok Aug
  | 9 -> Ok Sep
  | 10 -> Ok Oct
  | 11 -> Ok Nov
  | 12 -> Ok Dec
  | n -> Error (Invalid_month_number n)
;;

let to_string = function
  | Jan -> "january"
  | Feb -> "february"
  | Mar -> "march"
  | Apr -> "april"
  | May -> "may"
  | Jun -> "june"
  | Jul -> "july"
  | Aug -> "august"
  | Sep -> "september"
  | Oct -> "october"
  | Nov -> "november"
  | Dec -> "december"
;;

let to_short_string = function
  | Jan -> "jan"
  | Feb -> "feb"
  | Mar -> "mar"
  | Apr -> "apr"
  | May -> "may"
  | Jun -> "jun"
  | Jul -> "jul"
  | Aug -> "aug"
  | Sep -> "sep"
  | Oct -> "oct"
  | Nov -> "nov"
  | Dec -> "dec"
;;

let from_string str =
  match String.(trim @@ lowercase_ascii str) with
  | "jan" | "january" -> Ok Jan
  | "feb" | "february" -> Ok Feb
  | "mar" | "march" -> Ok Mar
  | "apr" | "april" -> Ok Apr
  | "may" -> Ok May
  | "jun" | "june" -> Ok Jun
  | "jul" | "july" -> Ok Jul
  | "aug" | "august" -> Ok Aug
  | "sep" | "september" -> Ok Sep
  | "oct" | "october" -> Ok Oct
  | "nov" | "november" -> Ok Nov
  | "dec" | "december" -> Ok Dec
  | s -> Error (Invalid_month_string s)
;;

let days_in ~year = function
  | Jan | Mar | May | Jul | Aug | Oct | Dec -> 31
  | Feb -> if Util.is_leap_year year then 29 else 28
  | Apr | Jun | Sep | Nov -> 30
;;

let succ = function
  | Dec -> Jan
  | mon ->
    from_int (succ @@ to_int mon)
    |>
    (* NOTE: [Dec] case is guarded so [get_ok] is safe. *)
    Result.get_ok
;;

let pred = function
  | Jan -> Dec
  | mon ->
    from_int (pred @@ to_int mon)
    |>
    (* NOTE: [Jan] case is guarded so [get_ok] is safe. *)
    Result.get_ok
;;

let equal a b =
  let a = to_int a
  and b = to_int b in
  Int.equal a b
;;

let compare a b =
  let a = to_int a
  and b = to_int b in
  Int.compare a b
;;

let quarter_of = function
  | Jan | Feb | Mar -> Jan, Mar
  | Apr | May | Jun -> Apr, Jun
  | Jul | Aug | Sep -> Jul, Sep
  | Oct | Nov | Dec -> Oct, Dec
;;

module CE = struct
  type nonrec t = t

  let equal = equal
  let compare = compare
end

include Util.Make_compare_helpers (CE)

module Infix = struct
  include Util.Make_equal_infix (CE)
  include Util.Make_compare_infix (CE)
end

module Map = Stdlib.Map.Make (CE)
module Set = Stdlib.Set.Make (CE)
include Infix