granularity
Timebuckets for dates.
Group date/timestamps/other date objects into timebuckets that you can traverse.
Really helpful for storing and querying dates from data bases.
Usage:
var bucket = Granularity(duration, ?origin)
Creates a timebucket.
Args:
duration
- (Optional, Default=1ms). The width of each timebucket (ms). 3600000 means 1 hour. 0 (Zero) means all time passed. If duration is negative, it'll start from the previous Timebucket.origin
- (Optional, Date|timestamp|iso date, default=now). The first timebucket. Any date string thatDate
object can read is acceptable here.
Return value:
The return value is an extended Date object, which will respond to all methods that are associated with Date, along with several additional methods.
Example:
console;var next24 = ; consolevar last30Days = ; // Note the duration is negative console;var allTime = ; console;var everyTwoHourBucket = ; // Origins at today consolevar everyHourOnTheHalf = // Origins at 2014-08-07T00:30:00Z
bucket.start()
Returns the Date() of the bucket itself.
Example
consolevar last30Days = ; // Note the duration is negativeconsole // Should be 30 days ago
bucket.end()
Returns a Date() that indicates when the bucket should end. Basically it's the next bucket minus 1 ms.
Example
consolevar last30Days = ; // Note the duration is negativeconsole // Should be 30 days agoconsole // Should be now - 1 ms
bucket.skip(?interval=1) || bucket.next(?interval=1)
Skips interval
timebuckets forward, or backwards (in case interval
is negative).
Example
console;// 2014-08-20T02:00:00.000Zconsole;console; console;// 2014-08-20T04:00:00.000Zconsole;console;
bucket.previous(?interval=1)
Same as bucket.skip
, just as negative value, or in other words: bucket.skip(-1)===bucket.previous(1)
.
Example
console;// 2014-08-19T22:00:00.000Zconsole;console; console;// 2014-08-19T20:00:00.000Zconsole;console;
bucket.floor(?date=now)
Create a new Timebucket that contains date
.
date
- Whether Date object or any type of string that is readable by Date object. Default is now.
Example
console;var everyTwoHourBucket = ; // Origins at todayconsole // 2014-08-08T12:00:00.000Zconsole // 2014-08-08T12:00:00.000Zconsole // 2014-08-08T14:00:00.000Z consolevar everyHourOnTheHalf = // Origins at 2014-08-07T00:30:00Z console // 2014-08-08T12:30:00.000Zconsole // 2014-08-08T12:30:00.000Zconsole // 2014-08-08T14:30:00.000Z
bucket.ceil(?date=now)
Similar to bucket.floor
, with one difference. It creates a Timebucket after bucket.floor
.
An alternative way of achieving this, would be: bucket.floor(date).next()
Example
console; console // 2014-08-08T12:00:00.000Zconsole // 2014-08-08T14:00:00.000Z
bucket.serialize()
Serializes the timebucket into a string.
Example
console;// 7200000~0console;
bucket.unserialize(str)
Unserializes a Timebucket string, by using the origin of the current Timebucket.
Example
console;// 2014-08-19T00:00:00.000Zconsole;
Granularity.unserialize(str, ?origin=now)
Unserializes a Timebucket string, by using an origin of your choice.
origin
- (Default=now). Whether Date object or any Date string that is acceptable by Date object.
Example
console;// 2014-08-20T00:00:00.000Zconsole; console;// 1970-01-01T00:00:00.000Zconsole;
Granularity.today()
Creates a daily Timebucket, that starts from today.
Example
consolevar today = Granularity;console;
Complete Example
var Granularity = ; consolevar today = Granularity;console; console;var next24 = ;console; // Should be nowconsole; // Should be tomorrow - 1 ms consolevar last30Days = ; // Note the duration is negativeconsole // Should be 30 days agoconsole // Should be now - 1 ms console;console // Should be 60 days agoconsole // Should be 30 days ago - 1 ms console;var allTime = ;console // Should be 0 in Epoch timeconsole // Should now - 1 ms console;var everyTwoHourBucket = ; // Origins at todayconsole // 2014-08-08T12:00:00.000Zconsole // 2014-08-08T12:00:00.000Zconsole // 2014-08-08T14:00:00.000Z consolevar everyHourOnTheHalf = // Origins at 2014-08-07T00:30:00Z console // 2014-08-08T12:30:00.000Zconsole // 2014-08-08T12:30:00.000Zconsole // 2014-08-08T14:30:00.000Z console; console // 2014-08-08T12:00:00.000Zconsole // 2014-08-08T14:00:00.000Z console;// Let's recreate the every to hour bucket for a fixed dateeveryTwoHourBucket = ;// 2014-08-20T00:00:00.000Zconsole; console;// 2014-08-20T01:59:59.999Zconsole; console;// 2014-08-20T02:00:00.000Zconsole;console; console;// 2014-08-20T04:00:00.000Zconsole;console; console;// 2014-08-19T22:00:00.000Zconsole;console; console;// 2014-08-19T20:00:00.000Zconsole;console; console;// -3600console;console; console;// 7200000~0console; console;// 2014-08-20T00:00:00.000Zconsole; console;// 1970-01-01T00:00:00.000Zconsole; console;// 2014-08-19T00:00:00.000Zconsole;
install
With npm do:
npm install granularity
license
MIT