Comparing version 0.7.0 to 0.8.0
@@ -6,5 +6,5 @@ { | ||
"keywords": ["strftime", "format", "date", "time"], | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"main": "strftime.js", | ||
"scripts": ["strftime.js"] | ||
} |
{ | ||
"name": "strftime", | ||
"description": "strftime for JavaScript", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"homepage": "http://samhuri.net/proj/strftime", | ||
@@ -6,0 +6,0 @@ "author": "Sami Samhuri <sami@samhuri.net>", |
@@ -56,2 +56,9 @@ strftime | ||
Alternatively you can use the timezone format used by ISO 8601, `+HHMM` or `-HHMM`. | ||
var strftimeTZ = require('strftime').strftimeTZ | ||
console.log(strftimeTZ('', new Date(1307472705067), '-0700')) // => June 07, 11 11:51:45 | ||
console.log(strftimeTZ('%F %T', new Date(1307472705067), '+0200')) // => 2011-06-07 20:51:45 | ||
Supported Specifiers | ||
@@ -58,0 +65,0 @@ ==================== |
@@ -1,7 +0,8 @@ | ||
(function(){function f(d,a,b){return g(d,a,b,!1)}function g(d,a,b,f){a&&!m(a)&&(b=a,a=void 0);a=a||new Date;b=b||n;b.formats=b.formats||{};var h=0;f&&(h=(a.getTimezoneOffset()||0)*6E4,a=new Date(a.getTime()+h));return d.replace(/%([-_0]?.)/g,function(d,i){var c;if(i.length==2){c=i[0];if(c=="-")c="";else if(c=="_")c=" ";else if(c=="0")c="0";else return d;i=i[1]}switch(i){case "A":return b.days[a.getDay()];case "a":return b.shortDays[a.getDay()];case "B":return b.months[a.getMonth()];case "b":return b.shortMonths[a.getMonth()]; | ||
case "C":return e(Math.floor(a.getFullYear()/100),c);case "D":return g(b.formats.D||"%m/%d/%y",a,b);case "d":return e(a.getDate(),c);case "e":return a.getDate();case "F":return g(b.formats.F||"%Y-%m-%d",a,b);case "H":return e(a.getHours(),c);case "h":return b.shortMonths[a.getMonth()];case "I":return e(j(a),c);case "j":return c=new Date(a.getFullYear(),0,1),c=Math.ceil((a.getTime()-c.getTime())/864E5),e(c,3);case "k":return e(a.getHours(),c==null?" ":c);case "L":return e(Math.floor(a.getTime()%1E3), | ||
3);case "l":return e(j(a),c==null?" ":c);case "M":return e(a.getMinutes(),c);case "m":return e(a.getMonth()+1,c);case "n":return"\n";case "o":return String(a.getDate())+o(a.getDate());case "P":return a.getHours()<12?b.am:b.pm;case "p":return a.getHours()<12?b.AM:b.PM;case "R":return g(b.formats.R||"%H:%M",a,b);case "r":return g(b.formats.r||"%I:%M:%S %p",a,b);case "S":return e(a.getSeconds(),c);case "s":return Math.floor((a.getTime()-h)/1E3);case "T":return g(b.formats.T||"%H:%M:%S",a,b);case "t":return"\t"; | ||
case "U":return e(k(a,"sunday"),c);case "u":return c=a.getDay(),c==0?7:c;case "v":return g(b.formats.v||"%e-%b-%Y",a,b);case "W":return e(k(a,"monday"),c);case "w":return a.getDay();case "Y":return a.getFullYear();case "y":return c=String(a.getFullYear()),c.slice(c.length-2);case "Z":return f?"GMT":(c=a.toString().match(/\((\w+)\)/))&&c[1]||"";case "z":return f?"+0000":(c=a.getTimezoneOffset(),(c<0?"+":"-")+e(Math.abs(c/60))+e(c%60));default:return i}})}function m(d){for(var a=0,b=l.length,a=0;a< | ||
b;++a)if(typeof d[l[a]]!="function")return!1;return!0}function e(d,a,b){typeof a==="number"&&(b=a,a="0");a==null&&(a="0");b=b||2;d=String(d);if(a)for(;d.length<b;)d=a+d;return d}function j(d){d=d.getHours();d==0?d=12:d>12&&(d-=12);return d}function o(d){var a=d%10;d%=100;if(d>=11&&d<=13||a===0||a>=4)return"th";switch(a){case 1:return"st";case 2:return"nd";case 3:return"rd"}}function k(d,a){var a=a||"sunday",b=d.getDay();a=="monday"&&(b==0?b=6:b--);var e=new Date(d.getFullYear(),0,1);return Math.floor(((d- | ||
e)/864E5+7-b)/7)}var h;h=typeof module!=="undefined"?module.exports=f:function(){return this||(0,eval)("this")}();h.strftime=f;h.strftimeUTC=f.strftimeUTC=function(d,a,b){return g(d,a,b,!0)};h.localizedStrftime=f.localizedStrftime=function(d){return function(a,b){return f(a,b,d)}};var n={days:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),shortDays:"Sun Mon Tue Wed Thu Fri Sat".split(" "),months:"January February March April May June July August September October November December".split(" "), | ||
shortMonths:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),AM:"AM",PM:"PM",am:"am",pm:"pm"},l=["getTime","getTimezoneOffset","getDay","getDate","getMonth","getFullYear","getYear","getHours","getMinutes","getSeconds"]})(); | ||
(function(){function i(c,a,b){return g(c,a,b)}function g(c,a,b,j){j=j||{};a&&!n(a)&&(b=a,a=void 0);a=a||new Date;b=b||o;b.formats=b.formats||{};var i=a.getTime(),h=j.timezone,e=typeof h;if(j.utc||e=="number"||e=="string")a=p(a);if(h){if(e=="string")var k=h[0]=="-"?-1:1,q=parseInt(h.slice(1,3),10),r=parseInt(h.slice(3,5),10),h=k*60*q+r;e&&(a=new Date(a.getTime()+h*6E4))}return c.replace(/%([-_0]?.)/g,function(c,e){var d;if(e.length==2){d=e[0];if(d=="-")d="";else if(d=="_")d=" ";else if(d=="0")d="0"; | ||
else return c;e=e[1]}switch(e){case "A":return b.days[a.getDay()];case "a":return b.shortDays[a.getDay()];case "B":return b.months[a.getMonth()];case "b":return b.shortMonths[a.getMonth()];case "C":return f(Math.floor(a.getFullYear()/100),d);case "D":return g(b.formats.D||"%m/%d/%y",a,b);case "d":return f(a.getDate(),d);case "e":return a.getDate();case "F":return g(b.formats.F||"%Y-%m-%d",a,b);case "H":return f(a.getHours(),d);case "h":return b.shortMonths[a.getMonth()];case "I":return f(l(a),d); | ||
case "j":return d=new Date(a.getFullYear(),0,1),d=Math.ceil((a.getTime()-d.getTime())/864E5),f(d,3);case "k":return f(a.getHours(),d==null?" ":d);case "L":return f(Math.floor(i%1E3),3);case "l":return f(l(a),d==null?" ":d);case "M":return f(a.getMinutes(),d);case "m":return f(a.getMonth()+1,d);case "n":return"\n";case "o":return String(a.getDate())+s(a.getDate());case "P":return a.getHours()<12?b.am:b.pm;case "p":return a.getHours()<12?b.AM:b.PM;case "R":return g(b.formats.R||"%H:%M",a,b);case "r":return g(b.formats.r|| | ||
"%I:%M:%S %p",a,b);case "S":return f(a.getSeconds(),d);case "s":return Math.floor(i/1E3);case "T":return g(b.formats.T||"%H:%M:%S",a,b);case "t":return"\t";case "U":return f(m(a,"sunday"),d);case "u":return d=a.getDay(),d==0?7:d;case "v":return g(b.formats.v||"%e-%b-%Y",a,b);case "W":return f(m(a,"monday"),d);case "w":return a.getDay();case "Y":return a.getFullYear();case "y":return d=String(a.getFullYear()),d.slice(d.length-2);case "Z":return j.utc?"GMT":(d=a.toString().match(/\((\w+)\)/))&&d[1]|| | ||
"";case "z":return j.utc?"+0000":(d=typeof h=="number"?h:-a.getTimezoneOffset(),(d<0?"-":"+")+f(Math.abs(d/60))+f(d%60));default:return e}})}function p(c){var a=(c.getTimezoneOffset()||0)*6E4;return new Date(c.getTime()+a)}function n(c){for(var a=0,b=k.length,a=0;a<b;++a)if(typeof c[k[a]]!="function")return!1;return!0}function f(c,a,b){typeof a==="number"&&(b=a,a="0");a==null&&(a="0");b=b||2;c=String(c);if(a)for(;c.length<b;)c=a+c;return c}function l(c){c=c.getHours();c==0?c=12:c>12&&(c-=12);return c} | ||
function s(c){var a=c%10;c%=100;if(c>=11&&c<=13||a===0||a>=4)return"th";switch(a){case 1:return"st";case 2:return"nd";case 3:return"rd"}}function m(c,a){var a=a||"sunday",b=c.getDay();a=="monday"&&(b==0?b=6:b--);var e=new Date(c.getFullYear(),0,1);return Math.floor(((c-e)/864E5+7-b)/7)}var e;e=typeof module!=="undefined"?module.exports=i:function(){return this||(0,eval)("this")}();var o={days:"Sunday Monday Tuesday Wednesday Thursday Friday Saturday".split(" "),shortDays:"Sun Mon Tue Wed Thu Fri Sat".split(" "), | ||
months:"January February March April May June July August September October November December".split(" "),shortMonths:"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(" "),AM:"AM",PM:"PM",am:"am",pm:"pm"};e.strftime=i;e.strftimeTZ=i.strftimeTZ=function(c,a,b,e){if((typeof b=="number"||typeof b=="string")&&e==null)e=b,b=void 0;return g(c,a,b,{timezone:e})};e.strftimeUTC=i.strftimeUTC=function(c,a,b){return g(c,a,b,{utc:!0})};e.localizedStrftime=i.localizedStrftime=function(c){return function(a, | ||
b){return g(a,b,c)}};var k=["getTime","getTimezoneOffset","getDay","getDate","getMonth","getFullYear","getYear","getHours","getMinutes","getSeconds"]})(); |
108
strftime.js
@@ -49,3 +49,3 @@ // | ||
function strftimeTZ(fmt, d, locale, timezone) { | ||
if (typeof locale == 'number' && timezone == null) { | ||
if ((typeof locale == 'number' || typeof locale == 'string') && timezone == null) { | ||
timezone = locale; | ||
@@ -92,8 +92,23 @@ locale = undefined; | ||
if (options.utc || typeof options.timezone == 'number') { | ||
var tz = options.timezone; | ||
var tzType = typeof tz; | ||
if (options.utc || tzType == 'number' || tzType == 'string') { | ||
d = dateToUTC(d); | ||
} | ||
if (typeof options.timezone == 'number') { | ||
d = new Date(d.getTime() + (options.timezone * 60000)); | ||
if (tz) { | ||
// ISO 8601 format timezone string, [-+]HHMM | ||
// | ||
// Convert to the number of minutes and it'll be applied to the date below. | ||
if (tzType == 'string') { | ||
var sign = tz[0] == '-' ? -1 : 1; | ||
var hours = parseInt(tz.slice(1, 3), 10); | ||
var mins = parseInt(tz.slice(3, 5), 10); | ||
tz = sign * (60 * hours) + mins; | ||
} | ||
if (tzType) { | ||
d = new Date(d.getTime() + (tz * 60000)); | ||
} | ||
} | ||
@@ -106,2 +121,3 @@ | ||
var mod, padding; | ||
if (c.length == 2) { | ||
@@ -127,15 +143,44 @@ mod = c[0]; | ||
} | ||
switch (c) { | ||
// Examples for new Date(0) in GMT | ||
// 'Thursday' | ||
case 'A': return locale.days[d.getDay()]; | ||
// 'Thu' | ||
case 'a': return locale.shortDays[d.getDay()]; | ||
// 'January' | ||
case 'B': return locale.months[d.getMonth()]; | ||
// 'Jan' | ||
case 'b': return locale.shortMonths[d.getMonth()]; | ||
// '19' | ||
case 'C': return pad(Math.floor(d.getFullYear() / 100), padding); | ||
// '01/01/70' | ||
case 'D': return _strftime(locale.formats.D || '%m/%d/%y', d, locale); | ||
// '01' | ||
case 'd': return pad(d.getDate(), padding); | ||
// '01' | ||
case 'e': return d.getDate(); | ||
// '1970-01-01' | ||
case 'F': return _strftime(locale.formats.F || '%Y-%m-%d', d, locale); | ||
// '00' | ||
case 'H': return pad(d.getHours(), padding); | ||
// 'Jan' | ||
case 'h': return locale.shortMonths[d.getMonth()]; | ||
// '12' | ||
case 'I': return pad(hours12(d), padding); | ||
// '000' | ||
case 'j': | ||
@@ -145,28 +190,74 @@ var y = new Date(d.getFullYear(), 0, 1); | ||
return pad(day, 3); | ||
// ' 0' | ||
case 'k': return pad(d.getHours(), padding == null ? ' ' : padding); | ||
// '000' | ||
case 'L': return pad(Math.floor(timestamp % 1000), 3); | ||
// '12' | ||
case 'l': return pad(hours12(d), padding == null ? ' ' : padding); | ||
// '00' | ||
case 'M': return pad(d.getMinutes(), padding); | ||
// '01' | ||
case 'm': return pad(d.getMonth() + 1, padding); | ||
// '\n' | ||
case 'n': return '\n'; | ||
// '1st' | ||
case 'o': return String(d.getDate()) + ordinal(d.getDate()); | ||
// 'am' | ||
case 'P': return d.getHours() < 12 ? locale.am : locale.pm; | ||
// 'AM' | ||
case 'p': return d.getHours() < 12 ? locale.AM : locale.PM; | ||
// '00:00' | ||
case 'R': return _strftime(locale.formats.R || '%H:%M', d, locale); | ||
// '12:00:00 AM' | ||
case 'r': return _strftime(locale.formats.r || '%I:%M:%S %p', d, locale); | ||
// '00' | ||
case 'S': return pad(d.getSeconds(), padding); | ||
// '0' | ||
case 's': return Math.floor(timestamp / 1000); | ||
// '00:00:00' | ||
case 'T': return _strftime(locale.formats.T || '%H:%M:%S', d, locale); | ||
// '\t' | ||
case 't': return '\t'; | ||
// '00' | ||
case 'U': return pad(weekNumber(d, 'sunday'), padding); | ||
// '4' | ||
case 'u': | ||
var day = d.getDay(); | ||
return day == 0 ? 7 : day; // 1 - 7, Monday is first day of the week | ||
// '1-Jan-1970' | ||
case 'v': return _strftime(locale.formats.v || '%e-%b-%Y', d, locale); | ||
// '00' | ||
case 'W': return pad(weekNumber(d, 'monday'), padding); | ||
// '4' | ||
case 'w': return d.getDay(); // 0 - 6, Sunday is first day of the week | ||
// '1970' | ||
case 'Y': return d.getFullYear(); | ||
// '70' | ||
case 'y': | ||
var y = String(d.getFullYear()); | ||
return y.slice(y.length - 2); | ||
// 'GMT' | ||
case 'Z': | ||
@@ -177,5 +268,7 @@ if (options.utc) { | ||
else { | ||
var tz = d.toString().match(/\((\w+)\)/); | ||
return tz && tz[1] || ''; | ||
var tzString = d.toString().match(/\((\w+)\)/); | ||
return tzString && tzString[1] || ''; | ||
} | ||
// '+0000' | ||
case 'z': | ||
@@ -186,5 +279,6 @@ if (options.utc) { | ||
else { | ||
var off = typeof options.timezone == 'number' ? options.timezone : -d.getTimezoneOffset(); | ||
var off = typeof tz == 'number' ? tz : -d.getTimezoneOffset(); | ||
return (off < 0 ? '-' : '+') + pad(Math.abs(off / 60)) + pad(off % 60); | ||
} | ||
default: return c; | ||
@@ -191,0 +285,0 @@ } |
@@ -42,3 +42,3 @@ #!/usr/bin/env node | ||
/// time zones | ||
if (!process.env.TZ || process.env.TZ == 'America/Vancouver') { | ||
if (process.env.TZ == 'America/Vancouver') { | ||
testTimezone('P[DS]T') | ||
@@ -56,3 +56,3 @@ assert.format('%C', '01', '01', new Date(100, 0, 1)) | ||
else { | ||
console.log('(Current timezone has no tests: ' + process.env.TZ + ')') | ||
console.log('(Current timezone has no tests: ' + (process.env.TZ || 'none') + ')') | ||
} | ||
@@ -174,4 +174,7 @@ | ||
assert.formatTZ('%F %r %z', '2011-06-07 06:51:45 PM +0000', 0) | ||
assert.formatTZ('%F %r %z', '2011-06-07 06:51:45 PM +0000', '+0000') | ||
assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', 120) | ||
assert.formatTZ('%F %r %z', '2011-06-07 08:51:45 PM +0200', '+0200') | ||
assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', -420) | ||
assert.formatTZ('%F %r %z', '2011-06-07 11:51:45 AM -0700', '-0700') | ||
ok('Time zone offset') | ||
@@ -178,0 +181,0 @@ |
Sorry, the diff of this file is not supported yet
27551
520
136
6