Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonjs committed Nov 11, 2010
0 parents commit f832767
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 0 deletions.
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
26 changes: 26 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -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 [email protected]

MIT (see included [LICENSE](/samsonjs/strftime/blob/master/LICENSE))
150 changes: 150 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/// strftime
/// http://github.com/samsonjs/strftime
/// @_sjs
///
/// Copyright 2010 Sami Samhuri <[email protected]>
/// 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;
}
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ "name" : "strftime"
, "description" : "strftime for JavaScript"
, "version" : "0.1"
, "homepage" : "http://samhuri.net/node/strftime"
, "author" : "Sami Samhuri <[email protected]>"
, "repository" :
{ "type" : "git"
, "url" : "http://github.com/samsonjs/strftime.git"
}
, "bugs" :
{ "mail" : "[email protected]"
, "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"
}
]
}

0 comments on commit f832767

Please sign in to comment.