misc fixes

This commit is contained in:
rolux 2011-04-23 02:20:24 +02:00
parent 0024af978c
commit b31339594d
3 changed files with 80 additions and 109 deletions

View file

@ -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") >>> Ox.formatDate(Ox.getDateInWeek(new Date("January 1 2000"), "Sunday"), "%A, %B %e, %Y")
"Sunday, January 2, 2000" "Sunday, January 2, 2000"
@ -912,12 +914,14 @@ Ox.getDateInWeek = function(date, weekday) {
"Monday, December 27, 1999" "Monday, December 27, 1999"
*/ */
var date = date || new Date(), var date = date || new Date(),
sourceWeekday = Ox.formatDate(date, "%u"); sourceWeekday = Ox.formatDate(date, '%u', utc);
targetWeekday = Ox.isNumber(weekday) ? weekday : targetWeekday = Ox.isNumber(weekday) ? weekday :
Ox.map(Ox.WEEKDAYS, function(v, i) { Ox.map(Ox.WEEKDAYS, function(v, i) {
return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null; return v.substr(0, 3) == weekday.substr(0, 3) ? i + 1 : null;
})[0]; })[0];
date.setDate(date.getDate() - sourceWeekday + targetWeekday); date[utc ? 'setUTCDate' : 'setDate'](
date[utc ? 'getUTCDate' : 'getDate']() - sourceWeekday + targetWeekday
);
return date; return date;
} }
@ -932,23 +936,11 @@ Ox.getDayOfTheYear = function(date) {
*/ */
return function(date) { return function(date) {
date = date || new Date(); date = date || new Date();
var month = date.getMonth(), var month = date[utc ? 'getUTCMonth' : 'getMonth'](),
year = date.getFullYear(); year = date[utc ? 'getUTCFullYear' : 'getFullYear']();
return Ox.sum(Ox.map(Ox.range(month), function(i) { return Ox.sum(Ox.map(Ox.range(month), function(i) {
return Ox.getDaysInMonth(year, i + 1); return Ox.getDaysInMonth(year, i + 1);
})) + date.getDate(); })) + 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); 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 See http://developer.apple.com/documentation/Darwin/Reference/ManPages/man3/strftime.3.html
and http://en.wikipedia.org/wiki/ISO_8601 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)" "Sun Jan 02 2005 00:03:04 GMT+0100 (CET)"
>>> Ox.formatDate(_date, "%A") // Full weekday >>> Ox.formatDate(_date, "%A") // Full weekday
"Sunday" "Sunday"
@ -1619,95 +1613,67 @@ Ox.formatDate = function() {
"01" "01"
*/ */
/*
var date, fn, format, utc
Array.prototype.slice.call(arguments).forEach(function(arg) {
});
var fn = {}, format; var fn = {}, format;
[ [
'getFullYear', 'getMonth', 'getDate', 'getFullYear', 'getMonth', 'getDate', 'getDay',
'getHours', 'getMinutes', 'getSeconds', 'toString' 'getHours', 'getMinutes', 'getSeconds'
].forEach(function(f) { ].forEach(function(v) {
fn[v] = utc ? v.replace('get', 'getUTC') : v;
}); });
*/
var format = [ format = [
["%", function() {return "%{%}";}], ['%', function() {return '%{%}';}],
["c", function() {return "%x %X";}], ['c', function() {return '%x %X';}],
["X", function() {return "%r";}], ['X', function() {return '%r';}],
["x", function() {return "%D";}], ['x', function() {return '%D';}],
["D", function() {return "%m/%d/%y";}], ['D', function() {return '%m/%d/%y';}],
["F", function() {return "%Y-%m-%d";}], ['F', function() {return '%Y-%m-%d';}],
["h", function() {return "%b";}], ['h', function() {return '%b';}],
["R", function() {return "%H:%M";}], ['R', function() {return '%H:%M';}],
["r", function() {return "%I:%M:%S %p";}], ['r', function() {return '%I:%M:%S %p';}],
["T", function() {return "%H:%M:%S";}], ['T', function() {return '%H:%M:%S';}],
["v", function() {return "%e-%b-%Y";}], ['v', function() {return '%e-%b-%Y';}],
["\\+", function() {return "%a %b %e %H:%M:%S %Z %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.WEEKDAYS[(d[fn.getDay]() + 6) % 7];}],
["a", function(d) {return Ox.SHORT_WEEKDAYS[(d.getDay() + 6) % 7];}], ['a', function(d) {return Ox.SHORT_WEEKDAYS[(d[fn.getDay]() + 6) % 7];}],
["B", function(d) {return Ox.MONTHS[d.getMonth()];}], ['B', function(d) {return Ox.MONTHS[d[fn.getMonth]()];}],
["b", function(d) {return Ox.SHORT_MONTHS[d.getMonth()];}], ['b', function(d) {return Ox.SHORT_MONTHS[d[fn.getMonth]()];}],
["C", function(d) {return (d.getFullYear() / 100).toString();}], ['C', function(d) {return Math.floor(d[fn.getFullYear]() / 100).toString();}],
["d", function(d) {return Ox.pad(d.getDate(), 2);}], ['d', function(d) {return Ox.pad(d[fn.getDate](), 2);}],
["e", function(d) {return Ox.pad(d.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);}],
["g", function(d) {return Ox.getISOYear(d).toString().substr(-2);}], ['g', function(d) {return Ox.getISOYear(d).toString().substr(-2);}],
["H", function(d) {return Ox.pad(d.getHours(), 2);}], ['H', function(d) {return Ox.pad(d[fn.getHours](), 2);}],
["I", function(d) {return Ox.pad((d.getHours() + 11) % 12 + 1, 2);}], ['I', function(d) {return Ox.pad((d[fn.getHours]() + 11) % 12 + 1, 2);}],
["j", function(d) {return Ox.pad(Ox.getDayOfTheYear(d), 3);}], ['j', function(d) {return Ox.pad(Ox.getDayOfTheYear(d), 3);}],
["k", function(d) {return Ox.pad(d.getHours(), 2, " ");}], ['k', function(d) {return Ox.pad(d[fn.getHours](), 2, ' ');}],
["l", function(d) {return Ox.pad(((d.getHours() + 11) % 12 + 1), 2, " ");}], ['l', function(d) {return Ox.pad(((d[fn.getHours]() + 11) % 12 + 1), 2, ' ');}],
["M", function(d) {return Ox.pad(d.getMinutes(), 2);}], ['M', function(d) {return Ox.pad(d[fn.getMinutes](), 2);}],
["m", function(d) {return Ox.pad((d.getMonth() + 1), 2);}], ['m', function(d) {return Ox.pad((d[fn.getMonth]() + 1), 2);}],
["p", function(d) {return Ox.AMPM[Math.floor(d.getHours() / 12)];}], ['p', function(d) {return Ox.AMPM[Math.floor(d[fn.getHours]() / 12)];}],
["Q", function(d) {return Math.floor(d.getMonth() / 4) + 1;}], ['Q', function(d) {return Math.floor(d[fn.getMonth]() / 4) + 1;}],
["S", function(d) {return Ox.pad(d.getSeconds(), 2);}], ['S', function(d) {return Ox.pad(d[fn.getSeconds](), 2);}],
["s", function(d) {return Math.floor(d.getTime() / 1000);}], ['s', function(d) {return Math.floor(d.getTime() / 1000);}],
["U", function(d) {return Ox.pad(Ox.getWeek(d), 2);}], ['U', function(d) {return Ox.pad(Ox.getWeek(d), 2);}],
["u", function(d) {return Ox.getISODay(d);}], ['u', function(d) {return Ox.getISODay(d);}],
["V", function(d) {return Ox.pad(Ox.getISOWeek(d), 2);}], ['V', function(d) {return Ox.pad(Ox.getISOWeek(d), 2);}],
["W", function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d) + ['W', function(d) {return Ox.pad(Math.floor((Ox.getDayOfTheYear(d) +
(Ox.getFirstDayOfTheYear(d) || 7) - 2) / 7), 2);}], (Ox.getFirstDayOfTheYear(d) || 7) - 2) / 7), 2);}],
["w", function(d) {return d.getDay();}], ['w', function(d) {return d[fn.getDay]();}],
["Y", function(d) {return d.getFullYear();}], ['Y', function(d) {return d[fn.getFullYear]();}],
["y", function(d) {return d.getFullYear().toString().substr(-2);}], ['y', function(d) {return d[fn.getFullYear]().toString().substr(-2);}],
["Z", function(d) {return d.toString().split("(")[1].replace(")", "");}], ['Z', function(d) {return d.toString().split('(')[1].replace(')', '');}],
["z", function(d) {return Ox.getTimezoneOffsetString(d);}], ['z', function(d) {return Ox.getTimezoneOffsetString(d);}],
["n", function() {return "\n";}], ['n', function() {return '\n';}],
["t", function() {return "\t";}], ['t', function() {return '\t';}],
["\\{%\\}", function() {return "%";}] ['\\{%\\}', function() {return '%';}]
]; ];
format.forEach(function(v, i) { format.forEach(function(v) {
v[0] = new RegExp('%' + v[0] + '', 'g'); 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) { Ox.formatDuration = function(sec, dec, format) {
/* /*

View file

@ -5,10 +5,11 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="../build/css/ox.ui.css"/> <link rel="stylesheet" type="text/css" href="../build/css/ox.ui.css"/>
<link rel="stylesheet" type="text/css" href="tests.css"/> <link rel="stylesheet" type="text/css" href="tests.css"/>
<script type="text/javascript" src="../build/js/jquery-1.5.js"></script> <script type="text/javascript" src="../build/js/jquery.js"></script>
<script type="text/javascript" src="../build/js/ox.js"></script> <!--<script type="text/javascript" src="../build/js/Ox.js"></script>-->
<script type="text/javascript" src="../build/js/ox.data.js"></script> <script type="text/javascript" src="../build/js/OxUI.js"></script>
<script type="text/javascript" src="../build/js/ox.ui.js"></script> <!--<script type="text/javascript" src="../build/js/ox.data.js"></script>-->
<!--<script type="text/javascript" src="../build/js/ox.ui.js"></script>-->
<script type="text/javascript" src="tests.js"></script> <script type="text/javascript" src="tests.js"></script>
</head> </head>
<body> <body>

View file

@ -1,4 +1,6 @@
$(function() { Ox.UI(function() {
//Ox.UI.ready(function() {
var $body = $('body') var $body = $('body')
.css({ .css({
@ -25,7 +27,7 @@ $(function() {
setBackground($tests, true); 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() { function tests() {
var succeeded = 0, failed = 0, var succeeded = 0, failed = 0,
@ -117,6 +119,8 @@ $(function() {
}); });
} }
//});
function setBackground($element, success) { function setBackground($element, success) {
$.each(gradients, function(i, v) { $.each(gradients, function(i, v) {
$element.css({ $element.css({