-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/** | ||
* Checks if a given time zone is valid. | ||
* | ||
* This function verifies whether a given time zone identifier is valid by | ||
* attempting to create a DateTimeFormat object with the provided time zone. | ||
* | ||
* @param {string} tz - The time zone identifier to be validated. | ||
* @returns {boolean} - Returns true if the time zone is valid, otherwise false. | ||
* @throws {Error} - Throws an error if the environment does not support time zones. | ||
* | ||
* @example | ||
* isValidTimeZone('America/New_York') // true | ||
* isValidTimeZone('Invalid/Timezone') // false | ||
*/ | ||
export function isValidTimezone (tz) { | ||
if (!Intl || !Intl.DateTimeFormat().resolvedOptions().timeZone) { | ||
throw new Error('Time zones are not available in this environment') | ||
} | ||
|
||
try { | ||
Intl.DateTimeFormat(undefined, {timeZone: tz}) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { expect } from 'chai' | ||
import { isValidTimezone } from '../index.js' | ||
|
||
describe('isValidTimezone', () => { | ||
it('should return true for a valid time zone', () => { | ||
expect(isValidTimezone('America/New_York')).to.be.true | ||
}) | ||
|
||
it('should return false for an invalid time zone', () => { | ||
expect(isValidTimezone('Invalid/Timezone')).to.be.false | ||
}) | ||
|
||
it('should return false for a valid GMT offset', () => { | ||
expect(isValidTimezone('GMT+01:00')).to.be.false | ||
}) | ||
|
||
it('should throw an error if time zones are not available in the environment', () => { | ||
const originalIntl = global.Intl | ||
|
||
// Mock the absence of Intl | ||
global.Intl = undefined | ||
|
||
try { | ||
expect(() => isValidTimezone('America/New_York')).to.throw('Time zones are not available in this environment') | ||
} finally { | ||
// Restore the original Intl object | ||
global.Intl = originalIntl | ||
} | ||
}) | ||
}) |