Comparing version
110
index.js
var bindings = require('./time'); | ||
var MONTHS_PER_YEAR = 12; | ||
var HOURS_PER_DAY = 24; | ||
var DAYS_PER_MONTH = 31; | ||
var SECONDS_PER_MINUTE = 60; | ||
var MINUTES_PER_HOUR = 60; | ||
var DAYS_PER_YEAR = 365; | ||
var MILLIS_PER_SECOND = 1000; | ||
var MILLIS_PER_MINUTE = MILLIS_PER_SECOND * SECONDS_PER_MINUTE; | ||
var MILLIS_PER_HOUR = MILLIS_PER_MINUTE * MINUTES_PER_HOUR; | ||
var MILLIS_PER_DAY = MILLIS_PER_HOUR * HOURS_PER_DAY; | ||
var MILLIS_PER_MONTH = MILLIS_PER_DAY * DAYS_PER_MONTH; | ||
var MILLIS_PER_YEAR = MILLIS_PER_DAY * DAYS_PER_YEAR; | ||
var DAYS_OF_WEEK = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; | ||
var MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December']; | ||
exports.DAYS_OF_WEEK = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; | ||
exports.MONTHS = ["January","February","March","April","May","June","July","August","September","October","November","December"] | ||
exports.time = bindings.time; | ||
@@ -88,5 +100,91 @@ exports.localtime = bindings.localtime; | ||
} | ||
// Sets the day of the month (from 1-31) in the current timezone | ||
this.setDate = function setDate(v) { | ||
var diff = v - this.getDate(); | ||
return this.setTime(this.getTime() + (diff * MILLIS_PER_DAY)); | ||
} | ||
// Sets the year (four digits) in the current timezone | ||
this.setFullYear = function setFullYear(v) { | ||
var diff = v - this.getFullYear(); | ||
return this.setTime(this.getTime() + (diff * MILLIS_PER_YEAR)); | ||
} | ||
// Sets the hour (from 0-23) in the current timezone | ||
this.setHours = function setHours(v) { | ||
var diff = v - this.getHours(); | ||
return this.setTime(this.getTime() + (diff * MILLIS_PER_HOUR)); | ||
} | ||
// Sets the milliseconds (from 0-999) in the current timezone | ||
this.setMilliseconds = function setMilliseconds(v) { | ||
var diff = v - this.getMilliseconds(); | ||
return this.setTime(this.getTime() + diff); | ||
} | ||
// Set the minutes (from 0-59) in the current timezone | ||
this.setMinutes = function setMinutes(v) { | ||
var diff = v - this.getMinutes(); | ||
return this.setTime(this.getTime() + (diff * MILLIS_PER_MINUTE)); | ||
} | ||
// Sets the month (from 0-11) in the current timezone | ||
this.setMonth = function setMonth(v) { | ||
var diff = v - this.getMonth(); | ||
return this.setTime(this.getTime() + (diff * MILLIS_PER_MONTH)); | ||
} | ||
// Sets the seconds (from 0-59) in the current timezone | ||
this.setSeconds = function setSeconds(v) { | ||
var diff = v - this.getSeconds(); | ||
return this.setTime(this.getTime() + (diff * MILLIS_PER_SECOND)); | ||
} | ||
// Sets a date and time by adding or subtracting a specified number of | ||
// milliseconds to/from midnight January 1, 1970. | ||
this.setTime = function setTime(v) { | ||
var rtn = Date.prototype.setTime.call(this, v); | ||
// Since this function changes the internal UTC epoch date value, we need to | ||
// re-setup these timezone translation functions to reflect the new value | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Sets the day of the month, according to universal time (from 1-31) | ||
this.setUTCDate = function setUTCDate(v) { | ||
var rtn = Date.prototype.setUTCDate.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Sets the year, according to universal time (four digits) | ||
this.setUTCFullYear = function setUTCFullYear(v) { | ||
var rtn = Date.prototype.setUTCFullYear.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Sets the hour, according to universal time (from 0-23) | ||
this.setUTCHours = function setUTCHours(v) { | ||
var rtn = Date.prototype.setUTCHours.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Sets the milliseconds, according to universal time (from 0-999) | ||
this.setUTCMilliseconds = function setUTCMillseconds(v) { | ||
var rtn = Date.prototype.setUTCMilliseconds.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Set the minutes, according to universal time (from 0-59) | ||
this.setUTCMinutes = function setUTCMinutes(v) { | ||
var rtn = Date.prototype.setUTCMinutes.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Sets the month, according to universal time (from 0-11) | ||
this.setUTCMonth = function setUTCMonth(v) { | ||
var rtn = Date.prototype.setUTCMonth.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
// Set the seconds, according to universal time (from 0-59) | ||
this.setUTCSeconds = function setUTCSeconds(v) { | ||
var rtn = Date.prototype.setUTCSeconds.call(this, v); | ||
reset.call(this); | ||
return rtn; | ||
} | ||
this.toDateString = function toDateString() { | ||
return exports.DAYS_OF_WEEK[this.getDay()].substring(0, 3) + ' ' + exports.MONTHS[this.getMonth()].substring(0, 3) + ' ' + pad(this.getDate(), 2) + ' ' + this.getFullYear(); | ||
return DAYS_OF_WEEK[this.getDay()].substring(0, 3) + ' ' + MONTHS[this.getMonth()].substring(0, 3) + ' ' + pad(this.getDate(), 2) + ' ' + this.getFullYear(); | ||
} | ||
@@ -105,3 +203,3 @@ | ||
this.toLocaleDateString = function toLocaleDateString() { | ||
return exports.DAYS_OF_WEEK[this.getDay()] + ', ' + exports.MONTHS[this.getMonth()] + ' ' + pad(this.getDate(), 2) + ', ' + this.getFullYear(); | ||
return DAYS_OF_WEEK[this.getDay()] + ', ' + MONTHS[this.getMonth()] + ' ' + pad(this.getDate(), 2) + ', ' + this.getFullYear(); | ||
} | ||
@@ -114,2 +212,6 @@ | ||
this.toLocaleString = this.toString; | ||
function reset() { | ||
this.setTimezone(this.getTimezone()); | ||
} | ||
} | ||
@@ -116,0 +218,0 @@ Date.prototype.setTimezone = Date.prototype.setTimeZone = setTimezone; |
{ | ||
"name": "time", | ||
"description": "\"time.h\" bindings for NodeJS", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"repository": { | ||
@@ -21,3 +21,5 @@ "type": "git", | ||
"" | ||
] | ||
], | ||
"dependencies": {}, | ||
"devDependencies": {} | ||
} |
@@ -7,4 +7,4 @@ node-time | ||
This module offers simple bindings for the C [time.h][] APIs. | ||
It also offers an extended regular `Date` object with `getTimeZone()` | ||
and `setTimeZone()` functions, which aren't normally part of JavaScript. | ||
It also offers an extended regular `Date` object with `getTimezone()` | ||
and `setTimezone()` functions, which aren't normally part of JavaScript. | ||
@@ -20,7 +20,7 @@ | ||
now.setTimeZone("America/Los_Angeles"); | ||
now.setTimezone("America/Los_Angeles"); | ||
// `.getDate()`, `.getDay()`, `.getHours()`, etc. | ||
// will return values according to UTC-8 | ||
now.setTimeZone("America/New_York"); | ||
now.setTimezone("America/New_York"); | ||
// `.getDate()`, `.getDay()`, `.getHours()`, etc. | ||
@@ -42,3 +42,3 @@ // will return values according to UTC-5 | ||
#### date.setTimeZone(timezone) -> Undefined | ||
#### date.setTimezone(timezone) -> Undefined | ||
@@ -49,10 +49,10 @@ Sets the timezone for the `Date` instance. Calls to `getHours()`, `getDays()`, | ||
date.setTimeZone("America/Argentina/San_Juan"); | ||
date.setTimezone("America/Argentina/San_Juan"); | ||
#### date.getTimeZone() -> String | ||
#### date.getTimezone() -> String | ||
Returns a String containing the currently configured timezone for the date instance. | ||
date.getTimeZone(); | ||
date.getTimezone(); | ||
// "America/Argentina/San_Juan" | ||
@@ -59,0 +59,0 @@ |
19
test.js
var assert = require('assert'); | ||
// The 'index.js' file extends "Date.prototype" | ||
require('./'); | ||
var time = require('./'); | ||
var d = new time.Date(Date.UTC(2011, 0, 1)); | ||
var d = new Date(Date.UTC(2011, 0, 1)); | ||
assert.equal(d.getUTCFullYear(), 2011); | ||
@@ -15,3 +13,3 @@ assert.equal(d.getUTCMonth(), 0); | ||
d.setTimeZone("UTC"); | ||
d.setTimezone("UTC"); | ||
console.log(d+''); | ||
@@ -30,3 +28,3 @@ console.log("UTC Hours: " + d.getHours()); | ||
d.setTimeZone("America/Los_Angeles"); | ||
d.setTimezone("America/Los_Angeles"); | ||
console.log(d+''); | ||
@@ -38,3 +36,3 @@ console.log("LA Hours: " + d.getHours()); | ||
d.setTimeZone("America/New_York"); | ||
d.setTimezone("America/New_York"); | ||
console.log(d+''); | ||
@@ -45,5 +43,10 @@ console.log("NY Hours: " + d.getHours()); | ||
d.setTimeZone("US/Arizona"); | ||
d.setTimezone("US/Arizona"); | ||
console.log(d+''); | ||
console.log("AZ Hours: " + d.getHours()); | ||
assert.equal(d.getHours(), 17); | ||
assert.equal(d.getUTCHours(), 0); | ||
d.setUTCHours(23); | ||
assert.equal(d.getHours(), 16); | ||
d.setHours(15); | ||
assert.equal(d.getHours(), 15); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19543
32.25%9
12.5%261
67.31%