mirror of
https://github.com/samsonjs/batteries.git
synced 2026-04-27 15:07:42 +00:00
use strftime module instead of duplicating the code
This commit is contained in:
parent
8e2ca7fec3
commit
f956cefaab
2 changed files with 9 additions and 83 deletions
91
lib/date.js
91
lib/date.js
|
|
@ -1,92 +1,17 @@
|
||||||
// batteries
|
// batteries
|
||||||
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
// Copyright 2010 - 2011 Sami Samhuri <sami@samhuri.net>
|
||||||
|
|
||||||
var DateExt = { format: format };
|
var strftime = require('strftime').strftime
|
||||||
|
, batteries = require('./')
|
||||||
|
, DateExt = { format: format }
|
||||||
|
;
|
||||||
|
|
||||||
exports.extendNative = function() {
|
exports.extendNative = function() {
|
||||||
require('./ext').extend(Date, DateExt);
|
batteries.object.extendPrototype(Date, DateExt);
|
||||||
};
|
};
|
||||||
|
|
||||||
require('./object').extend(exports, DateExt);
|
batteries.object.extend(exports, DateExt);
|
||||||
|
|
||||||
function words(s) { return (s || '').split(' '); }
|
function format(d, fmt, locale) {
|
||||||
|
return strftime.call(null, fmt, d, locale);
|
||||||
var Weekdays = words('Sunday Monday Tuesday Wednesday Thursday Friday Saturday');
|
|
||||||
var WeekdaysShort = words('Sun Mon Tue Wed Thu Fri Sat');
|
|
||||||
var Months = words('January February March April May June July August September October November December');
|
|
||||||
var MonthsShort = words('Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec');
|
|
||||||
|
|
||||||
function format(d, fmt) {
|
|
||||||
return fmt.replace(/%(.)/, function(_, c) {
|
|
||||||
switch (c) {
|
|
||||||
case 'A': return Weekdays[d.getDay()];
|
|
||||||
case 'a': return WeekdaysShort[d.getDay()];
|
|
||||||
case 'B': return Months[d.getMonth()];
|
|
||||||
case 'b': // fall through
|
|
||||||
case 'h': return MonthsShort[d.getMonth()];
|
|
||||||
case 'D': return format(d, '%m/%d/%y');
|
|
||||||
case 'd': return pad(d.getDate());
|
|
||||||
case 'e': return d.getDate();
|
|
||||||
case 'F': return format(d, '%Y-%m-%d');
|
|
||||||
case 'H': return pad(d.getHours());
|
|
||||||
case 'I':
|
|
||||||
var hour = d.getHours();
|
|
||||||
if (hour == 0) hour = 12;
|
|
||||||
else if (hour > 12) hour -= 12;
|
|
||||||
return pad(hour);
|
|
||||||
case 'k': return pad(d.getHours(), ' ');
|
|
||||||
case 'l':
|
|
||||||
var hour = d.getHours();
|
|
||||||
if (hour == 0) hour = 12;
|
|
||||||
else if (hour > 12) hour -= 12;
|
|
||||||
return pad(hour, ' ');
|
|
||||||
case 'M': return pad(d.getMinutes());
|
|
||||||
case 'm': return pad(d.getMonth() + 1);
|
|
||||||
case 'n': return '\n';
|
|
||||||
case 'p': return d.getHours() < 12 ? 'AM' : 'PM';
|
|
||||||
case 'R': return format(d, '%H:%M');
|
|
||||||
case 'r': return format(d, '%I:%M:%S %p');
|
|
||||||
case 'S': return pad(d.getSeconds());
|
|
||||||
case 's': return d.getTime();
|
|
||||||
case 'T': return format(d, '%H:%M:%S');
|
|
||||||
case 't': return '\t';
|
|
||||||
case 'u':
|
|
||||||
var day = d.getDay();
|
|
||||||
return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week
|
|
||||||
case 'v': return format(d, '%e-%b-%Y');
|
|
||||||
case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week
|
|
||||||
case 'Y': return d.getFullYear();
|
|
||||||
case 'y':
|
|
||||||
var year = d.getYear();
|
|
||||||
return year < 100 ? year : year - 100;
|
|
||||||
case 'Z':
|
|
||||||
var tz = d.toString().match(/\((\w+)\)/);
|
|
||||||
return tz && tz[1] || '';
|
|
||||||
case 'z':
|
|
||||||
var off = d.getTimezoneOffset();
|
|
||||||
return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60);
|
|
||||||
default: return c;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function pad(n, padding) {
|
|
||||||
padding = padding || '0';
|
|
||||||
return n < 10 ? (padding + n) : n;
|
|
||||||
}
|
|
||||||
|
|
||||||
function month(d) {
|
|
||||||
return Months(d.getMonth());
|
|
||||||
}
|
|
||||||
|
|
||||||
function shortMonth(d) {
|
|
||||||
return MonthsShort(d.getMonth());
|
|
||||||
}
|
|
||||||
|
|
||||||
function weekday(d) {
|
|
||||||
return Weekdays(d.getDay());
|
|
||||||
}
|
|
||||||
|
|
||||||
function shortWeekday(d) {
|
|
||||||
return WeekdaysShort(d.getDay());
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
, "licenses": [
|
, "licenses": [
|
||||||
{
|
{
|
||||||
"type": "MIT",
|
"type": "MIT",
|
||||||
|
, "dependencies" : { "strftime" : "0.4.x" }
|
||||||
"url": "https://github.com/samsonjs/batteries/raw/master/LICENSE"
|
"url": "https://github.com/samsonjs/batteries/raw/master/LICENSE"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue