Rails TimeZone Converter

1.2.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Demonstrates converting time zone names between Rails ActiveSupport and IANA formats, including handling unmapped inputs which return `undefined`.

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)

view raw JSON →