rewrite Ox.formatDuration()

This commit is contained in:
rolux 2011-10-10 09:32:26 +02:00
parent efca832c76
commit a39f76863f
2 changed files with 58 additions and 39 deletions

View file

@ -4,15 +4,8 @@
<title>ox.js form demo</title> <title>ox.js form demo</title>
<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="css/form.css"/> <link rel="stylesheet" type="text/css" href="css/form.css"/>
<script type="text/javascript" src="../../build/Ox.js"></script> <script type="text/javascript" src="../../dev/Ox.js"></script>
<script type="text/javascript" src="js/form.js"></script> <script type="text/javascript" src="js/form.js"></script>
<!--<script>
$(function() {
new Ox.Button({title: "close", type: "image"}).appendTo($("body"));
new Ox.Button({title: "remove", type: "image"}).appendTo($("body"));
new Ox.Button({title: "add", type: "image"}).appendTo($("body"));
});
</script>-->
</head> </head>
<body></body> <body></body>
</html> </html>

View file

@ -358,6 +358,8 @@ Ox.formatDateRangeDuration <f> Formats the duration of a date range as a string
A date range is a pair of arbitrary-presicion date strings A date range is a pair of arbitrary-presicion date strings
> Ox.formatDateRangeDuration('2000-01-01 00:00:00', '2001-03-04 04:05:06') > Ox.formatDateRangeDuration('2000-01-01 00:00:00', '2001-03-04 04:05:06')
'1 year 2 months 3 days 4 hours 5 minutes 6 seconds' '1 year 2 months 3 days 4 hours 5 minutes 6 seconds'
> Ox.formatDateRangeDuration('2000', '2001-01-01 00:00:01')
'1 year 1 second'
> Ox.formatDateRangeDuration('1999', '2000', true) > Ox.formatDateRangeDuration('1999', '2000', true)
'1 year' '1 year'
> Ox.formatDateRangeDuration('2000', '2001', true) > Ox.formatDateRangeDuration('2000', '2001', true)
@ -402,49 +404,73 @@ Ox.formatDateRangeDuration = function(start, end, utc) {
/*@ /*@
Ox.formatDuration <f> Formats a duration as a string Ox.formatDuration <f> Formats a duration as a string
> Ox.formatDuration(123456.789, 3)
"1:10:17:36.789"
> Ox.formatDuration(12345.6789)
"03:25:46"
> Ox.formatDuration(12345.6789, true)
"0:03:25:46"
> Ox.formatDuration(3599.999, 3)
"00:59:59.999"
> Ox.formatDuration(3599.999) > Ox.formatDuration(3599.999)
"01:00:00" '01:00:00'
> Ox.formatDuration(3599.999, 2)
'01:00:00.00'
> Ox.formatDuration(3599.999, 3)
'00:59:59.999'
> Ox.formatDuration(3599.999, 'short')
'1h'
> Ox.formatDuration(3599.999, 3, 'short')
'59m 59.999s'
> Ox.formatDuration(3599.999, 'long')
'1 hour'
> Ox.formatDuration(3599.999, 3, 'long')
'59 minutes 59.999 seconds'
> Ox.formatDuration(86520, 2)
'1:00:02:00.00'
> Ox.formatDuration(86520, 'long')
'1 day 2 minutes'
> Ox.formatDuration(31543203, 2)
'1:000:02:00:03.00'
> Ox.formatDuration(31543203, 'long')
'1 year 2 hours 3 seconds'
> Ox.formatDuration(0, 2)
'00:00:00.00'
> Ox.formatDuration(0, 'long')
''
@*/ @*/
Ox.formatDuration = function(sec, dec, format) { Ox.formatDuration = function(/*sec, dec, format*/) {
var format = arguments.length == 3 ? format : (Ox.isString(dec) ? dec : "short"), var format = Ox.isString(arguments[arguments.length - 1])
dec = (arguments.length == 3 || Ox.isNumber(dec)) ? dec : 0, ? arguments[arguments.length - 1] : '',
sec = dec ? sec : Math.round(sec), dec = Ox.isNumber(arguments[1]) ? arguments[1] : 0,
sec = Ox.round(arguments[0], dec),
val = [ val = [
Math.floor(sec / 31536000), Math.floor(sec / 31536000),
Math.floor(sec % 31536000 / 86400), Math.floor(sec % 31536000 / 86400),
Math.floor(sec % 86400 / 3600), Math.floor(sec % 86400 / 3600),
Math.floor(sec % 3600 / 60), Math.floor(sec % 3600 / 60),
format == "short" ? Ox.formatNumber(sec % 60, dec) : sec % 60 Ox.formatNumber(sec % 60, dec)
], ],
str = { str = !format ? []
medium: ["y", "d", "h", "m", "s"], : format == 'short' ? ['y', 'd', 'h', 'm', 's']
long: ["year", "day", "hour", "minute", "second"] : ['year', 'day', 'hour', 'minute', 'second'],
}, pad = [
pad = [0, 3, 2, 2, dec ? dec + 3 : 2]; val[0].toString().length,
while (!val[0] && val.length > (format == "short" ? 3 : 1)) { val[0] ? 3 : 1,
2,
2,
dec ? dec + 3 : 2
];
Ox.print('val', val)
while (!val[0] && val.length > (!format ? 3 : 1)) {
val.shift(); val.shift();
str.medium.shift(); str.shift();
str.long.shift();
pad.shift(); pad.shift();
} }
while (format != "short" && !val[val.length - 1] && val.length > 1) {
val.pop();
str.medium.pop();
str.long.pop();
}
return Ox.map(val, function(v, i) { return Ox.map(val, function(v, i) {
return format == "short" ? Ox.pad(v, pad[i]) : var ret;
v + (format == "long" ? " " : "") + str[format][i] + if (!format) {
(format == "long" && v != 1 ? "s" : ""); ret = Ox.pad(v, pad[i]);
}).join(format == "short" ? ":" : " "); } else if (Ox.isNumber(v) ? v : parseFloat(v)) {
ret = v + (format == 'long' ? ' ' : '') + str[i]
+ (format == 'long' && v != 1 ? 's' : '');
} else {
ret = null;
}
return ret;
}).join(!format ? ':' : ' ');
}; };
/*@ /*@