chronology.ha (5522B)
1 // License: MPL-2.0 2 // (c) 2021-2022 Byron Torres <b@torresjrjr.com> 3 use errors; 4 use time; 5 use time::chrono; 6 7 // These functions are renamed to avoid namespace conflicts, like in the 8 // parameters of the [[new]] function. 9 10 // Returns a [[datetime]]'s era. 11 export fn era(dt: *datetime) int = _era(dt); 12 13 // Returns a [[datetime]]'s year. 14 export fn year(dt: *datetime) int = _year(dt); 15 16 // Returns a [[datetime]]'s month of the year. 17 export fn month(dt: *datetime) int = _month(dt); 18 19 // Returns a [[datetime]]'s day of the month. 20 export fn day(dt: *datetime) int = _day(dt); 21 22 // Returns a [[datetime]]'s day of the week; Monday=0 to Sunday=6. 23 export fn weekday(dt: *datetime) int = _weekday(dt); 24 25 // Returns a [[datetime]]'s ordinal day of the year. 26 export fn yearday(dt: *datetime) int = _yearday(dt); 27 28 // Returns a [[datetime]]'s ISO week-numbering year. 29 export fn isoweekyear(dt: *datetime) int = _isoweekyear(dt); 30 31 // Returns a [[datetime]]'s Gregorian week starting Monday. 32 export fn week(dt: *datetime) int = _week(dt); 33 34 // Returns a [[datetime]]'s Gregorian week starting Sunday. 35 export fn sundayweek(dt: *datetime) int = _sundayweek(dt); 36 37 // Returns a [[datetime]]'s ISO week. 38 export fn isoweek(dt: *datetime) int = _isoweek(dt); 39 40 // Returns a [[datetime]]'s hour of the day. 41 export fn hour(dt: *datetime) int = _hour(dt); 42 43 // Returns a [[datetime]]'s minute of the hour. 44 export fn minute(dt: *datetime) int = _minute(dt); 45 46 // Returns a [[datetime]]'s second of the minute. 47 export fn second(dt: *datetime) int = _second(dt); 48 49 // Returns a [[datetime]]'s nanosecond of the second. 50 export fn nanosecond(dt: *datetime) int = _nanosecond(dt); 51 52 fn _era(dt: *datetime) int = { 53 match (dt.era) { 54 case void => 55 if (dt.year is void) { 56 dt.year = _year(dt); 57 }; 58 dt.era = calc_era(dt.year: int); 59 return dt.era: int; 60 case let a: int => 61 return a; 62 }; 63 }; 64 65 fn _year(dt: *datetime) int = { 66 match (dt.year) { 67 case void => 68 const ymd = calc_ymd(chrono::date(dt)); 69 dt.year = ymd.0; 70 dt.month = ymd.1; 71 dt.day = ymd.2; 72 return dt.year: int; 73 case let y: int => 74 return y; 75 }; 76 }; 77 78 fn _month(dt: *datetime) int = { 79 match (dt.month) { 80 case void => 81 const ymd = calc_ymd(chrono::date(dt)); 82 dt.year = ymd.0; 83 dt.month = ymd.1; 84 dt.day = ymd.2; 85 return dt.month: int; 86 case let y: int => 87 return y; 88 }; 89 }; 90 91 fn _day(dt: *datetime) int = { 92 match (dt.day) { 93 case void => 94 const ymd = calc_ymd(chrono::date(dt)); 95 dt.year = ymd.0; 96 dt.month = ymd.1; 97 dt.day = ymd.2; 98 return dt.day: int; 99 case let y: int => 100 return y; 101 }; 102 }; 103 104 fn _weekday(dt: *datetime) int = { 105 match (dt.weekday) { 106 case void => 107 dt.weekday = calc_weekday(chrono::date(dt)); 108 return dt.weekday: int; 109 case let y: int => 110 return y; 111 }; 112 }; 113 114 fn _yearday(dt: *datetime) int = { 115 match (dt.yearday) { 116 case void => 117 if (dt.year is void) { 118 _year(dt); 119 }; 120 if (dt.month is void) { 121 _month(dt); 122 }; 123 if (dt.day is void) { 124 _day(dt); 125 }; 126 dt.yearday = calc_yearday( 127 dt.year: int, 128 dt.month: int, 129 dt.day: int, 130 ); 131 return dt.yearday: int; 132 case let yd: int => 133 return yd; 134 }; 135 }; 136 137 fn _isoweekyear(dt: *datetime) int = { 138 match (dt.isoweekyear) { 139 case void => 140 if (dt.year is void) { 141 _year(dt); 142 }; 143 if (dt.month is void) { 144 _month(dt); 145 }; 146 if (dt.day is void) { 147 _day(dt); 148 }; 149 if (dt.weekday is void) { 150 _weekday(dt); 151 }; 152 dt.isoweekyear = calc_isoweekyear( 153 dt.year: int, 154 dt.month: int, 155 dt.day: int, 156 dt.weekday: int, 157 ); 158 return dt.isoweekyear: int; 159 case let iwy: int => 160 return iwy; 161 }; 162 }; 163 164 fn _week(dt: *datetime) int = { 165 match (dt.week) { 166 case void => 167 if (dt.yearday is void) { 168 _yearday(dt); 169 }; 170 if (dt.weekday is void) { 171 _weekday(dt); 172 }; 173 dt.week = calc_week( 174 dt.yearday: int, 175 dt.weekday: int, 176 ); 177 return dt.week: int; 178 case let w: int => 179 return w; 180 }; 181 }; 182 183 fn _sundayweek(dt: *datetime) int = { 184 match (dt.sundayweek) { 185 case void => 186 if (dt.yearday is void) { 187 _yearday(dt); 188 }; 189 if (dt.weekday is void) { 190 _weekday(dt); 191 }; 192 dt.sundayweek = calc_sundayweek( 193 dt.yearday: int, 194 dt.weekday: int, 195 ); 196 return dt.sundayweek: int; 197 case let w: int => 198 return w; 199 }; 200 }; 201 202 fn _isoweek(dt: *datetime) int = { 203 match (dt.isoweek) { 204 case void => 205 if (dt.year is void) { 206 _year(dt); 207 }; 208 if (dt.week is void) { 209 _week(dt); 210 }; 211 dt.isoweek = calc_isoweek( 212 dt.year: int, 213 dt.week: int, 214 ); 215 return dt.isoweek: int; 216 case let iw: int => 217 return iw; 218 }; 219 }; 220 221 fn _hour(dt: *datetime) int = { 222 match (dt.hour) { 223 case void => 224 const hmsn = calc_hmsn(chrono::time(dt)); 225 dt.hour = hmsn.0; 226 dt.minute = hmsn.1; 227 dt.second = hmsn.2; 228 dt.nanosecond = hmsn.3; 229 return dt.hour: int; 230 case let h: int => 231 return h; 232 }; 233 }; 234 235 fn _minute(dt: *datetime) int = { 236 match (dt.minute) { 237 case void => 238 const hmsn = calc_hmsn(chrono::time(dt)); 239 dt.hour = hmsn.0; 240 dt.minute = hmsn.1; 241 dt.second = hmsn.2; 242 dt.nanosecond = hmsn.3; 243 return dt.minute: int; 244 case let m: int => 245 return m; 246 }; 247 }; 248 249 fn _second(dt: *datetime) int = { 250 match (dt.second) { 251 case void => 252 const hmsn = calc_hmsn(chrono::time(dt)); 253 dt.hour = hmsn.0; 254 dt.minute = hmsn.1; 255 dt.second = hmsn.2; 256 dt.nanosecond = hmsn.3; 257 return dt.second: int; 258 case let s: int => 259 return s; 260 }; 261 }; 262 263 fn _nanosecond(dt: *datetime) int = { 264 match (dt.nanosecond) { 265 case void => 266 const hmsn = calc_hmsn(chrono::time(dt)); 267 dt.hour = hmsn.0; 268 dt.minute = hmsn.1; 269 dt.second = hmsn.2; 270 dt.nanosecond = hmsn.3; 271 return dt.nanosecond: int; 272 case let n: int => 273 return n; 274 }; 275 };