Format date fields in CSV output

This commit is contained in:
Sandro Santilli 2012-12-07 14:27:02 +01:00
parent a63cb20be0
commit 2e85588d99
4 changed files with 23 additions and 4 deletions

View File

@ -28,6 +28,7 @@ var express = require('express')
, crypto = require('crypto')
, fs = require('fs')
, zlib = require('zlib')
, strftime = require('strftime')
, util = require('util')
, spawn = require('child_process').spawn
, Meta = require(global.settings.app_root + '/app/models/metadata')
@ -425,7 +426,21 @@ function toCSV(data, res, callback){
// stream the csv out over http
csv()
.from(data.rows)
.toStream(res, {end: true, columns: columns, header: true});
.toStream(res, {end: true, columns: columns, header: true})
.transform( function(data) {
for (var i in data) {
// convert dates to string
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/77
// TODO: take a format string as a parameter
if ( data[i] instanceof Date ) {
// ISO time
data[i] = strftime('%F %H:%M:%S', data[i]);
}
}
return data;
})
;
return true;
} catch (err) {
callback(err,null);

5
npm-shrinkwrap.json generated
View File

@ -1,6 +1,6 @@
{
"name": "cartodb_api",
"version": "1.3.0",
"version": "1.3.3",
"dependencies": {
"cluster2": {
"version": "0.3.5-cdb02",
@ -215,6 +215,9 @@
"node-uuid": {
"version": "1.3.3"
},
"strftime": {
"version": "0.4.7"
},
"csv": {
"version": "0.0.13"
},

View File

@ -20,6 +20,7 @@
"step": "0.0.x",
"oauth-client": "0.2.0",
"node-uuid":"1.3.3",
"strftime":"~0.4.7",
"csv":"0.0.13"
},
"devDependencies": {

View File

@ -730,8 +730,8 @@ test('CSV format', function(done){
var row0 = rows[0].split(',');
var row1 = rows[1].split(',');
assert.equal(row0[0], 'updated_at');
assert.equal(row1[0], '2011-09-21 14:02:21.358706');
assert.equal(row0[1], 'created_at');
assert.equal(row1[1], '2011-09-21 14:02:21');
done();
});