commit f832767c6cfc0d7a4f61ac76c7c1906de143d08d Author: Sami Samhuri Date: Thu Nov 11 13:08:32 2010 -0800 first commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c37eb40 --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +Copyright 2010 Sami Samhuri. All rights reserved. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to +deal in the Software without restriction, including without limitation the +rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +sell copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +IN THE SOFTWARE. diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..d5500f1 --- /dev/null +++ b/Readme.md @@ -0,0 +1,26 @@ +strftime +======== + +strftime for JavaScript + + +Installation +============ + +npm install strftime + + +Usage +===== + + var strftime = require('strftime').strftime + console.log(strftime('%D-%M-%Y %H:%M:%S')) + +For details just see `man 3 strftime` as the format specifiers are identical. + +License +======= + +Copyright 2010 Sami Samhuri sami.samhuri@gmail.com + +MIT (see included [LICENSE](/samsonjs/strftime/blob/master/LICENSE)) diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..a97a0aa --- /dev/null +++ b/lib/index.js @@ -0,0 +1,150 @@ +/// strftime +/// http://github.com/samsonjs/strftime +/// @_sjs +/// +/// Copyright 2010 Sami Samhuri +/// MIT License + +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); + 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 '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; + case 'y': + var year = d.getYear(); + s += year < 100 ? year : year - 100; + break; + case 'Z': + var tz = d.toString().match(/\((\w+)\)/); + s += tz && tz[1] || ''; + break; + case 'z': + var off = d.getTimezoneOffset(); + s += (off < 0 ? '-' : '+') + pad(off / 60) + pad(off % 60); + break; + default: + s += c; + break; + } + } else { + s += c; + } + } + return s; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..5c5098a --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ "name" : "strftime" +, "description" : "strftime for JavaScript" +, "version" : "0.1" +, "homepage" : "http://samhuri.net/node/strftime" +, "author" : "Sami Samhuri " +, "repository" : + { "type" : "git" + , "url" : "http://github.com/samsonjs/strftime.git" + } +, "bugs" : + { "mail" : "sami.samhuri+strftime@gmail.com" + , "web" : "http://github.com/samsonjs/strftime/issues" + } +, "directories" : { "lib" : "./lib" } +, "main" : "./lib/index" +, "engines" : { "node" : ">=0.3.0" } +, "licenses" : + [ { "type" : "MIT" + , "url" : "http://github.com/samsonjs/strftime/raw/master/LICENSE" + } + ] +} \ No newline at end of file