npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tztimejs

v0.0.1

Published

Time-zone-aware Date replacement for JavaScript

Downloads

5

Readme

TzTime.JS

Time-zone-aware drop-in Date object replacement for JavaScript.

This libary is still in eary phases of development, and most things are not yet implemented. Please be patient.

Why another freaking date/time library?

The JavaScript's native Date constructor is just that horrible. There are as many solutions to this horror as there are developers, it seems, but there is always room for another, too.

TzTime does things a wee bit differently by providing a completely new constructor that fixes many of the Date constructor's quirks. It also provides full support for time zones. Finally, it retains close to 100% API compatibility, making it a drop-in replacement for many use cases.

Installation

TzTime uses an UMD wrapper so it's installable on NodeJS and usable in browsers as either AMD module or with vanilla <script> tag.

NodeJS

Install with NPM:

npm install tztime

Browser (AMD)

Simplly require it as usual:

var TzTime = require('tztime');

Browser (non-AMD)

Include a script tag:

<script src="js/tztime.js"></script>
<script>
  var t = new TzTime();
</script>

volo projects

volo users can install it by using the github repository:

volo add foxbunny/tztime

TzTime features

Here is an incomplete list of key features:

Better constructor

The TzTime constructor doesn't require the new keyword.

var v = TzTime(2013, 8, 1);

It will also take an instance and return a clone.

var v1 = TzTime(v);
v1.getTime() === v.getTime();  // true
v1 === v; // false

It can also take a Date instance and return a TzTime object.

var d = Date(2013, 8, 1);
var v2 = TzTime(d);

When using the longest form, it accepts time zone offset as well.

var v3 = TzTime(2013, 8, 1, 12, 45, 0, 0, -120);

Crated instances are instances of both TzTime, but not Date.

v3 instanceof TzTime; // true
v3 instanceof Date; // false

Code that works with Date instances should work with with TzTime instances, though (sans the time zone support) and a few exotic cases such as coertion into string.

Accessor attributes

You no longer have to use getters and setters to get the values (if you don't like them).

var v = TzTime(2013, 8, 1, 12, 45, 9, 123, -120);

v.year;         // 2013
v.month;        // 8
v.date;         // 1
v.hours;        // 12
v.minutes;      // 45
v.seconds;      // 9
v.milliseconds; // 123
v.timezone;     // -120
// (and more on their way)

The attributes can be assigned as you would expect:

v.year = 2014;
v.minutes = 20;

You can also use the -= and += operators (no more .addDays() and similar).

v.date += 20;

They behave just like the getter-setter combo to shift the date/time by desired number.

v.hours += 24 * 365; // shifts the date by 1 year

Compare that to:

v.setHours(v.getHours() + 24 * 365);

The setter-getter combo still works if you prefer that.

Time zone support

Yes, for real. The time zone support is finally available in JavaScript, and it's simpler than you'd think. (Yeah, it's not the first time ever that we have time zones in JavaScript, but TzTime offers a more Date-like interface for it.)

The getTimezoneOffset() now has a setTimezoneOffset() counterpart that works as expected. There is also a timezone attribute that you can use.

var v = TzTime(2013, 8, 1, 12, 45, 9, 123, -120);

v.getUTCHours() == 14; // true

v.timezone = 120;
v.hours == 12; // Hours remain constant, we have just changed the time zone

v.getUTCHours() == 10; // true

Time zone support is completely invisible if you don't use it. The constructor will always create new instances in local time zone of the platform unless the time zone is passed to it, and the instances will remain in local time zone of the platform as long as you don't modify the timezone attribute, or use the time zone offset setter. Note, however, that once you modify the time zone of the instance, the code that expects the date and time to be in platform time zone will no longer function as expected. However, the time zone offset getter will still work correctly.

Strftime formatting

TzTime offers reliable strftime formatting using the non-native toFormat() method.

var v = TzTime(2013, 8, 1, 12, 45, 9, 123, -120);
v.toFormat('At %i:%M %p on %B %D');
// 'At 12:45 p.m. on September 1'

Strptime parsing

The native Date.parse method has been replaced by strptime-powered TzTime.parse:

var v = TzTime.parse('2013-09-19 4:30 p.m.', '%Y-%m-%d %i:%M %p');

Design of TzTime and performance

Unfortunately, to get all the goodies to the table, a few design decisions have been made that impact the performance.

One of the decisions was to overload the local time getters (#getHours() et al) to compensate for time zone difference, and leave the UTC getters alone. This was done to keep TzTime object 100% compatible with the Date object (e.g., time zone change does not shift the local time of the instance). To achieve this, the getters all create a clone of the object using Date constructor, and shift the time zone of the clone before delegating to clone's getters. Needless to say, this makes the TzTime getters extremely slow.

Another decision was to make the TzTime constructor instantiate a Date object internally so it can reuse as much of the Date prototype as possible, maintain the same API as the Date constructor, and still add features to it. This made the constructor code a bit more complex and therefore slower.

Here are some numbers to illustrate the performance issues that you might have. The tests were performed with a simple loop ran over Date and TzTime objects. The loop looks like this:

(function() { 
    var start = Date.now(); 
    for (var i = 1000000; i; i -= 1) { 
        /*** OPERATION ***/
    } 
    var end = Date.now(); 
    console.log(end - start); 
}());

The o object appearing in the Operation column is a Date or TzTime object. They were created with respective constructors without any arguments. O in the Operation column is a constructor.

Times are shown in milliseconds.

Here are the results in Google Chrome 29.0.1547.66:

Operation                   Date            TzTime          Delta %
------------------------------------------------------------------------
o.getHours()                172             486             283%
o.setHours(5)               498             838             168%
new O()                     308             424             138%

Here are the results in FireFox 23.0.1:

Operation                   Date            TzTime          Delta %
------------------------------------------------------------------------
o.getHours()                156             2056            1317%
o.setHours(5)               423             3287            777%
new O()                     486             1804            371%

Internet Explorer 9.0.17 (32-bit / Windows 7):

Operation                   Date            TzTime          Delta %
------------------------------------------------------------------------
o.getHours()                251             789             917%
o.setHours(5)               718             1630            369%
new O()                     431             1532            521%

As you can see, performance is quite horrible at the moment, especially in Firefox. The question now is, is the performance hit justified in face of possible benefits? For most applications, I believe it is. Especially applications that need time zone support.

The performance issues will be addressed at a later date, though.

API documentation

API documentation is available in the doc directory in Markdown format.