Initial commit

This commit is contained in:
Hugo Mårdbrink 2025-11-30 15:44:22 +01:00
commit a6272848f9
379 changed files with 74829 additions and 0 deletions

View file

@ -0,0 +1,391 @@
import * as $int from "../../../gleam_stdlib/gleam/int.mjs";
import * as $order from "../../../gleam_stdlib/gleam/order.mjs";
import { Ok, Error, CustomType as $CustomType } from "../../gleam.mjs";
import * as $duration from "../../gleam/time/duration.mjs";
import { local_time_offset_seconds } from "../../gleam_time_ffi.mjs";
export class Date extends $CustomType {
constructor(year, month, day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
}
export const Date$Date = (year, month, day) => new Date(year, month, day);
export const Date$isDate = (value) => value instanceof Date;
export const Date$Date$year = (value) => value.year;
export const Date$Date$0 = (value) => value.year;
export const Date$Date$month = (value) => value.month;
export const Date$Date$1 = (value) => value.month;
export const Date$Date$day = (value) => value.day;
export const Date$Date$2 = (value) => value.day;
export class TimeOfDay extends $CustomType {
constructor(hours, minutes, seconds, nanoseconds) {
super();
this.hours = hours;
this.minutes = minutes;
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}
}
export const TimeOfDay$TimeOfDay = (hours, minutes, seconds, nanoseconds) =>
new TimeOfDay(hours, minutes, seconds, nanoseconds);
export const TimeOfDay$isTimeOfDay = (value) => value instanceof TimeOfDay;
export const TimeOfDay$TimeOfDay$hours = (value) => value.hours;
export const TimeOfDay$TimeOfDay$0 = (value) => value.hours;
export const TimeOfDay$TimeOfDay$minutes = (value) => value.minutes;
export const TimeOfDay$TimeOfDay$1 = (value) => value.minutes;
export const TimeOfDay$TimeOfDay$seconds = (value) => value.seconds;
export const TimeOfDay$TimeOfDay$2 = (value) => value.seconds;
export const TimeOfDay$TimeOfDay$nanoseconds = (value) => value.nanoseconds;
export const TimeOfDay$TimeOfDay$3 = (value) => value.nanoseconds;
export class January extends $CustomType {}
export const Month$January = () => new January();
export const Month$isJanuary = (value) => value instanceof January;
export class February extends $CustomType {}
export const Month$February = () => new February();
export const Month$isFebruary = (value) => value instanceof February;
export class March extends $CustomType {}
export const Month$March = () => new March();
export const Month$isMarch = (value) => value instanceof March;
export class April extends $CustomType {}
export const Month$April = () => new April();
export const Month$isApril = (value) => value instanceof April;
export class May extends $CustomType {}
export const Month$May = () => new May();
export const Month$isMay = (value) => value instanceof May;
export class June extends $CustomType {}
export const Month$June = () => new June();
export const Month$isJune = (value) => value instanceof June;
export class July extends $CustomType {}
export const Month$July = () => new July();
export const Month$isJuly = (value) => value instanceof July;
export class August extends $CustomType {}
export const Month$August = () => new August();
export const Month$isAugust = (value) => value instanceof August;
export class September extends $CustomType {}
export const Month$September = () => new September();
export const Month$isSeptember = (value) => value instanceof September;
export class October extends $CustomType {}
export const Month$October = () => new October();
export const Month$isOctober = (value) => value instanceof October;
export class November extends $CustomType {}
export const Month$November = () => new November();
export const Month$isNovember = (value) => value instanceof November;
export class December extends $CustomType {}
export const Month$December = () => new December();
export const Month$isDecember = (value) => value instanceof December;
/**
* Get the offset for the computer's currently configured time zone.
*
* Note this may not be the time zone that is correct to use for your user.
* For example, if you are making a web application that runs on a server you
* want _their_ computer's time zone, not yours.
*
* This is the _current local_ offset, not the current local time zone. This
* means that while it will result in the expected outcome for the current
* time, it may result in unexpected output if used with other timestamps. For
* example: a timestamp that would locally be during daylight savings time if
* is it not currently daylight savings time when this function is called.
*/
export function local_offset() {
return $duration.seconds(local_time_offset_seconds());
}
/**
* Returns the English name for a month.
*
* # Examples
*
* ```gleam
* month_to_string(April)
* // -> "April"
* ```
*/
export function month_to_string(month) {
if (month instanceof January) {
return "January";
} else if (month instanceof February) {
return "February";
} else if (month instanceof March) {
return "March";
} else if (month instanceof April) {
return "April";
} else if (month instanceof May) {
return "May";
} else if (month instanceof June) {
return "June";
} else if (month instanceof July) {
return "July";
} else if (month instanceof August) {
return "August";
} else if (month instanceof September) {
return "September";
} else if (month instanceof October) {
return "October";
} else if (month instanceof November) {
return "November";
} else {
return "December";
}
}
/**
* Returns the number for the month, where January is 1 and December is 12.
*
* # Examples
*
* ```gleam
* month_to_int(January)
* // -> 1
* ```
*/
export function month_to_int(month) {
if (month instanceof January) {
return 1;
} else if (month instanceof February) {
return 2;
} else if (month instanceof March) {
return 3;
} else if (month instanceof April) {
return 4;
} else if (month instanceof May) {
return 5;
} else if (month instanceof June) {
return 6;
} else if (month instanceof July) {
return 7;
} else if (month instanceof August) {
return 8;
} else if (month instanceof September) {
return 9;
} else if (month instanceof October) {
return 10;
} else if (month instanceof November) {
return 11;
} else {
return 12;
}
}
/**
* Returns the month for a given number, where January is 1 and December is 12.
*
* # Examples
*
* ```gleam
* month_from_int(1)
* // -> Ok(January)
* ```
*/
export function month_from_int(month) {
if (month === 1) {
return new Ok(new January());
} else if (month === 2) {
return new Ok(new February());
} else if (month === 3) {
return new Ok(new March());
} else if (month === 4) {
return new Ok(new April());
} else if (month === 5) {
return new Ok(new May());
} else if (month === 6) {
return new Ok(new June());
} else if (month === 7) {
return new Ok(new July());
} else if (month === 8) {
return new Ok(new August());
} else if (month === 9) {
return new Ok(new September());
} else if (month === 10) {
return new Ok(new October());
} else if (month === 11) {
return new Ok(new November());
} else if (month === 12) {
return new Ok(new December());
} else {
return new Error(undefined);
}
}
/**
* Determines if a given year is a leap year.
*
* A leap year occurs every 4 years, except for years divisible by 100,
* unless they are also divisible by 400.
*
* # Examples
*
* ```gleam
* is_leap_year(2024)
* // -> True
* ```
*
* ```gleam
* is_leap_year(2023)
* // -> False
* ```
*/
export function is_leap_year(year) {
let $ = (year % 400) === 0;
if ($) {
return $;
} else {
let $1 = (year % 100) === 0;
if ($1) {
return false;
} else {
return (year % 4) === 0;
}
}
}
/**
* Checks if a given date is valid.
*
* This function properly accounts for leap years when validating February days.
* A leap year occurs every 4 years, except for years divisible by 100,
* unless they are also divisible by 400.
*
* # Examples
*
* ```gleam
* is_valid_date(Date(2023, April, 15))
* // -> True
* ```
*
* ```gleam
* is_valid_date(Date(2023, April, 31))
* // -> False
* ```
*
* ```gleam
* is_valid_date(Date(2024, February, 29))
* // -> True (2024 is a leap year)
* ```
*/
export function is_valid_date(date) {
let year;
let month;
let day;
year = date.year;
month = date.month;
day = date.day;
let $ = day < 1;
if ($) {
return false;
} else {
if (month instanceof January) {
return day <= 31;
} else if (month instanceof February) {
let _block;
let $1 = is_leap_year(year);
if ($1) {
_block = 29;
} else {
_block = 28;
}
let max_february_days = _block;
return day <= max_february_days;
} else if (month instanceof March) {
return day <= 31;
} else if (month instanceof April) {
return day <= 30;
} else if (month instanceof May) {
return day <= 31;
} else if (month instanceof June) {
return day <= 30;
} else if (month instanceof July) {
return day <= 31;
} else if (month instanceof August) {
return day <= 31;
} else if (month instanceof September) {
return day <= 30;
} else if (month instanceof October) {
return day <= 31;
} else if (month instanceof November) {
return day <= 30;
} else {
return day <= 31;
}
}
}
/**
* Checks if a time of day is valid.
*
* Validates that hours are 0-23, minutes are 0-59, seconds are 0-59,
* and nanoseconds are 0-999,999,999.
*
* # Examples
*
* ```gleam
* is_valid_time_of_day(TimeOfDay(12, 30, 45, 123456789))
* // -> True
* ```
*/
export function is_valid_time_of_day(time) {
let hours;
let minutes;
let seconds;
let nanoseconds;
hours = time.hours;
minutes = time.minutes;
seconds = time.seconds;
nanoseconds = time.nanoseconds;
return (((((((hours >= 0) && (hours <= 23)) && (minutes >= 0)) && (minutes <= 59)) && (seconds >= 0)) && (seconds <= 59)) && (nanoseconds >= 0)) && (nanoseconds <= 999_999_999);
}
/**
* Naively compares two dates without any time zone information, returning an
* order.
*
* ## Correctness
*
* This function compares dates without any time zone information, only using
* the rules for the gregorian calendar. This is typically sufficient, but be
* aware that in reality some time zones will change their calendar date
* occasionally. This can result in days being skipped, out of order, or
* happening multiple times.
*
* If you need real-world correct time ordering then use the
* `gleam/time/timestamp` module instead.
*/
export function naive_date_compare(one, other) {
let _pipe = $int.compare(one.year, other.year);
let _pipe$1 = $order.lazy_break_tie(
_pipe,
() => {
return $int.compare(month_to_int(one.month), month_to_int(other.month));
},
);
return $order.lazy_break_tie(
_pipe$1,
() => { return $int.compare(one.day, other.day); },
);
}
/**
* The offset for the [Coordinated Universal Time (UTC)](https://en.wikipedia.org/wiki/Coordinated_Universal_Time)
* time zone.
*
* The utc zone has no time adjustments, it is always zero. It never observes
* daylight-saving time and it never shifts around based on political
* restructuring.
*/
export const utc_offset = $duration.empty;

View file

@ -0,0 +1,382 @@
import * as $bool from "../../../gleam_stdlib/gleam/bool.mjs";
import * as $int from "../../../gleam_stdlib/gleam/int.mjs";
import * as $order from "../../../gleam_stdlib/gleam/order.mjs";
import * as $string from "../../../gleam_stdlib/gleam/string.mjs";
import { CustomType as $CustomType, remainderInt, divideInt, isEqual } from "../../gleam.mjs";
class Duration extends $CustomType {
constructor(seconds, nanoseconds) {
super();
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}
}
export class Nanosecond extends $CustomType {}
export const Unit$Nanosecond = () => new Nanosecond();
export const Unit$isNanosecond = (value) => value instanceof Nanosecond;
export class Microsecond extends $CustomType {}
export const Unit$Microsecond = () => new Microsecond();
export const Unit$isMicrosecond = (value) => value instanceof Microsecond;
export class Millisecond extends $CustomType {}
export const Unit$Millisecond = () => new Millisecond();
export const Unit$isMillisecond = (value) => value instanceof Millisecond;
export class Second extends $CustomType {}
export const Unit$Second = () => new Second();
export const Unit$isSecond = (value) => value instanceof Second;
export class Minute extends $CustomType {}
export const Unit$Minute = () => new Minute();
export const Unit$isMinute = (value) => value instanceof Minute;
export class Hour extends $CustomType {}
export const Unit$Hour = () => new Hour();
export const Unit$isHour = (value) => value instanceof Hour;
export class Day extends $CustomType {}
export const Unit$Day = () => new Day();
export const Unit$isDay = (value) => value instanceof Day;
export class Week extends $CustomType {}
export const Unit$Week = () => new Week();
export const Unit$isWeek = (value) => value instanceof Week;
export class Month extends $CustomType {}
export const Unit$Month = () => new Month();
export const Unit$isMonth = (value) => value instanceof Month;
export class Year extends $CustomType {}
export const Unit$Year = () => new Year();
export const Unit$isYear = (value) => value instanceof Year;
/**
* Ensure the duration is represented with `nanoseconds` being positive and
* less than 1 second.
*
* This function does not change the amount of time that the duratoin refers
* to, it only adjusts the values used to represent the time.
*
* @ignore
*/
function normalise(duration) {
let multiplier = 1_000_000_000;
let nanoseconds$1 = remainderInt(duration.nanoseconds, multiplier);
let overflow = duration.nanoseconds - nanoseconds$1;
let seconds$1 = duration.seconds + (divideInt(overflow, multiplier));
let $ = nanoseconds$1 >= 0;
if ($) {
return new Duration(seconds$1, nanoseconds$1);
} else {
return new Duration(seconds$1 - 1, multiplier + nanoseconds$1);
}
}
/**
* Convert a duration to a number of the largest number of a unit, serving as
* a rough description of the duration that a human can understand.
*
* The size used for each unit are described in the documentation for the
* `Unit` type.
*
* ```gleam
* seconds(125)
* |> approximate
* // -> #(2, Minute)
* ```
*
* This function rounds _towards zero_. This means that if a duration is just
* short of 2 days then it will approximate to 1 day.
*
* ```gleam
* hours(47)
* |> approximate
* // -> #(1, Day)
* ```
*/
export function approximate(duration) {
let s;
let ns;
s = duration.seconds;
ns = duration.nanoseconds;
let minute = 60;
let hour = minute * 60;
let day = hour * 24;
let week = day * 7;
let year = day * 365 + hour * 6;
let month = globalThis.Math.trunc(year / 12);
let microsecond = 1000;
let millisecond = microsecond * 1000;
let $ = undefined;
if (s < 0) {
let _block;
let _pipe = new Duration(- s, - ns);
let _pipe$1 = normalise(_pipe);
_block = approximate(_pipe$1);
let $1 = _block;
let amount;
let unit;
amount = $1[0];
unit = $1[1];
return [- amount, unit];
} else if (s >= year) {
return [divideInt(s, year), new Year()];
} else if (s >= month) {
return [divideInt(s, month), new Month()];
} else if (s >= week) {
return [divideInt(s, week), new Week()];
} else if (s >= day) {
return [divideInt(s, day), new Day()];
} else if (s >= hour) {
return [divideInt(s, hour), new Hour()];
} else if (s >= minute) {
return [divideInt(s, minute), new Minute()];
} else if (s > 0) {
return [s, new Second()];
} else if (ns >= millisecond) {
return [divideInt(ns, millisecond), new Millisecond()];
} else if (ns >= microsecond) {
return [divideInt(ns, microsecond), new Microsecond()];
} else {
return [ns, new Nanosecond()];
}
}
/**
* Compare one duration to another, indicating whether the first spans a
* larger amount of time (and so is greater) or smaller amount of time (and so
* is lesser) than the second.
*
* # Examples
*
* ```gleam
* compare(seconds(1), seconds(2))
* // -> order.Lt
* ```
*
* Whether a duration is negative or positive doesn't matter for comparing
* them, only the amount of time spanned matters.
*
* ```gleam
* compare(seconds(-2), seconds(1))
* // -> order.Gt
* ```
*/
export function compare(left, right) {
let parts = (x) => {
let $ = x.seconds >= 0;
if ($) {
return [x.seconds, x.nanoseconds];
} else {
return [x.seconds * -1 - 1, 1_000_000_000 - x.nanoseconds];
}
};
let $ = parts(left);
let ls;
let lns;
ls = $[0];
lns = $[1];
let $1 = parts(right);
let rs;
let rns;
rs = $1[0];
rns = $1[1];
let _pipe = $int.compare(ls, rs);
return $order.break_tie(_pipe, $int.compare(lns, rns));
}
/**
* Calculate the difference between two durations.
*
* This is effectively substracting the first duration from the second.
*
* # Examples
*
* ```gleam
* difference(seconds(1), seconds(5))
* // -> seconds(4)
* ```
*/
export function difference(left, right) {
let _pipe = new Duration(
right.seconds - left.seconds,
right.nanoseconds - left.nanoseconds,
);
return normalise(_pipe);
}
/**
* Add two durations together.
*
* # Examples
*
* ```gleam
* add(seconds(1), seconds(5))
* // -> seconds(6)
* ```
*/
export function add(left, right) {
let _pipe = new Duration(
left.seconds + right.seconds,
left.nanoseconds + right.nanoseconds,
);
return normalise(_pipe);
}
function nanosecond_digits(loop$n, loop$position, loop$acc) {
while (true) {
let n = loop$n;
let position = loop$position;
let acc = loop$acc;
if (position === 9) {
return acc;
} else if ((acc === "") && ((remainderInt(n, 10)) === 0)) {
loop$n = globalThis.Math.trunc(n / 10);
loop$position = position + 1;
loop$acc = acc;
} else {
let acc$1 = $int.to_string(n % 10) + acc;
loop$n = globalThis.Math.trunc(n / 10);
loop$position = position + 1;
loop$acc = acc$1;
}
}
}
/**
* Create a duration of a number of seconds.
*/
export function seconds(amount) {
return new Duration(amount, 0);
}
/**
* Create a duration of a number of minutes.
*/
export function minutes(amount) {
return seconds(amount * 60);
}
/**
* Create a duration of a number of hours.
*/
export function hours(amount) {
return seconds(amount * 60 * 60);
}
/**
* Create a duration of a number of milliseconds.
*/
export function milliseconds(amount) {
let remainder = amount % 1000;
let overflow = amount - remainder;
let nanoseconds$1 = remainder * 1_000_000;
let seconds$1 = globalThis.Math.trunc(overflow / 1000);
let _pipe = new Duration(seconds$1, nanoseconds$1);
return normalise(_pipe);
}
/**
* Create a duration of a number of nanoseconds.
*
* # JavaScript int limitations
*
* Remember that JavaScript can only perfectly represent ints between positive
* and negative 9,007,199,254,740,991! If you use a single call to this
* function to create durations larger than that number of nanoseconds then
* you will likely not get exactly the value you expect. Use `seconds` and
* `milliseconds` as much as possible for large durations.
*/
export function nanoseconds(amount) {
let _pipe = new Duration(0, amount);
return normalise(_pipe);
}
/**
* Convert the duration to a number of seconds.
*
* There may be some small loss of precision due to `Duration` being
* nanosecond accurate and `Float` not being able to represent this.
*/
export function to_seconds(duration) {
let seconds$1 = $int.to_float(duration.seconds);
let nanoseconds$1 = $int.to_float(duration.nanoseconds);
return seconds$1 + (nanoseconds$1 / 1_000_000_000.0);
}
/**
* Convert the duration to a number of seconds and nanoseconds. There is no
* loss of precision with this conversion on any target.
*/
export function to_seconds_and_nanoseconds(duration) {
return [duration.seconds, duration.nanoseconds];
}
export const empty = /* @__PURE__ */ new Duration(0, 0);
/**
* Convert the duration to an [ISO8601][1] formatted duration string.
*
* The ISO8601 duration format is ambiguous without context due to months and
* years having different lengths, and because of leap seconds. This function
* encodes the duration as days, hours, and seconds without any leap seconds.
* Be sure to take this into account when using the duration strings.
*
* [1]: https://en.wikipedia.org/wiki/ISO_8601#Durations
*/
export function to_iso8601_string(duration) {
return $bool.guard(
isEqual(duration, empty),
"PT0S",
() => {
let split = (total, limit) => {
let amount = remainderInt(total, limit);
let remainder = divideInt((total - amount), limit);
return [amount, remainder];
};
let $ = split(duration.seconds, 60);
let seconds$1;
let rest;
seconds$1 = $[0];
rest = $[1];
let $1 = split(rest, 60);
let minutes$1;
let rest$1;
minutes$1 = $1[0];
rest$1 = $1[1];
let $2 = split(rest$1, 24);
let hours$1;
let rest$2;
hours$1 = $2[0];
rest$2 = $2[1];
let days = rest$2;
let add$1 = (out, value, unit) => {
if (value === 0) {
return out;
} else {
return (out + $int.to_string(value)) + unit;
}
};
let _block;
let _pipe = "P";
let _pipe$1 = add$1(_pipe, days, "D");
let _pipe$2 = $string.append(_pipe$1, "T");
let _pipe$3 = add$1(_pipe$2, hours$1, "H");
_block = add$1(_pipe$3, minutes$1, "M");
let output = _block;
let $3 = duration.nanoseconds;
if ($3 === 0) {
if (seconds$1 === 0) {
return output;
} else {
return (output + $int.to_string(seconds$1)) + "S";
}
} else {
let f = nanosecond_digits(duration.nanoseconds, 0, "");
return (((output + $int.to_string(seconds$1)) + ".") + f) + "S";
}
},
);
}

File diff suppressed because it is too large Load diff