From f956cefaab3c749a645c4809b38bfcf8f43c7851 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sun, 5 Jun 2011 18:09:25 -0700 Subject: [PATCH] use strftime module instead of duplicating the code --- lib/date.js | 91 +++++----------------------------------------------- package.json | 1 + 2 files changed, 9 insertions(+), 83 deletions(-) diff --git a/lib/date.js b/lib/date.js index e0e4a37..e7f9b96 100644 --- a/lib/date.js +++ b/lib/date.js @@ -1,92 +1,17 @@ // batteries // Copyright 2010 - 2011 Sami Samhuri -var DateExt = { format: format }; +var strftime = require('strftime').strftime + , batteries = require('./') + , DateExt = { format: format } + ; 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(' '); } - -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()); +function format(d, fmt, locale) { + return strftime.call(null, fmt, d, locale); } diff --git a/package.json b/package.json index 0c49360..d8ece2d 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ , "licenses": [ { "type": "MIT", +, "dependencies" : { "strftime" : "0.4.x" } "url": "https://github.com/samsonjs/batteries/raw/master/LICENSE" } ]