Rails TimeZone Converter
This `rails-timezone` package (version 1.2.0) provides essential utility functions for converting between the time zone names used by Rails' ActiveSupport and the universally recognized IANA Time Zone Database (also known as the Olsen database). Its primary purpose is to bridge the naming discrepancy between these two systems, ensuring compatibility for applications that interface with Rails backends. The library implements the exact mapping found in ActiveSupport's `time_zone.rb`, guaranteeing consistent conversions. While major releases are likely tied to significant changes in Rails' time zone handling, its current version offers stable functionality for both Node.js and browser environments, complete with TypeScript type definitions for improved development. Its key differentiator is its direct and accurate reflection of Rails' specific time zone naming conventions, unlike more general time zone libraries.
Common errors
-
TypeError: from is not a function
cause Attempting to call `from` directly on a CommonJS default import or incorrect ES module import syntax.fixFor ES modules, use named import: `import { from } from 'rails-timezone';`. For CommonJS, use destructuring: `const { from } = require('rails-timezone');`. -
Property 'RailsTimeZone' does not exist on type 'Window & typeof globalThis'.
cause TypeScript error when accessing `window.RailsTimeZone` in a browser environment without an explicit global type declaration.fixAdd a declaration file (`.d.ts`) to your project or augment the `Window` interface: `declare global { interface Window { RailsTimeZone: { from: (tz: string) => string | undefined; to: (tz: string) => string | undefined; }; } }`. -
Cannot find module 'rails-timezone'
cause The package is not installed in your project's `node_modules` or the import/require path is incorrect.fixRun `npm install rails-timezone` or `yarn add rails-timezone`. Verify that the import path `'rails-timezone'` is correct.
Warnings
- breaking The library's internal time zone mapping is a static snapshot derived from a specific version of ActiveSupport's `time_zone.rb`. If Rails updates its time zone names or mappings in a future version, this library might return outdated or incorrect conversions. Users relying on precise synchronization with the latest Rails definitions should monitor for `rails-timezone` updates.
- gotcha Both `from` and `to` functions return `undefined` when an input time zone name (either ActiveSupport or IANA) is not found in the internal mapping. This is by design and not an error, but it requires explicit handling to prevent downstream issues.
- gotcha When used directly in a browser environment via a `<script>` tag, the library exposes its functions on the global `window.RailsTimeZone` object. This could lead to naming collisions if another script defines a global object with the same name.
Install
-
npm install rails-timezone -
yarn add rails-timezone -
pnpm add rails-timezone
Imports
- from
import RailsTimeZone from 'rails-timezone'; RailsTimeZone.from(...)
import { from } from 'rails-timezone'; - to
const RailsTimeZone = require('rails-timezone'); RailsTimeZone.to(...)import { to } from 'rails-timezone'; - RailsTimeZone
import { RailsTimeZone } from 'rails-timezone';window.RailsTimeZone.from('Eastern Time (US & Canada)'); - CommonJS require
const { from, to } = require('rails-timezone');
Quickstart
import { from, to } from 'rails-timezone';
// Example 1: Convert from Rails ActiveSupport name to IANA
const railsTimeZoneName = 'Eastern Time (US & Canada)';
const ianaTimeZone = from(railsTimeZoneName);
console.log(`Rails TimeZone "${railsTimeZoneName}" converts to IANA: "${ianaTimeZone}"`);
// Expected output (America/New_York)
// Example 2: Convert from IANA name to Rails ActiveSupport
const ianaTimeZoneName = 'Europe/Berlin';
const railsTimeZone = to(ianaTimeZoneName);
console.log(`IANA TimeZone "${ianaTimeZoneName}" converts to Rails: "${railsTimeZone}"`);
// Expected output (Berlin)
// Example 3: Handling unknown time zones gracefully (returns undefined)
const unknownRails = 'Galactic Time';
const unknownIana = from(unknownRails);
console.log(`Unknown Rails TimeZone "${unknownRails}" converts to IANA: "${unknownIana}"`);
// Expected output (undefined)
const unknownIanaName = 'Mars/Equatorial';
const unknownRailsName = to(unknownIanaName);
console.log(`Unknown IANA TimeZone "${unknownIanaName}" converts to Rails: "${unknownRailsName}"`);
// Expected output (undefined)