Permalink
Browse files

fix: escapes date as iso string in utc to work with all versions of cql

  • Loading branch information...
1 parent ea4d283 commit 9e7f835e21fd542187178f9bf3250c6b7c6157f7 @tim-dev tim-dev committed Nov 15, 2013
Showing with 13 additions and 7 deletions.
  1. +13 −7 lib/connection.js
View
@@ -16,6 +16,10 @@ var util = require('util'),
* @memberOf Connection
*/
var NOOP = function(){};
+/**
+ * Adds zero left-padding to numbers, when necessary
+ */
+var zero_pad = function(n){ return n < 10 ? "0" + n : n }
/**
* Default port for cassandra
@@ -103,15 +107,17 @@ function escapeCQL(val) {
}
if(val instanceof Date){
- _val = val.getTime();
- if(isNaN(_val)){
+ if(isNaN(val.getTime())){
throw errors.create(new Error('Invalid Date'));
}
- // For cassandra < 2
- if(_val<0){
- _val = "'"+val.toISOString()+"'";
- }
- return _val;
+ // Format as an ISO date string
+ return "'" + val.getUTCFullYear()
+ + '-' + zero_pad(val.getUTCMonth() + 1)
+ + '-' + zero_pad(val.getUTCDate())
+ + ' ' + zero_pad(val.getUTCHours())
+ + ':' + zero_pad(val.getUTCMinutes())
+ + ':' + zero_pad(val.getUTCSeconds())
+ + "'"
}
if(typeof val === 'number' || typeof val === 'boolean'){

0 comments on commit 9e7f835

Please sign in to comment.