@kahi-ui/framework
Version:
Straight-forward Svelte UI for the Web
60 lines (59 loc) • 2.15 kB
JavaScript
import { Temporal } from "../../vendor/js-temporal-polyfill";
import { DEFAULT_HOUR, DEFAULT_HOUR_12, DEFAULT_LOCALE } from "../locale";
import { from_timestamp, to_timestamp } from "./timestamps";
const DEFAULT_FORMAT_OPTIONS = {
hour: DEFAULT_HOUR,
hour_12: DEFAULT_HOUR_12,
};
const RESET_UNITS = {
minute: 0,
second: 0,
};
export function add_hours(timestamp, hours) {
const time = from_timestamp(timestamp).add({ hours });
return to_timestamp(time);
}
export function format_hour(timestamp, locale = DEFAULT_LOCALE, options = DEFAULT_FORMAT_OPTIONS) {
const time = from_timestamp(timestamp);
return time.toLocaleString(locale, {
hour: options.hour,
hour12: options.hour_12,
});
}
export function get_hour(timestamp) {
return from_timestamp(timestamp).hour;
}
export function includes_hour(timestamp, targets) {
const source_time = from_timestamp(timestamp, RESET_UNITS);
return !!targets.find((target, index) => {
const target_time = from_timestamp(target, RESET_UNITS);
return source_time.equals(target_time);
});
}
export function is_hour(timestamp, target) {
const source_time = from_timestamp(timestamp, RESET_UNITS);
const target_time = from_timestamp(target, RESET_UNITS);
return source_time.equals(target_time);
}
export function is_hour_in_range(timestamp, minimum, maximum, inclusive = false) {
const time = from_timestamp(timestamp, RESET_UNITS);
if (maximum) {
const maximum_date = from_timestamp(maximum, RESET_UNITS);
if (Temporal.PlainTime.compare(maximum_date, time) < (inclusive ? 0 : 1))
return false;
}
if (minimum) {
const minimum_date = from_timestamp(minimum, RESET_UNITS);
if (Temporal.PlainTime.compare(minimum_date, time) > (inclusive ? 0 : -1))
return false;
}
return true;
}
export function now_hour() {
const time = Temporal.Now.plainTimeISO();
return to_timestamp(time, RESET_UNITS);
}
export function subtract_hours(timestamp, hours) {
const time = from_timestamp(timestamp).subtract({ hours });
return to_timestamp(time);
}