From b31339594d45788cc055d4a78463a16cfcca4f5c Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 23 Apr 2011 02:20:24 +0200 Subject: [PATCH] misc fixes --- source/js/Ox.js | 172 +++++++++++++++++++---------------------------- tests/index.html | 9 +-- tests/tests.js | 8 ++- 3 files changed, 80 insertions(+), 109 deletions(-) diff --git a/source/js/Ox.js b/source/js/Ox.js index d2c1f643..82aeeca3 100644 --- a/source/js/Ox.js +++ b/source/js/Ox.js @@ -902,7 +902,9 @@ Date functions ================================================================================ */ -Ox.getDateInWeek = function(date, weekday) { +// fixme: support UTC, only halfway done + +Ox.getDateInWeek = function(date, weekday, utc) { /* >>> Ox.formatDate(Ox.getDateInWeek(new Date("January 1 2000"), "Sunday"), "%A, %B %e, %Y") "Sunday, January 2, 2000" @@ -912,12 +914,14 @@ Ox.getDateInWeek = function(date, weekday) { "Monday, December 27, 1999" */ var date = date || new Date(), - sourceWeekday = Ox.formatDate(date, "%u"); + sourceWeekday = Ox.formatDate(date, '%u', utc); targetWeekday = Ox.isNumber(weekday) ? weekday : Ox.map(Ox.WEEKDAYS, function(v, i) { return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null; })[0]; - date.setDate(date.getDate() - sourceWeekday + targetWeekday); + date[utc ? 'setUTCDate' : 'setDate']( + date[utc ? 'getUTCDate' : 'getDate']() - sourceWeekday + targetWeekday + ); return date; } @@ -932,23 +936,11 @@ Ox.getDayOfTheYear = function(date) { */ return function(date) { date = date || new Date(); - var month = date.getMonth(), - year = date.getFullYear(); + var month = date[utc ? 'getUTCMonth' : 'getMonth'](), + year = date[utc ? 'getUTCFullYear' : 'getFullYear'](); return Ox.sum(Ox.map(Ox.range(month), function(i) { return Ox.getDaysInMonth(year, i + 1); })) + date.getDate(); - /* - var day = date.getDate(), - month = date.getMonth(); - i; - for (i = 0; i < month; i++) { - day += Ox.DAYS[i]; - } - if (month >= 2 && Ox.isLeapYear(date.getFullYear())) { - day++; - } - return day; - */ }; }(); @@ -1517,11 +1509,13 @@ Ox.formatCurrency = function(num, str, dec) { return str + Ox.formatNumber(num, dec); }; -Ox.formatDate = function() { +Ox.formatDate = function(date, str, utc) { + // fixme: date and utc are optional, date can be date, number or string + /* See http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/strftime.3.html and http://en.wikipedia.org/wiki/ISO_8601 - >>> _date = new Date("01/02/05 00:03:04") + >>> _date = new Date("2005-01-02 00:03:04") "Sun Jan 02 2005 00:03:04 GMT+0100 (CET)" >>> Ox.formatDate(_date, "%A") // Full weekday "Sunday" @@ -1618,96 +1612,68 @@ Ox.formatDate = function() { >>> Ox.formatDate(new Date("01/03/2000"), "%W") "01" */ - - /* - var date, fn, format, utc - Array.prototype.slice.call(arguments).forEach(function(arg) { - - }); var fn = {}, format; + [ - 'getFullYear', 'getMonth', 'getDate', - 'getHours', 'getMinutes', 'getSeconds', 'toString' - ].forEach(function(f) { - + 'getFullYear', 'getMonth', 'getDate', 'getDay', + 'getHours', 'getMinutes', 'getSeconds' + ].forEach(function(v) { + fn[v] = utc ? v.replace('get', 'getUTC') : v; }); - */ - var format = [ - ["%", function() {return "%{%}";}], - ["c", function() {return "%x %X";}], - ["X", function() {return "%r";}], - ["x", function() {return "%D";}], - ["D", function() {return "%m/%d/%y";}], - ["F", function() {return "%Y-%m-%d";}], - ["h", function() {return "%b";}], - ["R", function() {return "%H:%M";}], - ["r", function() {return "%I:%M:%S %p";}], - ["T", function() {return "%H:%M:%S";}], - ["v", function() {return "%e-%b-%Y";}], - ["\\+", function() {return "%a %b %e %H:%M:%S %Z %Y";}], - ["A", function(d) {return Ox.WEEKDAYS[(d.getDay() + 6) % 7];}], - ["a", function(d) {return Ox.SHORT_WEEKDAYS[(d.getDay() + 6) % 7];}], - ["B", function(d) {return Ox.MONTHS[d.getMonth()];}], - ["b", function(d) {return Ox.SHORT_MONTHS[d.getMonth()];}], - ["C", function(d) {return (d.getFullYear() / 100).toString();}], - ["d", function(d) {return Ox.pad(d.getDate(), 2);}], - ["e", function(d) {return Ox.pad(d.getDate(), 2, " ");}], - ["G", function(d) {return Ox.getISOYear(d);}], - ["g", function(d) {return Ox.getISOYear(d).toString().substr(-2);}], - ["H", function(d) {return Ox.pad(d.getHours(), 2);}], - ["I", function(d) {return Ox.pad((d.getHours() + 11) % 12 + 1, 2);}], - ["j", function(d) {return Ox.pad(Ox.getDayOfTheYear(d), 3);}], - ["k", function(d) {return Ox.pad(d.getHours(), 2, " ");}], - ["l", function(d) {return Ox.pad(((d.getHours() + 11) % 12 + 1), 2, " ");}], - ["M", function(d) {return Ox.pad(d.getMinutes(), 2);}], - ["m", function(d) {return Ox.pad((d.getMonth() + 1), 2);}], - ["p", function(d) {return Ox.AMPM[Math.floor(d.getHours() / 12)];}], - ["Q", function(d) {return Math.floor(d.getMonth() / 4) + 1;}], - ["S", function(d) {return Ox.pad(d.getSeconds(), 2);}], - ["s", function(d) {return Math.floor(d.getTime() / 1000);}], - ["U", function(d) {return Ox.pad(Ox.getWeek(d), 2);}], - ["u", function(d) {return Ox.getISODay(d);}], - ["V", function(d) {return Ox.pad(Ox.getISOWeek(d), 2);}], - ["W", function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d) + - (Ox.getFirstDayOfTheYear(d) || 7) - 2) / 7), 2);}], - ["w", function(d) {return d.getDay();}], - ["Y", function(d) {return d.getFullYear();}], - ["y", function(d) {return d.getFullYear().toString().substr(-2);}], - ["Z", function(d) {return d.toString().split("(")[1].replace(")", "");}], - ["z", function(d) {return Ox.getTimezoneOffsetString(d);}], - ["n", function() {return "\n";}], - ["t", function() {return "\t";}], - ["\\{%\\}", function() {return "%";}] - ]; - format.forEach(function(v, i) { - v[0] = new RegExp('%' + v[0] + '', 'g'); + format = [ + ['%', function() {return '%{%}';}], + ['c', function() {return '%x %X';}], + ['X', function() {return '%r';}], + ['x', function() {return '%D';}], + ['D', function() {return '%m/%d/%y';}], + ['F', function() {return '%Y-%m-%d';}], + ['h', function() {return '%b';}], + ['R', function() {return '%H:%M';}], + ['r', function() {return '%I:%M:%S %p';}], + ['T', function() {return '%H:%M:%S';}], + ['v', function() {return '%e-%b-%Y';}], + ['\\+', function() {return '%a %b %e %H:%M:%S %Z %Y';}], + ['A', function(d) {return Ox.WEEKDAYS[(d[fn.getDay]() + 6) % 7];}], + ['a', function(d) {return Ox.SHORT_WEEKDAYS[(d[fn.getDay]() + 6) % 7];}], + ['B', function(d) {return Ox.MONTHS[d[fn.getMonth]()];}], + ['b', function(d) {return Ox.SHORT_MONTHS[d[fn.getMonth]()];}], + ['C', function(d) {return Math.floor(d[fn.getFullYear]() / 100).toString();}], + ['d', function(d) {return Ox.pad(d[fn.getDate](), 2);}], + ['e', function(d) {return Ox.pad(d[fn.getDate](), 2, ' ');}], + ['G', function(d) {return Ox.getISOYear(d);}], + ['g', function(d) {return Ox.getISOYear(d).toString().substr(-2);}], + ['H', function(d) {return Ox.pad(d[fn.getHours](), 2);}], + ['I', function(d) {return Ox.pad((d[fn.getHours]() + 11) % 12 + 1, 2);}], + ['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d), 3);}], + ['k', function(d) {return Ox.pad(d[fn.getHours](), 2, ' ');}], + ['l', function(d) {return Ox.pad(((d[fn.getHours]() + 11) % 12 + 1), 2, ' ');}], + ['M', function(d) {return Ox.pad(d[fn.getMinutes](), 2);}], + ['m', function(d) {return Ox.pad((d[fn.getMonth]() + 1), 2);}], + ['p', function(d) {return Ox.AMPM[Math.floor(d[fn.getHours]() / 12)];}], + ['Q', function(d) {return Math.floor(d[fn.getMonth]() / 4) + 1;}], + ['S', function(d) {return Ox.pad(d[fn.getSeconds](), 2);}], + ['s', function(d) {return Math.floor(d.getTime() / 1000);}], + ['U', function(d) {return Ox.pad(Ox.getWeek(d), 2);}], + ['u', function(d) {return Ox.getISODay(d);}], + ['V', function(d) {return Ox.pad(Ox.getISOWeek(d), 2);}], + ['W', function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d) + + (Ox.getFirstDayOfTheYear(d) || 7) - 2) / 7), 2);}], + ['w', function(d) {return d[fn.getDay]();}], + ['Y', function(d) {return d[fn.getFullYear]();}], + ['y', function(d) {return d[fn.getFullYear]().toString().substr(-2);}], + ['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}], + ['z', function(d) {return Ox.getTimezoneOffsetString(d);}], + ['n', function() {return '\n';}], + ['t', function() {return '\t';}], + ['\\{%\\}', function() {return '%';}] + ]; + format.forEach(function(v) { + str = str.replace(new RegExp('%' + v[0], 'g'), v[1](date)); }); - return function(date, str) { - str = str || date; - date = arguments.length == 2 ? date : new Date(); - var split; - if (typeof date == 'string') { - // support YYYY-MM-DD - split = date.substr(0, 10).split('-'); - if (split.length == 3) { - date = [split[1], split[2], split[0]].join('/') + date.substr(10); - } - } - if (Ox.isNumber(date) || Ox.isString(date)) { - date = new Date(date); - } - if (Ox.isDate(date) && date.toString() != 'Invalid Date') { - Ox.forEach(format, function(v) { - str = str.replace(v[0], v[1](date)); - }); - } else { - str = ''; - } - return str; - }; -}(); + return str; +}; Ox.formatDuration = function(sec, dec, format) { /* diff --git a/tests/index.html b/tests/index.html index f35bfbeb..210fb321 100644 --- a/tests/index.html +++ b/tests/index.html @@ -5,10 +5,11 @@ - - - - + + + + + diff --git a/tests/tests.js b/tests/tests.js index 417c83fb..0457efac 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -1,4 +1,6 @@ -$(function() { +Ox.UI(function() { + + //Ox.UI.ready(function() { var $body = $('body') .css({ @@ -25,7 +27,7 @@ $(function() { setBackground($tests, true); - tests(['../build/js/ox.js', '../build/js/ox.data.js']); + tests(['../build/js/ox.js'/*, '../build/js/ox.data.js'*/]); function tests() { var succeeded = 0, failed = 0, @@ -117,6 +119,8 @@ $(function() { }); } + //}); + function setBackground($element, success) { $.each(gradients, function(i, v) { $element.css({