( timestamp: any, props?: IOptionalDateTimeFieldProperty, userTimeZone?: string, )
| 149 | }; |
| 150 | |
| 151 | export const dateTimeFormat = ( |
| 152 | timestamp: any, |
| 153 | props?: IOptionalDateTimeFieldProperty, |
| 154 | userTimeZone?: string, |
| 155 | ) => { |
| 156 | if (!timestamp) { |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | props = props || DEFAULT_DATETIME_PROPS; |
| 161 | const dateFormat = DateFormat[props.dateFormat || DEFAULT_DATETIME_PROPS.dateFormat]; |
| 162 | const timeFormat = TimeFormat[props.timeFormat || DEFAULT_DATETIME_PROPS.timeFormat]; |
| 163 | let format = dateFormat + (props.includeTime ? ' ' + timeFormat : ''); |
| 164 | |
| 165 | if (props.includeTime && timeFormat === 'hh:mm') { |
| 166 | const locale = getLanguage(); |
| 167 | const formatLocale = locale.toLowerCase().split('_').join('-'); |
| 168 | dayjs.locale(formatLocale); |
| 169 | format += ' a'; |
| 170 | } |
| 171 | let timeZone = props.timeZone || userTimeZone; |
| 172 | if (isServer()) { |
| 173 | timeZone = timeZone || DEFAULT_TIME_ZONE; |
| 174 | } |
| 175 | try { |
| 176 | if (props.includeTimeZone) { |
| 177 | timeZone = timeZone || DEFAULT_DATETIME_PROPS.timeZone; |
| 178 | const abbr = getTimeZoneAbbrByUtc(timeZone)!; |
| 179 | return dfzFormatWithValid(utcToZonedTime(Number(timestamp), timeZone), covertDayjsFormat2DateFnsFormat(format), timeZone ) + ` (${abbr})`; |
| 180 | } |
| 181 | if (!props.includeTimeZone && timeZone) { |
| 182 | // dayjs(Number(timestamp)).tz(timeZone).format(format); |
| 183 | // Frequent invocations of the "tz" function here will result in severe page lag. |
| 184 | return dfzFormatWithValid(utcToZonedTime(Number(timestamp), timeZone), covertDayjsFormat2DateFnsFormat(format), timeZone ); |
| 185 | } |
| 186 | } catch (e) { |
| 187 | if (e instanceof RangeError) { |
| 188 | return 'Invalid Date'; |
| 189 | } |
| 190 | throw e; |
| 191 | } |
| 192 | return dfzFormatWithValid(timestamp, covertDayjsFormat2DateFnsFormat(format)); |
| 193 | }; |
| 194 | |
| 195 | const withTimeZone = (timestamp: number | undefined | string, timeZone?: string, locale?: string) => { |
| 196 | if (timeZone) { |
no test coverage detected