Skip to content

Commit a3c2e97

Browse files
author
Michael Spengler
committed
adding get time by timezone
1 parent 4ba63dc commit a3c2e97

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ Deno.test("get time", async (): Promise<void> => {
2828
assertEquals(time.length, 8)
2929
});
3030

31+
Deno.test("get time by timezone", async (): Promise<void> => {
32+
33+
const timeZone = 'Europe/Berlin'
34+
35+
const time = await TimeService.getTimeByTimeZone(timeZone)
36+
37+
log.info(`In ${timeZone} it is ${time} o'Clock :)`)
38+
39+
assertEquals(time.length, 8)
40+
});
41+
3142

3243
Deno.test("get offset", async (): Promise<void> => {
3344

time-service.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@ export class TimeService {
1616
return result.substr(11, 8)
1717
}
1818

19+
public static async getTimeByTimeZone(timeZone: string): Promise<string> {
20+
21+
const allTimeZones = JSON.parse(await Persistence.readFromLocalFile(`${Deno.cwd()}/timezones.json`))
22+
23+
const entry = allTimeZones.filter((e: any) => e.timezone === timeZone)[0]
24+
25+
const minutes = Number(TimeService.convertOffsetToMinutes(entry.dayLightSavingTimeOffset).toString())
26+
27+
log.warning(minutes)
28+
let getDifferenceToUtcInMilisec = minutes * 60000;
29+
let getUTCMilisecond = new Date().getTime();
30+
31+
const result = new Date(getUTCMilisecond - getDifferenceToUtcInMilisec).toISOString()
32+
33+
34+
return result.substr(11, 8)
35+
}
36+
1937
public static async getTimeZone(countryCode: string, cityName: string): Promise<string> {
2038

2139
const entry = await TimeService.getTimeZoneEntry(countryCode, cityName)
@@ -54,9 +72,8 @@ export class TimeService {
5472
}
5573

5674
private static async getTimeZoneEntry(countryCode: string, cityName: string): Promise<any> {
57-
const myPath = `${Deno.cwd()}/timezones.json`
58-
const allTimeZones = JSON.parse(await Persistence.readFromLocalFile(myPath))
5975

76+
const allTimeZones = JSON.parse(await Persistence.readFromLocalFile(`${Deno.cwd()}/timezones.json`))
6077

6178
const entry = allTimeZones.filter((e: any) => e.iso2 === countryCode && (e.city === cityName || e.city_ascii === cityName))[0]
6279
if (entry === undefined) {
@@ -65,5 +82,4 @@ export class TimeService {
6582

6683
return entry
6784
}
68-
6985
}

0 commit comments

Comments
 (0)