vitess.io/vitess@v0.16.2/web/vtadmin/src/util/time.ts (about) 1 /** 2 * Copyright 2021 The Vitess Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 import * as dayjs from 'dayjs'; 17 import localizedFormat from 'dayjs/plugin/localizedFormat'; 18 import relativeTime from 'dayjs/plugin/relativeTime'; 19 20 dayjs.extend(localizedFormat); 21 dayjs.extend(relativeTime); 22 23 export const parse = (timestamp: number | Long | null | undefined): dayjs.Dayjs | null => { 24 if (typeof timestamp !== 'number') { 25 return null; 26 } 27 return dayjs.unix(timestamp); 28 }; 29 30 export const format = (timestamp: number | Long | null | undefined, template: string | undefined): string | null => { 31 const u = parse(timestamp); 32 return u ? u.format(template) : null; 33 }; 34 35 export const formatDateTime = (timestamp: number | Long | null | undefined): string | null => { 36 return format(timestamp, 'YYYY-MM-DD LT Z'); 37 }; 38 39 export const formatRelativeTime = (timestamp: number | Long | null | undefined): string | null => { 40 const u = parse(timestamp); 41 return u ? u.fromNow() : null; 42 };