my-timezone 1.5.3 → 1.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/MyTimezone.js +58 -74
- package/dist/cjs/MyTimezone.test.js +11 -20
- package/dist/cjs/cli.js +4 -13
- package/dist/esm/MyTimezone.js +4 -1
- package/package.json +8 -8
package/dist/cjs/MyTimezone.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import axios from 'axios';
|
|
11
2
|
import { NTPClient } from 'ntpclient';
|
|
12
3
|
export var DIRECTION;
|
|
@@ -21,73 +12,68 @@ const defaultConfig = {
|
|
|
21
12
|
const nominatimAPI = 'https://nominatim.openstreetmap.org';
|
|
22
13
|
export class MyTimezone {
|
|
23
14
|
constructor(config) {
|
|
24
|
-
this.config =
|
|
15
|
+
this.config = {
|
|
16
|
+
...defaultConfig,
|
|
17
|
+
...config,
|
|
18
|
+
};
|
|
25
19
|
this.ntpClient = new NTPClient(this.config.ntpServer);
|
|
26
20
|
}
|
|
27
|
-
getLocation(location) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return this.getLocationByName(location);
|
|
36
|
-
}
|
|
37
|
-
throw error;
|
|
21
|
+
async getLocation(location) {
|
|
22
|
+
try {
|
|
23
|
+
const coordinates = this.parseCoordinates(location);
|
|
24
|
+
return coordinates;
|
|
25
|
+
}
|
|
26
|
+
catch (error) {
|
|
27
|
+
if (error.message.includes('No coordinates parsed')) {
|
|
28
|
+
return this.getLocationByName(location);
|
|
38
29
|
}
|
|
39
|
-
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
40
32
|
}
|
|
41
|
-
getLocationByName(address, radius) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
};
|
|
73
|
-
});
|
|
33
|
+
async getLocationByName(address, radius) {
|
|
34
|
+
const requestConfig = {
|
|
35
|
+
method: 'get',
|
|
36
|
+
params: {
|
|
37
|
+
format: 'json',
|
|
38
|
+
limit: 9,
|
|
39
|
+
// eslint-disable-next-line id-length
|
|
40
|
+
q: address,
|
|
41
|
+
},
|
|
42
|
+
url: `${nominatimAPI}/search`,
|
|
43
|
+
};
|
|
44
|
+
if (radius) {
|
|
45
|
+
requestConfig.params.radius = radius;
|
|
46
|
+
}
|
|
47
|
+
let results;
|
|
48
|
+
try {
|
|
49
|
+
const response = await axios.request(requestConfig);
|
|
50
|
+
results = response.data;
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
throw new Error(`Nominatim API Error: ${error.message}`);
|
|
54
|
+
}
|
|
55
|
+
if (!results.length) {
|
|
56
|
+
throw new Error('No place found.');
|
|
57
|
+
}
|
|
58
|
+
const { display_name, lon } = results[0];
|
|
59
|
+
const parsedLongitude = parseFloat(lon);
|
|
60
|
+
return {
|
|
61
|
+
formattedAddress: display_name,
|
|
62
|
+
longitude: parsedLongitude,
|
|
63
|
+
};
|
|
74
64
|
}
|
|
75
|
-
getDateByAddress(address) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
return this.getDateByLongitude(longitude);
|
|
79
|
-
});
|
|
65
|
+
async getDateByAddress(address) {
|
|
66
|
+
const { longitude } = await this.getLocationByName(address);
|
|
67
|
+
return this.getDateByLongitude(longitude);
|
|
80
68
|
}
|
|
81
|
-
getDateByLongitude(longitude) {
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
return calculatedDate;
|
|
90
|
-
});
|
|
69
|
+
async getDateByLongitude(longitude) {
|
|
70
|
+
const direction = longitude < 0 ? DIRECTION.WEST : DIRECTION.EAST;
|
|
71
|
+
const utcDate = await this.getUTCDate();
|
|
72
|
+
const offsetMillis = this.getOffsetMillis(longitude, direction);
|
|
73
|
+
const calculatedDate = direction === DIRECTION.EAST
|
|
74
|
+
? new Date(utcDate.getTime() + offsetMillis)
|
|
75
|
+
: new Date(utcDate.getTime() - offsetMillis);
|
|
76
|
+
return calculatedDate;
|
|
91
77
|
}
|
|
92
78
|
parseCoordinates(coordinates) {
|
|
93
79
|
var _a;
|
|
@@ -122,9 +108,7 @@ export class MyTimezone {
|
|
|
122
108
|
const offsetHours = (dir * longitudeDegrees * dayInHours) / degreesOnEarth;
|
|
123
109
|
return offsetHours * oneHourInMillis;
|
|
124
110
|
}
|
|
125
|
-
getUTCDate() {
|
|
126
|
-
return
|
|
127
|
-
return this.config.offline ? new Date() : this.ntpClient.getNetworkTime();
|
|
128
|
-
});
|
|
111
|
+
async getUTCDate() {
|
|
112
|
+
return this.config.offline ? new Date() : this.ntpClient.getNetworkTime();
|
|
129
113
|
}
|
|
130
114
|
}
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
/* eslint-disable no-magic-numbers */
|
|
11
2
|
import { expect, describe, test, vi } from 'vitest';
|
|
12
3
|
import nock from 'nock';
|
|
@@ -46,24 +37,24 @@ describe('MyTimezone', () => {
|
|
|
46
37
|
],
|
|
47
38
|
])
|
|
48
39
|
.persist();
|
|
49
|
-
test('returns an address from Nominatim', () =>
|
|
40
|
+
test('returns an address from Nominatim', async () => {
|
|
50
41
|
const location = 'Berlin, Germany';
|
|
51
|
-
const { formattedAddress } =
|
|
42
|
+
const { formattedAddress } = await tz.getLocationByName(location);
|
|
52
43
|
expect(formattedAddress).toBe(location);
|
|
53
|
-
})
|
|
54
|
-
test('returns the correct time for a location', () =>
|
|
55
|
-
const berlinTime =
|
|
44
|
+
});
|
|
45
|
+
test('returns the correct time for a location', async () => {
|
|
46
|
+
const berlinTime = await tz.getDateByLongitude(13.40803);
|
|
56
47
|
//console.log('Timezone at 52.51848, 13.40803:', berlinTime.toString());
|
|
57
|
-
const frankfurtTime =
|
|
48
|
+
const frankfurtTime = await tz.getDateByLongitude(8.67931);
|
|
58
49
|
//console.log('Timezone at 50.11796, 8.67931:', frankfurtTime.toString());
|
|
59
50
|
expect(frankfurtTime.getTime() < berlinTime.getTime()).toBe(true);
|
|
60
|
-
})
|
|
61
|
-
test('returns the time for an address', () =>
|
|
62
|
-
const dataBerlin =
|
|
51
|
+
});
|
|
52
|
+
test('returns the time for an address', async () => {
|
|
53
|
+
const dataBerlin = await tz.getDateByAddress('Berlin, Germany');
|
|
63
54
|
expect(dataBerlin).toBeDefined();
|
|
64
55
|
//console.log('Timezone Berlin:', dataBerlin.toString());
|
|
65
|
-
const dataMinsk =
|
|
56
|
+
const dataMinsk = await tz.getDateByAddress('Minsk, Belarus');
|
|
66
57
|
expect(dataMinsk).toBeDefined();
|
|
67
58
|
//console.log('Timezone Minsk:', dataMinsk.toString());
|
|
68
|
-
})
|
|
59
|
+
});
|
|
69
60
|
});
|
package/dist/cjs/cli.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
import { program as commander } from 'commander';
|
|
12
3
|
import { createRequire } from 'module';
|
|
13
4
|
const require = createRequire(import.meta.url);
|
|
@@ -30,10 +21,10 @@ const myTimezone = new MyTimezone({
|
|
|
30
21
|
ntpServer: commander.opts().server,
|
|
31
22
|
offline: !!commander.opts().offline,
|
|
32
23
|
});
|
|
33
|
-
void (() =>
|
|
24
|
+
void (async () => {
|
|
34
25
|
try {
|
|
35
|
-
const { longitude } =
|
|
36
|
-
const date =
|
|
26
|
+
const { longitude } = await myTimezone.getLocation(location);
|
|
27
|
+
const date = await myTimezone.getDateByLongitude(longitude);
|
|
37
28
|
const parsedDate = myTimezone.parseDate(date);
|
|
38
29
|
const formattedDate = `${parsedDate.year}-${parsedDate.month}-${parsedDate.day}`;
|
|
39
30
|
const formattedTime = `${parsedDate.hours}:${parsedDate.minutes}:${parsedDate.seconds}`;
|
|
@@ -44,4 +35,4 @@ void (() => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
44
35
|
console.error(error.message);
|
|
45
36
|
process.exit(1);
|
|
46
37
|
}
|
|
47
|
-
})
|
|
38
|
+
})();
|
package/dist/esm/MyTimezone.js
CHANGED
|
@@ -12,7 +12,10 @@ const defaultConfig = {
|
|
|
12
12
|
const nominatimAPI = 'https://nominatim.openstreetmap.org';
|
|
13
13
|
export class MyTimezone {
|
|
14
14
|
constructor(config) {
|
|
15
|
-
this.config =
|
|
15
|
+
this.config = {
|
|
16
|
+
...defaultConfig,
|
|
17
|
+
...config,
|
|
18
|
+
};
|
|
16
19
|
this.ntpClient = new NTPClient(this.config.ntpServer);
|
|
17
20
|
}
|
|
18
21
|
async getLocation(location) {
|
package/package.json
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
"author": "Florian Imdahl <git@ffflorian.de>",
|
|
3
3
|
"bin": "dist/cjs/cli.js",
|
|
4
4
|
"dependencies": {
|
|
5
|
-
"axios": "1.
|
|
6
|
-
"commander": "12.
|
|
7
|
-
"ntpclient": "1.6.
|
|
5
|
+
"axios": "1.7.2",
|
|
6
|
+
"commander": "12.1.0",
|
|
7
|
+
"ntpclient": "1.6.5"
|
|
8
8
|
},
|
|
9
9
|
"description": "Get the exact time based on your location.",
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"nock": "13.5.4",
|
|
12
|
-
"rimraf": "5.0.
|
|
13
|
-
"typescript": "5.
|
|
14
|
-
"vitest": "1.
|
|
12
|
+
"rimraf": "5.0.7",
|
|
13
|
+
"typescript": "5.5.2",
|
|
14
|
+
"vitest": "1.6.0"
|
|
15
15
|
},
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">= 18.0"
|
|
@@ -41,6 +41,6 @@
|
|
|
41
41
|
"test": "vitest run"
|
|
42
42
|
},
|
|
43
43
|
"type": "module",
|
|
44
|
-
"version": "1.5.
|
|
45
|
-
"gitHead": "
|
|
44
|
+
"version": "1.5.5",
|
|
45
|
+
"gitHead": "f7a6a79286e4eb85392b5f2d33942ab166142109"
|
|
46
46
|
}
|