anki/ts/lib/time.ts

204 lines
5.9 KiB
TypeScript
Raw Normal View History

2020-06-27 11:24:49 +02:00
// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
Refactor i18n (#1405) Merging note: the typing changes were fixed in a separate PR. * Put rootDirs into subprojects - typings do not work for any ts or svelte files - if we set the 'rootDirs' in ts/tsconfig.json to '../bazel-bin/ts' and then inherit them from e.g. editor, the root will be changed to '../../bazel-bin/ts', however editor needs look in '../../bazel-bin/ts/editor' instead. * Rename i18n and i18n_helpers to i18n-generated and i18n - This way, we can restrict the awkwardness of importing files outside the ts directory within lib * Fix missing typing of i18n and backend_proto by adding back symlinks * Split up i18n-generated into i18n-{translate,modules} * Change i18n from singleton to functions * Revert "Put rootDirs into subprojects" This partially reverts commit e1d4292ce3979e7b7ee21bf3951b8a462d45c29c. It seems like this might not be necessary after all. However some other change made on this branch seems to have fixed the .svelte.d.ts imports * Introduce i18n-bundles to remove circular import There was a circular import i18n.ts <-> i18n-translate.ts * Create own directory for i18n * Move lib/i18n/translate to lib/translate * This restores tree shaking * Update tsconfig libs and module * es2018-2020 have wide support on all modern browsers including * Switch bundles and langs inside i18n to variables again * Add missing copyright header * Rename translate.ts to ftl.ts * Remove the symlinks again I added them to fix to have completion for tr, however this would have also have meant to abandon the tree shaking. As we want to have tree shaking, it's also not necessary to have the symlinks anymore * Revert "Update tsconfig libs and module" This reverts commit 0a96776a475e9901c1f9f3407c726d1d002fb9ef. * move withCollapsedWhitespace back to i18n/utils * Add back /ts as in rootDirs
2021-10-07 15:31:49 +02:00
import * as tr from "./ftl";
2020-06-27 11:24:49 +02:00
2020-06-27 14:10:56 +02:00
export const SECOND = 1.0;
export const MINUTE = 60.0 * SECOND;
export const HOUR = 60.0 * MINUTE;
export const DAY = 24.0 * HOUR;
export const MONTH = 30.0 * DAY;
export const YEAR = 12.0 * MONTH;
2020-06-27 11:24:49 +02:00
export enum TimespanUnit {
2020-06-27 11:24:49 +02:00
Seconds,
Minutes,
Hours,
Days,
Months,
Years,
}
export function unitName(unit: TimespanUnit): string {
2020-06-27 11:24:49 +02:00
switch (unit) {
case TimespanUnit.Seconds:
return "seconds";
case TimespanUnit.Minutes:
return "minutes";
case TimespanUnit.Hours:
return "hours";
case TimespanUnit.Days:
return "days";
case TimespanUnit.Months:
return "months";
case TimespanUnit.Years:
return "years";
}
}
export function naturalUnit(secs: number): TimespanUnit {
2020-06-27 11:24:49 +02:00
secs = Math.abs(secs);
if (secs < MINUTE) {
return TimespanUnit.Seconds;
} else if (secs < HOUR) {
return TimespanUnit.Minutes;
} else if (secs < DAY) {
return TimespanUnit.Hours;
} else if (secs < MONTH) {
return TimespanUnit.Days;
} else if (secs < YEAR) {
return TimespanUnit.Months;
} else {
return TimespanUnit.Years;
}
}
/// Number of seconds in a given unit.
export function unitSeconds(unit: TimespanUnit): number {
2020-06-27 11:24:49 +02:00
switch (unit) {
case TimespanUnit.Seconds:
return SECOND;
2020-06-27 11:24:49 +02:00
case TimespanUnit.Minutes:
return MINUTE;
2020-06-27 11:24:49 +02:00
case TimespanUnit.Hours:
return HOUR;
2020-06-27 11:24:49 +02:00
case TimespanUnit.Days:
return DAY;
2020-06-27 11:24:49 +02:00
case TimespanUnit.Months:
return MONTH;
2020-06-27 11:24:49 +02:00
case TimespanUnit.Years:
return YEAR;
}
}
export function unitAmount(unit: TimespanUnit, secs: number): number {
return secs / unitSeconds(unit);
}
/// Largest unit provided seconds can be divided by without a remainder.
export function naturalWholeUnit(secs: number): TimespanUnit {
let unit = naturalUnit(secs);
while (unit != TimespanUnit.Seconds) {
const amount = Math.round(unitAmount(unit, secs));
if (Math.abs(secs - amount * unitSeconds(unit)) < Number.EPSILON) {
return unit;
}
unit -= 1;
2020-06-27 11:24:49 +02:00
}
return unit;
2020-06-27 11:24:49 +02:00
}
export function studiedToday(cards: number, secs: number): string {
2020-06-27 11:24:49 +02:00
const unit = naturalUnit(secs);
const amount = unitAmount(unit, secs);
const name = unitName(unit);
let secsPer = 0;
if (cards > 0) {
secsPer = secs / cards;
}
return tr.statisticsStudiedToday({
unit: name,
secsPerCard: secsPer,
2020-06-27 11:24:49 +02:00
cards,
amount,
});
}
2020-06-27 14:10:56 +02:00
2021-03-26 10:24:48 +01:00
function i18nFuncForUnit(
unit: TimespanUnit,
short: boolean,
2021-03-26 10:24:48 +01:00
): ({ amount: number }) => string {
if (short) {
switch (unit) {
case TimespanUnit.Seconds:
return tr.statisticsElapsedTimeSeconds;
case TimespanUnit.Minutes:
return tr.statisticsElapsedTimeMinutes;
case TimespanUnit.Hours:
return tr.statisticsElapsedTimeHours;
case TimespanUnit.Days:
return tr.statisticsElapsedTimeDays;
case TimespanUnit.Months:
return tr.statisticsElapsedTimeMonths;
case TimespanUnit.Years:
return tr.statisticsElapsedTimeYears;
}
} else {
switch (unit) {
case TimespanUnit.Seconds:
return tr.schedulingTimeSpanSeconds;
case TimespanUnit.Minutes:
return tr.schedulingTimeSpanMinutes;
case TimespanUnit.Hours:
return tr.schedulingTimeSpanHours;
case TimespanUnit.Days:
return tr.schedulingTimeSpanDays;
case TimespanUnit.Months:
return tr.schedulingTimeSpanMonths;
case TimespanUnit.Years:
return tr.schedulingTimeSpanYears;
}
}
}
2020-06-27 14:10:56 +02:00
/// Describe the given seconds using the largest appropriate unit.
/// If precise is true, show to two decimal places, eg
/// eg 70 seconds -> "1.17 minutes"
/// If false, seconds and days are shown without decimals.
export function timeSpan(seconds: number, short = false): string {
2020-06-27 14:10:56 +02:00
const unit = naturalUnit(seconds);
const amount = unitAmount(unit, seconds);
return i18nFuncForUnit(unit, short)({ amount });
2020-06-27 14:10:56 +02:00
}
2020-06-28 11:34:19 +02:00
export function dayLabel(daysStart: number, daysEnd: number): string {
2020-06-28 11:34:19 +02:00
const larger = Math.max(Math.abs(daysStart), Math.abs(daysEnd));
const smaller = Math.min(Math.abs(daysStart), Math.abs(daysEnd));
if (larger - smaller <= 1) {
// singular
if (daysStart >= 0) {
return tr.statisticsInDaysSingle({ days: daysStart });
2020-06-28 11:34:19 +02:00
} else {
return tr.statisticsDaysAgoSingle({ days: -daysStart });
2020-06-28 11:34:19 +02:00
}
} else {
// range
if (daysStart >= 0) {
return tr.statisticsInDaysRange({
daysStart,
daysEnd: daysEnd - 1,
});
2020-06-28 11:34:19 +02:00
} else {
return tr.statisticsDaysAgoRange({
daysStart: Math.abs(daysEnd - 1),
daysEnd: -daysStart,
2020-06-28 11:34:19 +02:00
});
}
}
}
/** Helper for converting Unix timestamps to date strings. */
export class Timestamp {
private date: Date;
constructor(seconds: number) {
this.date = new Date(seconds * 1000);
}
/** YYYY-MM-DD */
dateString(): string {
const year = this.date.getFullYear();
const month = ("0" + (this.date.getMonth() + 1)).slice(-2);
const date = ("0" + this.date.getDate()).slice(-2);
return `${year}-${month}-${date}`;
}
/** HH:MM */
timeString(): string {
const hours = ("0" + this.date.getHours()).slice(-2);
const minutes = ("0" + this.date.getMinutes()).slice(-2);
return `${hours}:${minutes}`;
}
}