| 416 | */ |
| 417 | var MAXSECONDS = [59, 59.9, 59.99, 59.999, 59.9999]; |
| 418 | function formatTime(x, tr) { |
| 419 | var timePart = mod(x + 0.05, ONEDAY); |
| 420 | |
| 421 | var timeStr = lpad(Math.floor(timePart / ONEHOUR), 2) + ':' + |
| 422 | lpad(mod(Math.floor(timePart / ONEMIN), 60), 2); |
| 423 | |
| 424 | if(tr !== 'M') { |
| 425 | if(!isNumeric(tr)) tr = 0; // should only be 'S' |
| 426 | |
| 427 | /* |
| 428 | * this is a weird one - and shouldn't come up unless people |
| 429 | * monkey with tick0 in weird ways, but we need to do something! |
| 430 | * IN PARTICULAR we had better not display garbage (see below) |
| 431 | * for numbers we always round to the nearest increment of the |
| 432 | * precision we're showing, and this seems like the right way to |
| 433 | * handle seconds and milliseconds, as they have a decimal point |
| 434 | * and people will interpret that to mean rounding like numbers. |
| 435 | * but for larger increments we floor the value: it's always |
| 436 | * 2013 until the ball drops on the new year. We could argue about |
| 437 | * which field it is where we start rounding (should 12:08:59 |
| 438 | * round to 12:09 if we're stopping at minutes?) but for now I'll |
| 439 | * say we round seconds but floor everything else. BUT that means |
| 440 | * we need to never round up to 60 seconds, ie 23:59:60 |
| 441 | */ |
| 442 | var sec = Math.min(mod(x / ONESEC, 60), MAXSECONDS[tr]); |
| 443 | |
| 444 | var secStr = (100 + sec).toFixed(tr).slice(1); |
| 445 | if(tr > 0) { |
| 446 | secStr = secStr.replace(/0+$/, '').replace(/[\.]$/, ''); |
| 447 | } |
| 448 | |
| 449 | timeStr += ':' + secStr; |
| 450 | } |
| 451 | return timeStr; |
| 452 | } |
| 453 | |
| 454 | /* |
| 455 | * formatDate: turn a date into tick or hover label text. |