Skip to content

Commit 50ed5a0

Browse files
authored
Add negative length option to toIsoString(). Add Timezone.version. Trim spurious beginning transition entry. Update to use timezone data generated by @tubular/time-tzdb. (#7)
1 parent 9a7f3c8 commit 50ed5a0

10 files changed

Lines changed: 1285 additions & 1353 deletions

.eslintrc.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"node": true
66
},
77
"extends": [
8-
"standard",
98
"plugin:@typescript-eslint/eslint-recommended"
109
],
1110
"globals": {

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,8 @@ toDate(): Date;
11251125
toHoursAndMinutesString(includeDst = false): string;
11261126

11271127
// Format as 'Y-MM-DDTHH:mm:ss.SSSZ', trimming to an optional maxLength that *does not* count
1128-
// any leading + or - sign.
1128+
// any leading + or - sign. If maxLength is negative, remove that many characters from the
1129+
// end of the full string. Base length for positive years <= 9999 is 24 characters.
11291130
toIsoString(maxLength?: number): string;
11301131

11311132
// Create a clone of a DateTime instance with a different locale.
@@ -1168,6 +1169,12 @@ addDaysToDate(deltaDays: number, yearOrDate: YearOrDate, month?: number, day?: n
11681169
static DATELESS: Timezone; // A pseudo timezone for abstract dateless, time-only `DateTime` instances.
11691170
```
11701171

1172+
### Static `Timezone` getter
1173+
1174+
```typescript
1175+
version: string; // Current timezone version, e.g. 2021a
1176+
```
1177+
11711178
### Static `Timezone` methods
11721179

11731180
Check if a given IANA `zoneName` is associated with an ISO Alpha-2 (two-letter) `country` code:

package-lock.json

Lines changed: 463 additions & 552 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@tubular/time",
3-
"version": "2.4.2",
3+
"version": "2.5.0",
44
"description": "Date/time, IANA timezones, calendar with settable Julian/Gregorian switchover",
55
"browser": "dist/web/index.js",
66
"browser-es5": "dist/web5/index.js",
@@ -35,7 +35,7 @@
3535
"license": "MIT",
3636
"dependencies": {
3737
"@tubular/math": "^2.2.1",
38-
"@tubular/util": "^3.4.1"
38+
"@tubular/util": "^3.5.0"
3939
},
4040
"devDependencies": {
4141
"@babel/core": "^7.13.8",
@@ -50,12 +50,10 @@
5050
"by-request": "^1.2.0",
5151
"chai": "^4.2.0",
5252
"eslint": "^7.21.0",
53-
"eslint-config-standard": "^16.0.2",
5453
"eslint-plugin-chai-friendly": "^0.6.0",
5554
"eslint-plugin-import": "^2.22.1",
5655
"eslint-plugin-node": "^11.1.0",
5756
"eslint-plugin-promise": "^4.2.1",
58-
"eslint-plugin-standard": "^5.0.0",
5957
"esm": "^3.2.25",
6058
"mocha": "^8.2.1",
6159
"nyc": "^15.1.0",

src/date-time.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,9 @@ export class DateTime extends Calendar {
13971397
let s = this.format(undefined, 'en-US');
13981398

13991399
if (maxLength != null) {
1400-
if (/^[-+]/.test(s))
1400+
if (maxLength < 0)
1401+
maxLength = s.length + maxLength;
1402+
else if (/^[-+]/.test(s))
14011403
++maxLength;
14021404

14031405
s = s.substr(0, maxLength);

src/timezone-large-alt.ts

Lines changed: 250 additions & 250 deletions
Large diffs are not rendered by default.

src/timezone-large.ts

Lines changed: 274 additions & 274 deletions
Large diffs are not rendered by default.

src/timezone-small.ts

Lines changed: 268 additions & 268 deletions
Large diffs are not rendered by default.

src/timezone.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { expect } from 'chai';
22
import { Timezone } from './timezone';
33
import timezoneLarge from './timezone-large';
44

5+
const version = timezoneLarge.version;
6+
57
describe('Timezone', () => {
68
beforeEach(() => Timezone.defineTimezones(Object.assign({
79
'America/Barberg': 'America/Chicago',
@@ -10,6 +12,7 @@ describe('Timezone', () => {
1012

1113
it('should properly create Timezone instances', () => {
1214
expect(Timezone.from('America/New_York').aliasFor).to.equal(null);
15+
expect(Timezone.from('Antarctica/Troll').aliasFor).to.equal(null);
1316
expect(Timezone.from('America/New_York').population).to.be.greaterThan(20000000);
1417
expect(Timezone.from('America/New_York').supportsCountry('US')).to.be.true;
1518
expect(Timezone.from('America/New_York').supportsCountry('AD')).to.be.false;
@@ -21,12 +24,13 @@ describe('Timezone', () => {
2124

2225
it('should properly guess matching timezones', () => {
2326
expect(Timezone.guess(true, 'US', 'US/Eastern')).to.equal('America/New_York');
24-
expect(Timezone.guess(true, 'CA', 'US/Eastern')).to.equal('America/Montreal');
27+
expect(Timezone.guess(true, 'CA', 'US/Eastern')).to.equal('America/Toronto');
2528
expect(Timezone.guess(true, 'LY', 'Africa/Cairo')).to.equal('Africa/Tripoli');
2629
Timezone.guess(true);
2730
});
2831

2932
it('should properly perform Timezone utility functions', () => {
33+
expect(Timezone.version).to.equal(version);
3034
expect(Timezone.from('America/Fooberg').population).to.equal(250);
3135
expect(Timezone.getPopulation('America/Fooberg')).to.equal(250);
3236
expect(Timezone.from('America/Fooberg').supportsCountry('xx')).to.be.true;

src/timezone.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ export class Timezone {
193193
private static countriesForZone: Record<string, Set<string>> = {};
194194
private static zonesForCountry: Record<string, Set<string>> = {};
195195
private static populationForZone: Record<string, number> = {};
196+
private static _version: string = 'unspecified';
197+
198+
static get version(): string { return this._version; }
196199

197200
static OS_ZONE = new Timezone({ zoneName: 'OS', currentUtcOffset: osProbableStdOffset, usesDst: osUsesDst,
198201
dstOffset: osDstOffset, transitions: osTransitions });
@@ -208,7 +211,7 @@ export class Timezone {
208211

209212
private static offsetsAndZones: OffsetsAndZones[];
210213
private static regionAndSubzones: RegionAndSubzones[];
211-
private static zoneLookup: { [id: string]: Timezone } = {};
214+
private static zoneLookup: Record<string, Timezone> = {};
212215

213216
private readonly _zoneName: string;
214217
private readonly _utcOffset: number;
@@ -229,6 +232,11 @@ export class Timezone {
229232
static defineTimezones(encodedTimezones: Record<string, string>): boolean {
230233
const changed = !isEqual(this.encodedTimezones, encodedTimezones);
231234

235+
if (encodedTimezones.version)
236+
this._version = encodedTimezones.version;
237+
else
238+
this._version = 'unspecified';
239+
232240
this.encodedTimezones = Object.assign({}, encodedTimezones ?? {});
233241
this.extractZoneInfo();
234242

@@ -797,6 +805,9 @@ export class Timezone {
797805
last(insertTransitions).transitionTime >= transitions[1].transitionTime)
798806
insertTransitions.splice(insertTransitions.length - 1, 1);
799807

808+
if (insertTransitions[0].transitionTime === transitions[0].transitionTime)
809+
insertTransitions.splice(0, 1);
810+
800811
transitions.splice(1, 0, ...insertTransitions);
801812
}
802813
}

0 commit comments

Comments
 (0)