mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-04 02:05:21 +03:00
added strftime option
This commit is contained in:
parent
037dc5fbbc
commit
401e3d3be7
@ -1,7 +1,8 @@
|
|||||||
|
import strftime from 'strftime';
|
||||||
|
import { fromUnixTime, intervalToDuration, formatDuration } from 'date-fns';
|
||||||
export const longToDate = (long: number): Date => new Date(long * 1000);
|
export const longToDate = (long: number): Date => new Date(long * 1000);
|
||||||
export const epochToLong = (date: number): number => date / 1000;
|
export const epochToLong = (date: number): number => date / 1000;
|
||||||
export const dateToLong = (date: Date): number => epochToLong(date.getTime());
|
export const dateToLong = (date: Date): number => epochToLong(date.getTime());
|
||||||
import { fromUnixTime, intervalToDuration, formatDuration } from 'date-fns';
|
|
||||||
|
|
||||||
const getDateTimeYesterday = (dateTime: Date): Date => {
|
const getDateTimeYesterday = (dateTime: Date): Date => {
|
||||||
const twentyFourHoursInMilliseconds = 24 * 60 * 60 * 1000;
|
const twentyFourHoursInMilliseconds = 24 * 60 * 60 * 1000;
|
||||||
@ -17,23 +18,33 @@ export const getNowYesterdayInLong = (): number => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function takes in a unix timestamp, locale, timezone,
|
* This function takes in a Unix timestamp, configuration options for date/time display, and an optional strftime format string,
|
||||||
* and returns a dateTime string.
|
* and returns a formatted date/time string.
|
||||||
* If unixTimestamp is not provided, it returns 'Invalid time'
|
*
|
||||||
* @param unixTimestamp: number
|
* If the Unix timestamp is not provided, it returns "Invalid time".
|
||||||
* @param locale: string
|
*
|
||||||
* @param timezone: string
|
* The configuration options determine how the date and time are formatted.
|
||||||
* @returns string - dateTime or 'Invalid time' if unixTimestamp is not provided
|
* The `timezone` option allows you to specify a specific timezone for the output, otherwise the user's browser timezone will be used.
|
||||||
|
* The `use12hour` option allows you to display time in a 12-hour format if true, and 24-hour format if false.
|
||||||
|
* The `dateStyle` and `timeStyle` options allow you to specify pre-defined formats for displaying the date and time.
|
||||||
|
* The `strftime_fmt` option allows you to specify a custom format using the strftime syntax.
|
||||||
|
*
|
||||||
|
* If both `strftime_fmt` and `dateStyle`/`timeStyle` are provided, `strftime_fmt` takes precedence.
|
||||||
|
*
|
||||||
|
* @param unixTimestamp The Unix timestamp to format
|
||||||
|
* @param config An object containing the configuration options for date/time display
|
||||||
|
* @returns The formatted date/time string, or "Invalid time" if the Unix timestamp is not provided or invalid.
|
||||||
*/
|
*/
|
||||||
interface DateTimeStyle {
|
interface DateTimeStyle {
|
||||||
timezone: string;
|
timezone: string;
|
||||||
use12hour: boolean | undefined;
|
use12hour: boolean | undefined;
|
||||||
dateStyle: 'full' | 'long' | 'medium' | 'short';
|
dateStyle: 'full' | 'long' | 'medium' | 'short';
|
||||||
timeStyle: 'full' | 'long' | 'medium' | 'short';
|
timeStyle: 'full' | 'long' | 'medium' | 'short';
|
||||||
|
strftime_fmt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const formatUnixTimestampToDateTime = (unixTimestamp: number, config: DateTimeStyle): string => {
|
export const formatUnixTimestampToDateTime = (unixTimestamp: number, config: DateTimeStyle): string => {
|
||||||
const { timezone, use12hour, dateStyle, timeStyle } = config;
|
const { timezone, use12hour, dateStyle, timeStyle, strftime_fmt } = config;
|
||||||
const locale = window.navigator?.language || 'en-US';
|
const locale = window.navigator?.language || 'en-US';
|
||||||
|
|
||||||
if (isNaN(unixTimestamp)) {
|
if (isNaN(unixTimestamp)) {
|
||||||
@ -41,6 +52,14 @@ export const formatUnixTimestampToDateTime = (unixTimestamp: number, config: Dat
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const date = new Date(unixTimestamp * 1000);
|
const date = new Date(unixTimestamp * 1000);
|
||||||
|
|
||||||
|
// use strftime_fmt if defined in config file
|
||||||
|
if (strftime_fmt) {
|
||||||
|
const strftime_locale = strftime.localizeByIdentifier(locale);
|
||||||
|
return strftime_locale(strftime_fmt, date);
|
||||||
|
}
|
||||||
|
|
||||||
|
// else use Intl.DateTimeFormat
|
||||||
const formatter = new Intl.DateTimeFormat(locale, {
|
const formatter = new Intl.DateTimeFormat(locale, {
|
||||||
dateStyle,
|
dateStyle,
|
||||||
timeStyle,
|
timeStyle,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user