test for strftime

This commit is contained in:
harttle
2016-10-24 00:23:50 +08:00
parent 365a047736
commit fbcd244946
3 changed files with 15 additions and 160 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
const strftime = require('./src/strftime.js');
const strftime = require('./src/util/strftime.js');
const _ = require('./src/util/underscore.js');
var escapeMap = {
-159
View File
@@ -88,18 +88,6 @@ var _date = {
}
};
var _obj = {
values_of: function(obj) {
var values = [];
for(var k in obj){
if(obj.hasOwnProperty(k)){
values.push(obj[k]);
}
}
return values;
}
};
var _number = {
pad: function(value, size, ch) {
if (!ch) ch = '0';
@@ -217,153 +205,6 @@ var format_codes = {
format_codes.h = format_codes.b;
format_codes.N = format_codes.L;
// * r stands for regex, p stands for parser
// * all parseInt calls have to have the base supplied as the second
// parameter, otherwise they will default to octal when parsing numbers
// with leading zeros. This is most evident when parsing a date with 08 as
// the minutes / year as 08 is an invalid octal number, and so returns 0
var parse_codes = {
a: {
r: "(?:" + dayNamesShort.join("|") + ")"
},
A: {
r: "(?:" + dayNames.join("|") + ")"
},
b: {
r: "(" + monthNamesShort.join("|") + ")",
p: function(data) {
this.month = $.inArray(data, monthNamesShort);
}
},
B: {
r: "(" + monthNames.join("|") + ")",
p: function(data) {
this.month = $.inArray(data, monthNames);
}
},
C: {
r: "(\\d{1,2})",
p: function(d) {
this.century = parseInt(d, 10);
}
},
d: {
r: "(\\d{1,2})",
p: function(d) {
this.day = parseInt(d, 10);
}
},
H: {
r: "(\\d{1,2})",
p: function(d) {
this.hour = parseInt(d, 10);
}
},
// This gives only the day. Parsing of the month happens at the end because
// we also need the year
j: {
r: "(\\d{1,3})",
p: function(d) {
this.day = parseInt(d, 10);
}
},
L: {
r: "(\\d{3})",
p: function(d) {
this.milliseconds = parseInt(d, 10);
}
},
m: {
r: "(\\d{1,2})",
p: function(d) {
this.month = parseInt(d, 10) - 1;
}
},
M: {
r: "(\\d{2})",
p: function(d) {
this.minute = parseInt(d, 10);
}
},
p: {
r: "(AM|PM)",
p: function(d) {
if (d == 'AM') {
if (this.hour == 12) {
this.hour = 0;
}
} else {
if (this.hour < 12) {
this.hour += 12;
}
}
}
},
P: {
r: "(am|pm)",
p: function(d) {
if (d == 'am') {
if (this.hour == 12) {
this.hour = 0;
}
} else {
if (this.hour < 12) {
this.hour += 12;
}
}
}
},
q: {
r: "(?:" + _obj.values_of(suffixes).join('|') + ")"
},
S: {
r: "(\\d{2})",
p: function(d) {
this.second = parseInt(d, 10);
}
},
y: {
r: "(\\d{1,2})",
p: function(d) {
this.year = parseInt(d, 10);
}
},
Y: {
r: "(\\d{4})",
p: function(d) {
this.century = Math.floor(parseInt(d, 10) / 100);
this.year = parseInt(d, 10) % 100;
}
},
z: { // "Z", "+05:00", "+0500" all acceptable.
r: "(Z|[+-]\\d{2}:?\\d{2})",
p: function(d) {
// UTC, no offset.
if (d == "Z") {
this.zone = 0;
return;
}
var seconds = parseInt(d[0] + d[1] + d[2], 10) * 3600; // e.g., "+05" or "-08"
if (d[3] == ":") {
// "+HH:MM" is preferred iso8601 format
seconds += parseInt(d[4] + d[5], 10) * 60;
} else {
// "+HHMM" is frequently used, though.
seconds += parseInt(d[3] + d[4], 10) * 60;
}
this.zone = seconds;
}
}
};
parse_codes.e = parse_codes.d;
parse_codes.h = parse_codes.b;
parse_codes.I = parse_codes.H;
parse_codes.k = parse_codes.H;
parse_codes.l = parse_codes.H;
var strftime = function(d, format) {
// I used to use string split with a regex and a capturing block here,
// which I thought was really clever, but apparently this exact feature is
+14
View File
@@ -0,0 +1,14 @@
const chai = require("chai");
const expect = chai.expect;
var t = require('../../src/util/strftime.js');
describe('util/strftime', function() {
var now = new Date('2016-10-23T16:15:23');
it('should format week by %U', function() {
expect(t(now, '%U')).to.equal('43');
});
it('should format GMT string', function() {
expect(t(now, '%Y-%m-%dT%H:%M:%S')).to.equal('2016-10-24T00:15:23');
});
});