Skip to content

Proper timezone conversions for Javascript, heavily based on pytz

Notifications You must be signed in to change notification settings

marcinkaszynski/pytz-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

A simple timezone support library for Javascript.
Based heavily on pytz.

MIT license, same as pytz.

Current status: the library should be usable -- it handles all the
important corner cases from pytz well.  There might be some breakage
elsewhere, but you won't know until you try it.

You know you want to.

------------------------------------------------------------------------------

The goal of this library is to make two things easy:

- convert a Date to a chosen timezone,
- build a Date based on local time in a chosen timezone.

The core idea is that the right way to store a timestamp is in a Date,
as UTC, always.  Timestamps in other timezones are just presentation.

Example:

<script src="{{ STATIC_URL }}js/pytz.js"></script>
<script src="{{ STATIC_URL }}js/zones/Europe/Warsaw.js"></script>
<script src="{{ STATIC_URL }}js/zones/America/New_york.js"></script>
[add time zones as needed; everything in pytz.all_timezones is supported]

<script>
    // Create a date representing the time.
    // All fields in the dictionary are required except milliseconds,
    // which default to 0.
    var localTime = {year: 2011, month: 10, date: 11,
                     hours: 22, minutes: 34, seconds: 50,
                     milliseconds: 120};
    var date = Tz.localToDate('Europe/Warsaw', localTime);

    // Note: date.getHours(), date.getDays() etc WILL BE DIFFERENT
    // from fields in localTime if your system/browser timezone is
    // different than localTime.tz.  That's because localToDate
    // creates a date that represents the requested moment in time,
    // which means different representations in different timezones.
    // Use Tz.dateToLocal to get back the date and time in your chosen
    // timezone:
    var localTime_back = Tz.dateToLocal('Europe/Warsaw', date);
    
    // Now localTime_back is localTime + one more field, 'day',
    // meaning the day of week (0=Sunday, 1=Monday, ...).

    // If you specify ambiguous times during time change (when you
    // switch off DST there is an overlap when every local time occurs
    // twice, once before the switch and once after); localToDate
    // will throw "AmbiguousTimeError" unless you add a hint which 
    // one you mean using the is_dst argument.
    var dt = Tz.dict(2004, 9, 31, 2, 0, 0, 0);

    var loc_dt1 = Tz.localToDate('Europe/Amsterdam', dt, true);
    // loc_dt1.getUTCHours() == 0

    var loc_dt2 = Tz.localToDate('Europe/Amsterdam', dt, false);
    // loc_dt2.getUTCHours() == 1
</script>

NOTES:

- pytz handles some dates before unix epoch start (1970-1-1), but for
  the time being I'm throwing those away.  Do not use this library for
  dates outside of current unix time range (1970-1-1 to 2038-01-19
  UTC).

- JS Date-related API is braindead in many ways, but by now a lot of
  people are used to its quirks so I'm keeping my API consistent with
  it.  It means, in particular, that months are in the 0..11 range
  while days of month are 1..31 (and are called 'date').

About

Proper timezone conversions for Javascript, heavily based on pytz

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published