diff --git a/Readme.md b/Readme.md index d5500f1..c4604ae 100644 --- a/Readme.md +++ b/Readme.md @@ -14,7 +14,7 @@ Usage ===== var strftime = require('strftime').strftime - console.log(strftime('%D-%M-%Y %H:%M:%S')) + console.log(strftime('%d-%m-%y %H:%M:%S')) // => 11-11-2010 13:24:17 For details just see `man 3 strftime` as the format specifiers are identical. diff --git a/lib/index.js b/lib/index.js index a97a0aa..be93770 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,146 +5,77 @@ /// Copyright 2010 Sami Samhuri /// MIT License -var Weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', - 'Friday', 'Saturday']; +var strftime = (function() { + var Weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', + 'Friday', 'Saturday']; -var WeekdaysShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; - -var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', - 'August', 'September', 'October', 'November', 'December']; - -var MonthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', - 'Sep', 'Oct', 'Nov', 'Dec']; - -if (typeof exports !== 'undefined') exports.strftime = strftime - -// Most of the specifiers supported by C's strftime -function strftime(fmt, d) { - var c, - i, - n = fmt.length, - s = '', - pad = function(n, padding) { - padding = padding || '0'; - return n < 10 ? (padding + n) : n; - }; - d || (d = new Date()) - for (i = 0; i < n; ++i) { - c = fmt.charAt(i); - if (c == '%') { - i += 1; - c = fmt.charAt(i); + var WeekdaysShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; + + var Months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', + 'August', 'September', 'October', 'November', 'December']; + + var MonthsShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', + 'Sep', 'Oct', 'Nov', 'Dec']; + + function pad(n, padding) { + padding = padding || '0'; + return n < 10 ? (padding + n) : n; + } + + function hours12(d) { + var hour = d.getHours(); + if (hour == 0) hour = 12; + else if (hour > 12) hour -= 12; + return hour; + } + + // Most of the specifiers supported by C's strftime + function strftime(fmt, d) { + d || (d = new Date()); + return fmt.replace(/%(.)/g, function(_, c) { switch (c) { - case 'A': - s += Weekdays[d.getDay()]; - break; - case 'a': - s += WeekdaysShort[d.getDay()]; - break; - case 'B': - s += Months[d.getMonth()]; - break; - case 'b': - case 'h': - s += MonthsShort[d.getMonth()]; - break; - case 'D': - s += strftime(d, '%m/%d/%y'); - break; - case 'd': - var day = d.getDate(); - s += pad(day); - break; - case 'e': - s += d.getDate(); - break; - case 'F': - s += strftime(d, '%Y-%m-%d'); - break; - case 'H': - var hour = d.getHours(); - s += pad(hour); - break; - case 'I': - var hour = d.getHours(); - if (hour == 0) hour = 12; - else if (hour > 12) hour -= 12; - s += pad(hour); - break; - case 'k': - var hour = d.getHours(); - s += pad(hour, ' '); - break; - case 'l': - var hour = d.getHours(); - if (hour == 0) hour = 12; - else if (hour > 12) hour -= 12; - s += pad(hour, ' '); - break; - case 'M': - var min = d.getMinutes(); - s += pad(min); - break; - case 'm': - var month = d.getMonth() + 1; - s += pad(month); - break; - case 'n': - s += '\n'; - break; - case 'p': - s += d.getHours() < 12 ? 'AM' : 'PM'; - break; - case 'R': - s += strftime(d, '%H:%M'); - break; - case 'r': - s += strftime(d, '%I:%M:%S %p'); - break; - case 'S': - s += pad(d.getSeconds()); - break; - case 's': - s += d.getTime(); - break; - case 'T': - s += strftime(d, '%H:%M:%S'); - break; - case 't': - s += '\t'; - break; + 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 strftime(d, '%m/%d/%y'); + case 'd': return pad(d.getDate()); + case 'e': return d.getDate(); + case 'F': return strftime(d, '%Y-%m-%d'); + case 'H': return pad(d.getHours()); + case 'I': return pad(hours12(d)); + case 'k': return pad(d.getHours(), ' '); + case 'l': return pad(hours12(d), ' '); + 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 strftime(d, '%H:%M'); + case 'r': return strftime(d, '%I:%M:%S %p'); + case 'S': return pad(d.getSeconds()); + case 's': return d.getTime(); + case 'T': return strftime(d, '%H:%M:%S'); + case 't': return '\t'; case 'u': var day = d.getDay(); - s += day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week - break; - case 'v': - s += strftime(d, '%e-%b-%Y'); - break; - case 'w': - s += d.getDay(); // 0 - 6, Sunday is first day of the week - break; - case 'Y': - s += d.getFullYear(); - break; + return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week + case 'v': return strftime(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(); - s += year < 100 ? year : year - 100; - break; + return year < 100 ? year : year - 100; case 'Z': var tz = d.toString().match(/\((\w+)\)/); - s += tz && tz[1] || ''; - break; + return tz && tz[1] || ''; case 'z': var off = d.getTimezoneOffset(); - s += (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60); - break; - default: - s += c; - break; + return (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60); + default: return c; } - } else { - s += c; - } + }); } - return s; -} +}()); + +if (typeof exports !== 'undefined') exports.strftime = strftime; diff --git a/package.json b/package.json index 5c5098a..7515f76 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name" : "strftime" , "description" : "strftime for JavaScript" -, "version" : "0.1" +, "version" : "0.2.0" , "homepage" : "http://samhuri.net/node/strftime" , "author" : "Sami Samhuri " , "repository" :