platform/packages/core/src/time.ts
Kristina c0348bf313
UBERF-6829: group messages of the same type and user (#5569)
Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
2024-05-10 18:58:02 +07:00

59 lines
1.7 KiB
TypeScript

//
// Copyright © 2023 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
//
import { Timestamp } from './classes'
export function getDay (time: Timestamp): Timestamp {
const date: Date = new Date(time)
date.setHours(0, 0, 0, 0)
return date.getTime()
}
export function getHour (time: Timestamp): Timestamp {
const date: Date = new Date(time)
date.setMinutes(0, 0, 0)
return date.getTime()
}
export function getDisplayTime (time: number): string {
let options: Intl.DateTimeFormatOptions = { hour: 'numeric', minute: 'numeric' }
if (!isToday(time)) {
options = {
month: 'numeric',
day: 'numeric',
...options
}
}
return new Date(time).toLocaleString('default', options)
}
export function isOtherDay (time1: Timestamp, time2: Timestamp): boolean {
return getDay(time1) !== getDay(time2)
}
export function isOtherHour (time1: Timestamp, time2: Timestamp): boolean {
return getHour(time1) !== getHour(time2)
}
function isToday (time: number): boolean {
const current = new Date()
const target = new Date(time)
return (
current.getDate() === target.getDate() &&
current.getMonth() === target.getMonth() &&
current.getFullYear() === target.getFullYear()
)
}