@@ -3,7 +3,14 @@ import * as log from "https://deno.land/std/log/mod.ts";
3
3
4
4
export class TimeService {
5
5
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
+ }
7
14
8
15
public static async getTimeByCountryAndCity ( countryCode : string , cityName : string ) : Promise < string > {
9
16
@@ -12,21 +19,9 @@ export class TimeService {
12
19
return TimeService . getTimeByTimeZone ( entry . timezone )
13
20
}
14
21
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 ) )
28
24
29
- log . warning ( minutes )
30
25
let getDifferenceToUtcInMilisec = minutes * 60000 ;
31
26
let getUTCMilisecond = new Date ( ) . getTime ( ) ;
32
27
@@ -36,6 +31,19 @@ export class TimeService {
36
31
return result . substr ( 11 , 8 )
37
32
}
38
33
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
+
39
47
public static isTimeZoneInDST ( timeZone : string ) : boolean {
40
48
return true // still figuring out a more sophisticated solution ;)
41
49
}
@@ -66,7 +74,7 @@ export class TimeService {
66
74
return entry . dayLightSavingTimeOffset
67
75
}
68
76
69
- private static convertOffsetToMinutes ( offsetString : string ) : number {
77
+ public static convertOffsetToMinutes ( offsetString : string ) : number {
70
78
const preFix = offsetString . substr ( 0 , 1 )
71
79
const hours = Number ( offsetString . substr ( 1 , 2 ) )
72
80
const minutes = Number ( offsetString . substr ( 4 , 2 ) )
@@ -79,8 +87,7 @@ export class TimeService {
79
87
80
88
private static async getTimeZoneEntry ( countryCode : string , cityName : string ) : Promise < any > {
81
89
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 ( )
84
91
85
92
const entry = allTimeZones . filter ( ( e : any ) => e . iso2 === countryCode && ( e . city === cityName || e . city_ascii === cityName ) ) [ 0 ]
86
93
if ( entry === undefined ) {
0 commit comments