group timestamps into "buckets" by applying a granularity to a discrete value
Group timestamps into "buckets" by applying a granularity to a discrete value
A timebucket is a type of time domain which combines a size identifier (granularity) with a discrete value, and can be stored as a compact string.
Timebuckets are useful for grouping timestamps together using a particular granularity (such as groups of 30 minutes), as an aid for graphing or map/reduce-ing time-series data.
A timebucket consists of:
- a size spec (string) which consists of:
- (optional) an unsigned multiplier (default:
- a granularity identifier, i.e.
- a signed value, i.e. number of seconds, after or before epoch year (1970)
8h-491- time (within 8 hours) of first moonwalk (
y43- the year 2013
timebucket module exports a factory function. It supports the
following argument combinations:
timebucket()- build timebucket with millisecond granularity applied to current date
timebucket(size)- build from specific size (string identifier, see list below) applied to current date
timebucket(milliseconds)- build from milliseconds since 1970
timebucket(size, value)- build from specific size and value
timebucket(date)- build from date object with millisecond granularity
timebucket(str)- build from string representation
To resize a bucket, call
console.logtimebucket'y'resize'30m' + '';// '30m753360'
toString()- convert to string representation
toJSON()- same as
toMilliseconds()- convert to millisecond UNIX time offset
toDate()- convert to Date object
var timebucket = require'timebucket';// defaults to current milliseconds after unix epochconsole.logtimebucket + '';// 'ms1369656669680'// default size (milliseconds) and valueconsole.logtimebucket1369601120380 + '';// 'ms1369601120380'// specific size (seconds) and valueconsole.logtimebucket's' 1369601125 + '';// 's1369601125'// resize to yearconsole.logtimebucketresize'y' + '';// 'y43'// use current time, specific sizeconsole.logtimebucket'30m' + '';// '30m760920'// create timebucket from current seconds after unix epoch, and add 5console.logtimebucket's'add5 + '';// 's1369656674'// from date objectconsole.logtimebucket + '';// 'ms1369656669686'// from string representationconsole.logtimebucket'y43' + '';// 'y43'// access granularity and value as propertiesvar t = timebucket;console.logtsizevalue tsizegranularity tvalue;// 1 'ms' 1369656669686// resizeconsole.logtimebucket'y'resize'30m' + '';// '30m753360'// from current microseconds (smallest supported bucket size)console.logtimebucket'µs' + '';// 'µs1369659466378060'// resize from microsecondsconsole.logtimebucket'µs1369657390541000'resize'y' + '';// 'y43'
Timebuckets also have a 64-bit integer representation which contains granularity metadata, while maintaining sortability with buckets of the same granularity.
var timebucket = require'timebucket';var num = timebucket'8h-491'toNumber;// toNumber() returns a 64-bit integer representation as a Numberconsole.lognum;// -49133var bucket = timebucketfromNumbernum;// fromNumber() takes the result of toNumber(), and returns a timebucket instance:console.logbuckettoString;// 8h-491
Developed by Terra Eclipse
Terra Eclipse, Inc. is a nationally recognized political technology and strategy firm located in Aptos, CA and Washington, D.C.
- Copyright (C) 2012 Carlos Rodriguez (http://s8f.org/)
- Copyright (C) 2012 Terra Eclipse, Inc. (http://www.terraeclipse.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.