From 2804e34ca7b63ee08b148cea6a3ff4cefe1b7aad Mon Sep 17 00:00:00 2001 From: fresk Date: Thu, 26 Nov 2015 16:35:27 +0300 Subject: [PATCH] =?UTF-8?q?strftime=20=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=B5?= =?UTF-8?q?=20=D0=BD=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B7=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=D1=81=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B4=D0=B0=D0=BD=D0=BD=D1=83=D1=8E=20=D0=B5=D0=B9=20?= =?UTF-8?q?=D0=B4=D0=B0=D1=82=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/strftime.js | 16 +++++++++------- strftime.js | 16 +++++++++------- strftime.jsx | 16 +++++++++------- strftime.min.js | 2 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/lib/strftime.js b/lib/strftime.js index 580e8e5..abd2e88 100644 --- a/lib/strftime.js +++ b/lib/strftime.js @@ -14,21 +14,23 @@ */ /*jshint -W079 */ var strftime = function(format, date, utc, localeName) { - if (typeof date === 'undefined') { - date = new Date(); + var newDate = date; + + if (typeof newDate === 'undefined') { + newDate = new Date(); } - if (typeof date === 'number') { - date = new Date(date); + if (typeof newDate === 'number') { + newDate = new Date(newDate); } - if (!(date instanceof Date) || String(date) === 'Invalid Date') { + if (!(newDate instanceof Date) || String(newDate) === 'Invalid Date') { return null; } if (utc) { - date.setTime(date.getTime() + date.getTimezoneOffset() * 60000); + newDate = new Date(newDate.getTime() + newDate.getTimezoneOffset() * 60000); } - return strftime.format(format, date, localeName); + return strftime.format(format, newDate, localeName); }; diff --git a/strftime.js b/strftime.js index c8b432d..f09112a 100644 --- a/strftime.js +++ b/strftime.js @@ -18,23 +18,25 @@ */ /*jshint -W079 */ var strftime = function(format, date, utc, localeName) { - if (typeof date === 'undefined') { - date = new Date(); + var newDate = date; + + if (typeof newDate === 'undefined') { + newDate = new Date(); } - if (typeof date === 'number') { - date = new Date(date); + if (typeof newDate === 'number') { + newDate = new Date(newDate); } - if (!(date instanceof Date) || String(date) === 'Invalid Date') { + if (!(newDate instanceof Date) || String(newDate) === 'Invalid Date') { return null; } if (utc) { - date.setTime(date.getTime() + date.getTimezoneOffset() * 60000); + newDate = new Date(newDate.getTime() + newDate.getTimezoneOffset() * 60000); } - return strftime.format(format, date, localeName); + return strftime.format(format, newDate, localeName); }; diff --git a/strftime.jsx b/strftime.jsx index a3314f7..5278e71 100644 --- a/strftime.jsx +++ b/strftime.jsx @@ -18,23 +18,25 @@ */ /*jshint -W079 */ var strftime = function(format, date, utc, localeName) { - if (typeof date === 'undefined') { - date = new Date(); + var newDate = date; + + if (typeof newDate === 'undefined') { + newDate = new Date(); } - if (typeof date === 'number') { - date = new Date(date); + if (typeof newDate === 'number') { + newDate = new Date(newDate); } - if (!(date instanceof Date) || String(date) === 'Invalid Date') { + if (!(newDate instanceof Date) || String(newDate) === 'Invalid Date') { return null; } if (utc) { - date.setTime(date.getTime() + date.getTimezoneOffset() * 60000); + newDate = new Date(newDate.getTime() + newDate.getTimezoneOffset() * 60000); } - return strftime.format(format, date, localeName); + return strftime.format(format, newDate, localeName); }; diff --git a/strftime.min.js b/strftime.min.js index 42f4073..8d00cdb 100644 --- a/strftime.min.js +++ b/strftime.min.js @@ -1 +1 @@ -!function(){"use strict";var strftime=function(format,date,utc,localeName){if(typeof date==="undefined"){date=new Date}if(typeof date==="number"){date=new Date(date)}if(!(date instanceof Date)||String(date)==="Invalid Date"){return null}if(utc){date.setTime(date.getTime()+date.getTimezoneOffset()*6e4)}return strftime.format(format,date,localeName)};strftime.version="0.0.1";var namespace;if(typeof module!=="undefined"){namespace=module.exports=strftime}else{namespace=function(){return this||(1,eval)("this")}()}namespace.strftime=strftime;!function(strftime){var locales={};strftime.setLocale=function(name,data){locales[name]=data};strftime.getLocale=function(name){name=name||"en";return locales[name]||{}};strftime.setLocale("en",{a:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],A:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],b:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],B:["January","February","March","April","May","June","July","August","September","October","November","December"],f:["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."],c:"%Y-%m-%d %H:%M:%S",P:["am","pm"],r:"%I:%M:%S %p",x:"%m/%d/%y",X:"%H:%M:%S",day:["Yesterday","Today","Tomorrow"],bg:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],Bg:["January","February","March","April","May","June","July","August","September","October","November","December"],fg:["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."],Date_dBY_year_in_HM:"%#B %-d, %Y at %-H:%M",Date_dBY_year:"%#B %-d, %Y",Date_dBY:"%#B %-d, %Y",Date_AdBY:"%A, %#B %-d, %Y",Date_dBA:"%#B %-d, %A",Date_df_in_HM:"%#f, %-d at %-H:%M",Date_dfY:"%-d %#f %Y",Date_dB_in_HM:"%#B %-d at %-H:%M",Date_df:"%-d %#f"})}(strftime);!function(strftime){var currentLocale;var regAgregat=/%(Date_[a-zA-Z0-9_]+|([#\^]?)[v]|[cDFrRTxX])/g;var regAgregatSearch=/%(Date_[a-zA-Z0-9_]+|[#\^]?[v]|[cDFrRTxX])/;var regSpec=/%(([#\^!~]{0,2})[aAbBfh]|([0\-_]?)[CdegHIjmMSVWyl]|[GnpPtuUwYzZs%])/g;var specifiers={a:function(d,mode){return toLetterCase(currentLocale.a[d.getDay()],mode)},A:function(d,mode){return toLetterCase(currentLocale.A[d.getDay()],mode)},b:function(d,mode,numPad,genitive){return toLetterCase(currentLocale[genitive?"bg":"b"][d.getMonth()],mode)},h:function(d,mode,numPad,genitive){return specifiers.b(d,mode,numPad,genitive)},f:function(d,mode,numPad,genitive){return toLetterCase(currentLocale[genitive?"fg":"f"][d.getMonth()],mode)},B:function(d,mode,numPad,genitive){return toLetterCase(currentLocale[genitive?"Bg":"B"][d.getMonth()],mode)},c:function(){return currentLocale.c},C:function(d,mode,numPad){return pad(parseInt(d.getFullYear()/100,10),numPad,0)},d:function(d,mode,numPad){return pad(d.getDate(),numPad,0)},D:function(){return"%m/%d/%y"},e:function(d,mode,numPad){return pad(d.getDate(),numPad," ")},F:function(){return"%Y-%m-%d"},g:function(d,mode,numPad){return pad(specifiers.G(d)%100,numPad,0)},G:function(d){var y=parseInt(d.getFullYear(),10);var V=parseInt(specifiers.V(d),10);var W=parseInt(specifiers.W(d),10);if(W>V){y++}else if(W===0&&V>=52){y--}return y},H:function(d,mode,numPad){return pad(d.getHours(),numPad,0)},I:function(d,mode,numPad){var hour=d.getHours()%12;return pad(hour===0?12:hour,numPad,0)},j:function(d,mode,numPad){var ms=d-new Date(String(d.getFullYear())+"/1/1 GMT");ms+=d.getTimezoneOffset()*6e4;var day=parseInt(1+ms/6e4/60/24,10);return pad(day,numPad,0,100)},m:function(d,mode,numPad){return pad(d.getMonth()+1,numPad,0)},M:function(d,mode,numPad){return pad(d.getMinutes(),numPad,0)},n:function(){return"\n"},p:function(d){var p=d.getHours()>=12?1:0;return String(currentLocale.P[p]).toUpperCase()},P:function(d){var p=d.getHours()>=12?1:0;return String(currentLocale.P[p])},r:function(){return currentLocale.r},R:function(){return"%H:%M"},S:function(d,mode,numPad){return pad(d.getSeconds(),numPad,0)},t:function(){return" "},T:function(){return"%H:%M:%S"},u:function(d){var day=d.getDay();return day===0?7:day},U:function(d){var day=parseInt(specifiers.j(d),10);var rdow=6-d.getDay();var woy=parseInt((day+rdow)/7,10);return pad(woy,0)},V:function(d,mode,numPad){var woy=parseInt(specifiers.W(d),10);var dow1_1=new Date(d.getFullYear()+"/1/1").getDay();var idow=woy+(dow1_1>4||dow1_1<=1?0:1);if(idow===53&&new Date(d.getFullYear()+"/12/31").getDay()<4){idow=1}else if(idow===0){idow=specifiers.V(new Date(d.getFullYear()-1+"/12/31"))}return pad(idow,numPad,0)},w:function(d){return d.getDay()},W:function(d,mode,numPad){var day=parseInt(specifiers.j(d),10);var rdow=7-parseInt(specifiers.u(d),10);var woy=parseInt((day+rdow)/7,10);return pad(woy,numPad,0,10)},x:function(){return currentLocale.x},X:function(){return currentLocale.X},y:function(d,mode,numPad){return pad(d.getFullYear()%100,numPad,0)},Y:function(d){return d.getFullYear()},z:function(d){var o=d.getTimezoneOffset();var H=pad(parseInt(Math.abs(o/60),10),0);var M=pad(o%60,0);return(o>0?"-":"+")+H+M},Z:function(d){return d.toString().replace(/^.*\(([^)]+)\)$/,"$1")},l:function(d,mode,numPad){var l=d.getHours()%12;return pad(l===0?12:l,numPad," ")},s:function(d){return parseInt(d.getTime()/1e3,10)},"%":function(){return"%"},v:function(d,mode){var now=new Date;var td=d.getTime()+d.getTimezoneOffset()*6e4;var time=now.getTime()+now.getTimezoneOffset()*6e4;var diff=Math.ceil((td-time)/6e4/60/24)+1;if(currentLocale.day[diff]){return toLetterCase(currentLocale.day[diff],mode)}else{return"%d %"+mode+"B"}},Date_iso:function(){return"%Y-%m-%dT%H:%M:%S"},Date_dBY_year_in_HM:function(){return currentLocale.Date_dBY_year_in_HM},Date_dBY_year:function(){return currentLocale.Date_dBY_year},Date_dBY:function(){return currentLocale.Date_dBY},Date_dBA:function(){return currentLocale.Date_dBA},Date_AdBY:function(){return currentLocale.Date_AdBY},Date_df_in_HM:function(){return currentLocale.Date_df_in_HM},Date_dfY:function(){return currentLocale.Date_dfY},Date_dB_in_HM:function(){return currentLocale.Date_dB_in_HM},Date_dmY__dot:function(){return"%d.%m.%Y"},Date_df:function(){return currentLocale.Date_df},Date_FT:function(){return"%F %T"},Date_dmY__minus:function(){return"%d-%m-%Y"}};strftime.format=function(format,date,localeName){formatTransform.date=date;currentLocale=strftime.getLocale(localeName);var loop=5;while(regAgregatSearch.test(format)&&loop){format=format.replace(regAgregat,formatTransform);loop--}return format.replace(regSpec,formatTransform)};function formatTransform(_,spec,mod,numPad,pos,str){spec=String(spec);mod=String(mod);spec=spec.replace(/^[#_0\^\-!~]+/,"");var s=specifiers[spec];if(!s){return _}var genitive=false;if(mod.indexOf("!")===-1&&spec.length===1&&(mod.indexOf("~")>-1||"bBf".indexOf(spec)>-1&&/%[0\-_]?d[\s]+$/.test(str.substr(0,pos)))){genitive=true}return s(formatTransform.date,mod,numPad,genitive)}function pad(aData,aPad,aDef,aRate){if(aPad==="-"){return aData}if(aPad==="_"){aPad=" "}if(typeof aDef==="undefined"){aDef=0}if(typeof aRate==="undefined"){aRate=10}if(typeof aPad==="undefined"||aPad===""){aPad=aDef}for(;aRate>parseInt(aData,10)&&aRate>1;aRate/=10){aData=aPad.toString()+aData}return aData.toString()}function toLetterCase(str,mode){str=String(str);mode=String(mode);if(mode.indexOf("#")>-1){return str.toLowerCase()}if(mode.indexOf("^")>-1){return str.toUpperCase()}return str.substr(0,1).toUpperCase()+str.substr(1)}}(strftime)}(); \ No newline at end of file +!function(){"use strict";var strftime=function(format,date,utc,localeName){var newDate=date;if(typeof newDate==="undefined"){newDate=new Date}if(typeof newDate==="number"){newDate=new Date(newDate)}if(!(newDate instanceof Date)||String(newDate)==="Invalid Date"){return null}if(utc){newDate=new Date(newDate.getTime()+newDate.getTimezoneOffset()*6e4)}return strftime.format(format,newDate,localeName)};strftime.version="0.0.1";var namespace;if(typeof module!=="undefined"){namespace=module.exports=strftime}else{namespace=function(){return this||(1,eval)("this")}()}namespace.strftime=strftime;!function(strftime){var locales={};strftime.setLocale=function(name,data){locales[name]=data};strftime.getLocale=function(name){name=name||"en";return locales[name]||{}};strftime.setLocale("en",{a:["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],A:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],b:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],B:["January","February","March","April","May","June","July","August","September","October","November","December"],f:["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."],c:"%Y-%m-%d %H:%M:%S",P:["am","pm"],r:"%I:%M:%S %p",x:"%m/%d/%y",X:"%H:%M:%S",day:["Yesterday","Today","Tomorrow"],bg:["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],Bg:["January","February","March","April","May","June","July","August","September","October","November","December"],fg:["Jan.","Feb.","Mar.","Apr.","May","Jun.","Jul.","Aug.","Sep.","Oct.","Nov.","Dec."],Date_dBY_year_in_HM:"%#B %-d, %Y at %-H:%M",Date_dBY_year:"%#B %-d, %Y",Date_dBY:"%#B %-d, %Y",Date_AdBY:"%A, %#B %-d, %Y",Date_dBA:"%#B %-d, %A",Date_df_in_HM:"%#f, %-d at %-H:%M",Date_db_in_HM:"%#b, %-d at %-H:%M",Date_dfY:"%-d %#f %Y",Date_dB_in_HM:"%#B %-d at %-H:%M",Date_df:"%-d %#f",Date_db:"%-d %#b",Date_today_at_HM:"today at %-H:%M",Date_dmy_at_HM:"%d.%m.%y at %-H:%M"})}(strftime);!function(strftime){var currentLocale;var regAgregat=/%(Date_[a-zA-Z0-9_]+|([#\^]?)[v]|[cDFrRTxX])/g;var regAgregatSearch=/%(Date_[a-zA-Z0-9_]+|[#\^]?[v]|[cDFrRTxX])/;var regSpec=/%(([#\^!~]{0,2})[aAbBfh]|([0\-_]?)[CdegHIjmMSVWyl]|[GnpPtuUwYzZs%])/g;var specifiers={a:function(d,mode){return toLetterCase(currentLocale.a[d.getDay()],mode)},A:function(d,mode){return toLetterCase(currentLocale.A[d.getDay()],mode)},b:function(d,mode,numPad,genitive){return toLetterCase(currentLocale[genitive?"bg":"b"][d.getMonth()],mode)},h:function(d,mode,numPad,genitive){return specifiers.b(d,mode,numPad,genitive)},f:function(d,mode,numPad,genitive){return toLetterCase(currentLocale[genitive?"fg":"f"][d.getMonth()],mode)},B:function(d,mode,numPad,genitive){return toLetterCase(currentLocale[genitive?"Bg":"B"][d.getMonth()],mode)},c:function(){return currentLocale.c},C:function(d,mode,numPad){return pad(parseInt(d.getFullYear()/100,10),numPad,0)},d:function(d,mode,numPad){return pad(d.getDate(),numPad,0)},D:function(){return"%m/%d/%y"},e:function(d,mode,numPad){return pad(d.getDate(),numPad," ")},F:function(){return"%Y-%m-%d"},g:function(d,mode,numPad){return pad(specifiers.G(d)%100,numPad,0)},G:function(d){var y=parseInt(d.getFullYear(),10);var V=parseInt(specifiers.V(d),10);var W=parseInt(specifiers.W(d),10);if(W>V){y++}else if(W===0&&V>=52){y--}return y},H:function(d,mode,numPad){return pad(d.getHours(),numPad,0)},I:function(d,mode,numPad){var hour=d.getHours()%12;return pad(hour===0?12:hour,numPad,0)},j:function(d,mode,numPad){var ms=d-new Date(String(d.getFullYear())+"/1/1 GMT");ms+=d.getTimezoneOffset()*6e4;var day=parseInt(1+ms/6e4/60/24,10);return pad(day,numPad,0,100)},m:function(d,mode,numPad){return pad(d.getMonth()+1,numPad,0)},M:function(d,mode,numPad){return pad(d.getMinutes(),numPad,0)},n:function(){return"\n"},p:function(d){var p=d.getHours()>=12?1:0;return String(currentLocale.P[p]).toUpperCase()},P:function(d){var p=d.getHours()>=12?1:0;return String(currentLocale.P[p])},r:function(){return currentLocale.r},R:function(){return"%H:%M"},S:function(d,mode,numPad){return pad(d.getSeconds(),numPad,0)},t:function(){return" "},T:function(){return"%H:%M:%S"},u:function(d){var day=d.getDay();return day===0?7:day},U:function(d){var day=parseInt(specifiers.j(d),10);var rdow=6-d.getDay();var woy=parseInt((day+rdow)/7,10);return pad(woy,0)},V:function(d,mode,numPad){var woy=parseInt(specifiers.W(d),10);var dow1_1=new Date(d.getFullYear()+"/1/1").getDay();var idow=woy+(dow1_1>4||dow1_1<=1?0:1);if(idow===53&&new Date(d.getFullYear()+"/12/31").getDay()<4){idow=1}else if(idow===0){idow=specifiers.V(new Date(d.getFullYear()-1+"/12/31"))}return pad(idow,numPad,0)},w:function(d){return d.getDay()},W:function(d,mode,numPad){var day=parseInt(specifiers.j(d),10);var rdow=7-parseInt(specifiers.u(d),10);var woy=parseInt((day+rdow)/7,10);return pad(woy,numPad,0,10)},x:function(){return currentLocale.x},X:function(){return currentLocale.X},y:function(d,mode,numPad){return pad(d.getFullYear()%100,numPad,0)},Y:function(d){return d.getFullYear()},z:function(d){var o=d.getTimezoneOffset();var H=pad(parseInt(Math.abs(o/60),10),0);var M=pad(o%60,0);return(o>0?"-":"+")+H+M},Z:function(d){return d.toString().replace(/^.*\(([^)]+)\)$/,"$1")},l:function(d,mode,numPad){var l=d.getHours()%12;return pad(l===0?12:l,numPad," ")},s:function(d){return parseInt(d.getTime()/1e3,10)},"%":function(){return"%"},v:function(d,mode){var now=new Date;var td=d.getTime()+d.getTimezoneOffset()*6e4;var time=now.getTime()+now.getTimezoneOffset()*6e4;var diff=Math.ceil((td-time)/6e4/60/24)+1;if(currentLocale.day[diff]){return toLetterCase(currentLocale.day[diff],mode)}else{return"%d %"+mode+"B"}},Date_iso:function(){return"%Y-%m-%dT%H:%M:%S"},Date_dBY_year_in_HM:function(){return currentLocale.Date_dBY_year_in_HM},Date_dBY_year:function(){return currentLocale.Date_dBY_year},Date_dBY:function(){return currentLocale.Date_dBY},Date_dBA:function(){return currentLocale.Date_dBA},Date_AdBY:function(){return currentLocale.Date_AdBY},Date_df_in_HM:function(){return currentLocale.Date_df_in_HM},Date_db_in_HM:function(){return currentLocale.Date_db_in_HM},Date_dfY:function(){return currentLocale.Date_dfY},Date_dB_in_HM:function(){return currentLocale.Date_dB_in_HM},Date_dmY__dot:function(){return"%d.%m.%Y"},Date_df:function(){return currentLocale.Date_df},Date_db:function(){return currentLocale.Date_db},Date_FT:function(){return"%F %T"},Date_dmY__minus:function(){return"%d-%m-%Y"},Date_today_at_HM:function(){return currentLocale.Date_today_at_HM},Date_dmy_at_HM:function(){return currentLocale.Date_dmy_at_HM}};strftime.format=function(format,date,localeName){formatTransform.date=date;currentLocale=strftime.getLocale(localeName);var loop=5;while(regAgregatSearch.test(format)&&loop){format=format.replace(regAgregat,formatTransform);loop--}return format.replace(regSpec,formatTransform)};function formatTransform(_,spec,mod,numPad,pos,str){spec=String(spec);mod=String(mod);spec=spec.replace(/^[#_0\^\-!~]+/,"");var s=specifiers[spec];if(!s){return _}var genitive=false;if(mod.indexOf("!")===-1&&spec.length===1&&(mod.indexOf("~")>-1||"bBf".indexOf(spec)>-1&&/%[0\-_]?d[\s]+$/.test(str.substr(0,pos)))){genitive=true}return s(formatTransform.date,mod,numPad,genitive)}function pad(aData,aPad,aDef,aRate){if(aPad==="-"){return aData}if(aPad==="_"){aPad=" "}if(typeof aDef==="undefined"){aDef=0}if(typeof aRate==="undefined"){aRate=10}if(typeof aPad==="undefined"||aPad===""){aPad=aDef}for(;aRate>parseInt(aData,10)&&aRate>1;aRate/=10){aData=aPad.toString()+aData}return aData.toString()}function toLetterCase(str,mode){str=String(str);mode=String(mode);if(mode.indexOf("#")>-1){return str.toLowerCase()}if(mode.indexOf("^")>-1){return str.toUpperCase()}return str.substr(0,1).toUpperCase()+str.substr(1)}}(strftime)}(); \ No newline at end of file