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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>
// Observes a [[date]]'s [[zonephase]].
export fn zone(d: *date) zonephase = {
match (d.zonephase) {
case let z: *zonephase =>
return *z;
case null =>
let lookup = match (d.loc.lookupzone) {
case let l: *zonelookuper =>
yield l;
case null =>
yield &lookupzone: *zonelookuper;
};
const z = lookup(d.loc, d);
d.zonephase = z;
return *z;
};
};
// Observes a [[date]]'s observed daydate (day number since epoch).
//
// For dates with a [[locality]] based on the UTC, TAI, GPS, and similar
// [[time::chrono::timescale]]s, their epoch date should be interpreted as the
// Unix epoch (1970 Janurary 1st). Other timescales may suggest their own
// interpretations applicable to other chronologies.
export fn daydate(d: *date) i64 = {
match (d.daydate) {
case let dd: i64 =>
return dd;
case void =>
const (dd, dt) = calc_datetime(
d.loc, to_instant(*d), zone(d).zoff,
);
d.daytime = dt;
d.daydate = dd;
return dd;
};
};
// Observes a [[date]]'s observed time-of-day (amount of daytime progressed in
// a day) as nanoseconds.
export fn daytime(d: *date) i64 = {
match (d.daytime) {
case let dt: i64 =>
return dt;
case void =>
const (dd, dt) = calc_datetime(
d.loc, to_instant(*d), zone(d).zoff,
);
d.daytime = dt;
d.daydate = dd;
return dt;
};
};
// These functions are renamed to avoid namespace conflicts, like in the
// parameters of the [[new]] function.
// Observes a [[date]]'s era.
export fn era(d: *date) int = _era(d);
// Observes a [[date]]'s year.
export fn year(d: *date) int = _year(d);
// Observes a [[date]]'s month of the year. Range January=1 to December=12.
export fn month(d: *date) int = _month(d);
// Observes a [[date]]'s day of the month. Range 1 to 31.
export fn day(d: *date) int = _day(d);
// Observes a [[date]]'s day of the week. Range Monday=0 to Sunday=6.
export fn weekday(d: *date) int = _weekday(d);
// Observes a [[date]]'s ordinal day of the year. Range 1 to 366.
export fn yearday(d: *date) int = _yearday(d);
// Observes a [[date]]'s ISO week-numbering year.
export fn isoweekyear(d: *date) int = _isoweekyear(d);
// Observes a [[date]]'s Gregorian week starting Monday. Range 0 to 53.
// All days in a year before the year's first Monday belong to week 0.
export fn week(d: *date) int = _week(d);
// Observes a [[date]]'s Gregorian week starting Sunday. Range 0 to 53.
// All days in a year before the year's first Sunday belong to week 0.
export fn sundayweek(d: *date) int = _sundayweek(d);
// Observes a [[date]]'s ISO week-numbering week. Range 0 to 53.
export fn isoweek(d: *date) int = _isoweek(d);
// Observes a [[date]]'s hour of the day.
export fn hour(d: *date) int = _hour(d);
// Observes a [[date]]'s minute of the hour.
export fn minute(d: *date) int = _minute(d);
// Observes a [[date]]'s second of the minute.
export fn second(d: *date) int = _second(d);
// Observes a [[date]]'s nanosecond of the second.
export fn nanosecond(d: *date) int = _nanosecond(d);
fn _era(d: *date) int = {
match (d.era) {
case void =>
d.era = calc_era(
_year(d),
);
return d.era: int;
case let a: int =>
return a;
};
};
fn _year(d: *date) int = {
match (d.year) {
case void =>
const ymd = calc_ymd(
daydate(d),
);
d.year = ymd.0;
d.month = ymd.1;
d.day = ymd.2;
return d.year: int;
case let y: int =>
return y;
};
};
fn _month(d: *date) int = {
match (d.month) {
case void =>
const ymd = calc_ymd(
daydate(d),
);
d.year = ymd.0;
d.month = ymd.1;
d.day = ymd.2;
return d.month: int;
case let y: int =>
return y;
};
};
fn _day(d: *date) int = {
match (d.day) {
case void =>
const ymd = calc_ymd(
daydate(d),
);
d.year = ymd.0;
d.month = ymd.1;
d.day = ymd.2;
return d.day: int;
case let y: int =>
return y;
};
};
fn _weekday(d: *date) int = {
match (d.weekday) {
case void =>
d.weekday = calc_weekday(
daydate(d),
);
return d.weekday: int;
case let y: int =>
return y;
};
};
fn _yearday(d: *date) int = {
match (d.yearday) {
case void =>
d.yearday = calc_yearday(
_year(d),
_month(d),
_day(d),
);
return d.yearday: int;
case let yd: int =>
return yd;
};
};
fn _isoweekyear(d: *date) int = {
match (d.isoweekyear) {
case void =>
d.isoweekyear = calc_isoweekyear(
_year(d),
_month(d),
_day(d),
_weekday(d),
);
return d.isoweekyear: int;
case let iwy: int =>
return iwy;
};
};
fn _week(d: *date) int = {
match (d.week) {
case void =>
d.week = calc_week(
_yearday(d),
_weekday(d),
);
return d.week: int;
case let w: int =>
return w;
};
};
fn _sundayweek(d: *date) int = {
match (d.sundayweek) {
case void =>
d.sundayweek = calc_sundayweek(
_yearday(d),
_weekday(d),
);
return d.sundayweek: int;
case let w: int =>
return w;
};
};
fn _isoweek(d: *date) int = {
match (d.isoweek) {
case void =>
d.isoweek = calc_isoweek(
_year(d),
_week(d),
);
return d.isoweek: int;
case let iw: int =>
return iw;
};
};
fn _hour(d: *date) int = {
match (d.hour) {
case void =>
const hmsn = calc_hmsn(
daytime(d),
);
d.hour = hmsn.0;
d.minute = hmsn.1;
d.second = hmsn.2;
d.nanosecond = hmsn.3;
return d.hour: int;
case let h: int =>
return h;
};
};
fn _minute(d: *date) int = {
match (d.minute) {
case void =>
const hmsn = calc_hmsn(
daytime(d),
);
d.hour = hmsn.0;
d.minute = hmsn.1;
d.second = hmsn.2;
d.nanosecond = hmsn.3;
return d.minute: int;
case let m: int =>
return m;
};
};
fn _second(d: *date) int = {
match (d.second) {
case void =>
const hmsn = calc_hmsn(
daytime(d),
);
d.hour = hmsn.0;
d.minute = hmsn.1;
d.second = hmsn.2;
d.nanosecond = hmsn.3;
return d.second: int;
case let s: int =>
return s;
};
};
fn _nanosecond(d: *date) int = {
match (d.nanosecond) {
case void =>
const hmsn = calc_hmsn(
daytime(d),
);
d.hour = hmsn.0;
d.minute = hmsn.1;
d.second = hmsn.2;
d.nanosecond = hmsn.3;
return d.nanosecond: int;
case let n: int =>
return n;
};
};
|