1import { TimeZone, DefaultTimeZone } from '../types/time'; 2 3/** 4 * Used for helper functions handling time zones. 5 * 6 * @public 7 */ 8export interface TimeZoneOptions { 9 /** 10 * Specify this if you want to override the timeZone used when parsing or formatting 11 * a date and time value. If no timeZone is set, the default timeZone for the current 12 * user is used. 13 */ 14 timeZone?: TimeZone; 15} 16 17/** 18 * The type describing date and time options. Used for all the helper functions 19 * available to parse or format date and time values. 20 * 21 * @public 22 */ 23export interface DateTimeOptions extends TimeZoneOptions { 24 /** 25 * Specify a {@link https://momentjs.com/docs/#/displaying/format | momentjs} format to 26 * use a custom formatting pattern or parsing pattern. If no format is set, 27 * then system configured default format is used. 28 */ 29 format?: string; 30} 31 32/** 33 * The type to describe the time zone resolver function that will be used to access 34 * the default time zone of a user. 35 * 36 * @public 37 */ 38export type TimeZoneResolver = () => TimeZone | undefined; 39 40let defaultTimeZoneResolver: TimeZoneResolver = () => DefaultTimeZone; 41 42/** 43 * Used by Grafana internals to set the {@link TimeZoneResolver} to access the current 44 * user timeZone. 45 * 46 * @internal 47 */ 48export const setTimeZoneResolver = (resolver: TimeZoneResolver) => { 49 defaultTimeZoneResolver = resolver ?? defaultTimeZoneResolver; 50}; 51 52/** 53 * Used to get the current selected time zone. If a valid time zone is passed in the 54 * options it will be returned. If no valid time zone is passed either the time zone 55 * configured for the user account will be returned or the default for Grafana. 56 * 57 * @public 58 */ 59export const getTimeZone = <T extends TimeZoneOptions>(options?: T): TimeZone => { 60 return options?.timeZone ?? defaultTimeZoneResolver() ?? DefaultTimeZone; 61}; 62