Skip to content

Commit df00827

Browse files
author
Michael Spengler
committed
adding some convenience
1 parent caab572 commit df00827

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import * as log from "https://deno.land/std/log/mod.ts";
33
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
44

55

6+
Deno.test("get time by offset", async (): Promise<void> => {
7+
8+
const offset = '+02:00'
9+
10+
const time = await TimeService.getTimeByOffset(offset)
11+
12+
log.info(`The time for offset ${offset} is ${time}`)
13+
14+
});
15+
616
Deno.test("get timezone", async (): Promise<void> => {
717

818
const countryCode = 'DE'

time-service.ts

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@ import * as log from "https://deno.land/std/log/mod.ts";
33

44
export class TimeService {
55

6-
public static readonly pathToTimeZonesFile = 'https://raw.githubusercontent.com/michael-spengler/time/master/timezones.json'
6+
7+
public static async getAllTimeZoneEntries(): Promise<any[]> {
8+
const pathToTimeZonesFile = 'https://raw.githubusercontent.com/michael-spengler/time/master/timezones.json'
9+
10+
const allTimeZoneEntries = JSON.parse(await Persistence.readFromRemoteFile(pathToTimeZonesFile))
11+
12+
return allTimeZoneEntries
13+
}
714

815
public static async getTimeByCountryAndCity(countryCode: string, cityName: string): Promise<string> {
916

@@ -12,21 +19,9 @@ export class TimeService {
1219
return TimeService.getTimeByTimeZone(entry.timezone)
1320
}
1421

15-
public static async getTimeByTimeZone(timeZone: string): Promise<string> {
16-
17-
// const allTimeZones = JSON.parse(await Persistence.readFromLocalFile(`${Deno.cwd()}/timezones.json`))
18-
const allTimeZones = JSON.parse(await Persistence.readFromRemoteFile(TimeService.pathToTimeZonesFile))
19-
20-
const entry = allTimeZones.filter((e: any) => e.timezone === timeZone)[0]
21-
22-
let minutes
23-
if (TimeService.isTimeZoneInDST(timeZone)) {
24-
minutes = Number(TimeService.convertOffsetToMinutes(entry.dayLightSavingTimeOffset).toString())
25-
} else {
26-
minutes = Number(TimeService.convertOffsetToMinutes(entry.offset).toString())
27-
}
22+
public static getTimeByOffset(offset: string) {
23+
const minutes = Number(TimeService.convertOffsetToMinutes(offset))
2824

29-
log.warning(minutes)
3025
let getDifferenceToUtcInMilisec = minutes * 60000;
3126
let getUTCMilisecond = new Date().getTime();
3227

@@ -36,6 +31,19 @@ export class TimeService {
3631
return result.substr(11, 8)
3732
}
3833

34+
public static async getTimeByTimeZone(timeZone: string): Promise<string> {
35+
36+
const allTimeZones = await TimeService.getAllTimeZoneEntries()
37+
38+
const entry = allTimeZones.filter((e: any) => e.timezone === timeZone)[0]
39+
40+
if (TimeService.isTimeZoneInDST(timeZone)) {
41+
return TimeService.getTimeByOffset(entry.dayLightSavingTimeOffset)
42+
} else {
43+
return TimeService.getTimeByOffset(entry.offset)
44+
}
45+
}
46+
3947
public static isTimeZoneInDST(timeZone: string): boolean {
4048
return true // still figuring out a more sophisticated solution ;)
4149
}
@@ -66,7 +74,7 @@ export class TimeService {
6674
return entry.dayLightSavingTimeOffset
6775
}
6876

69-
private static convertOffsetToMinutes(offsetString: string): number {
77+
public static convertOffsetToMinutes(offsetString: string): number {
7078
const preFix = offsetString.substr(0, 1)
7179
const hours = Number(offsetString.substr(1, 2))
7280
const minutes = Number(offsetString.substr(4, 2))
@@ -79,8 +87,7 @@ export class TimeService {
7987

8088
private static async getTimeZoneEntry(countryCode: string, cityName: string): Promise<any> {
8189

82-
// const allTimeZones = JSON.parse(await Persistence.readFromLocalFile(`${Deno.cwd()}/timezones.json`))
83-
const allTimeZones = JSON.parse(await Persistence.readFromRemoteFile(TimeService.pathToTimeZonesFile))
90+
const allTimeZones = await TimeService.getAllTimeZoneEntries()
8491

8592
const entry = allTimeZones.filter((e: any) => e.iso2 === countryCode && (e.city === cityName || e.city_ascii === cityName))[0]
8693
if (entry === undefined) {

0 commit comments

Comments
 (0)