hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

observe.ha (4902B)


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