matcher/src/modules/ad/infrastructure/time-converter.ts

58 lines
1.7 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { DateTime, TimeZone } from 'timezonecomplete';
import { TimeConverterPort } from '../core/application/ports/time-converter.port';
@Injectable()
export class TimeConverter implements TimeConverterPort {
private readonly UNIX_EPOCH = '1970-01-01';
localStringTimeToUtcStringTime = (time: string, timezone: string): string =>
new DateTime(`${this.UNIX_EPOCH}T${time}`, TimeZone.zone(timezone))
.convert(TimeZone.zone('UTC'))
.format('HH:mm');
utcStringTimeToLocalStringTime = (time: string, timezone: string): string =>
new DateTime(`${this.UNIX_EPOCH}T${time}`, TimeZone.zone('UTC'))
.convert(TimeZone.zone(timezone))
.format('HH:mm');
localStringDateTimeToUtcDate = (
date: string,
time: string,
timezone: string,
dst = false,
): Date =>
new Date(
new DateTime(
`${date}T${time}`,
TimeZone.zone(timezone, dst),
).toIsoString(),
);
utcStringDateTimeToLocalIsoString = (
date: string,
time: string,
timezone: string,
dst = false,
): string =>
new DateTime(`${date}T${time}`, TimeZone.zone('UTC'))
.convert(TimeZone.zone(timezone, dst))
.toIsoString();
utcUnixEpochDayFromTime = (time: string, timezone: string): number =>
new Date(
new DateTime(`${this.UNIX_EPOCH}T${time}`, TimeZone.zone(timezone, false))
.convert(TimeZone.zone('UTC'))
.toIsoString()
.split('T')[0],
).getUTCDay();
localUnixEpochDayFromTime = (time: string, timezone: string): number =>
new Date(
new DateTime(`${this.UNIX_EPOCH}T${time}`, TimeZone.zone('UTC'))
.convert(TimeZone.zone(timezone))
.toIsoString()
.split('T')[0],
).getUTCDay();
}