模板:Duration/doc
Format a time value, input in seconds.
Usage
{{ duration | <time in seconds/ticks> | <maximum unit> | round = <rounding level> | raw<seconds/minutes/hours/days/weeks> = <non-numerical time> }}
- First unnamed parameter
Number, any valid {{#expr:}}
string is accepted. By default interpreted as time in seconds, unless the number is followed by any of the strings t
/tick
/ticks
, in which case the value is interpreted as time in ticks.[1]
- Second unnamed parameter
Greatest unit of the output. Can be any of seconds
, minutes
(default), hours
, days
, and weeks
, or s
, m
, h
, d
, and w
for short, respectively.
- round
Rounding level for decimal seconds, 2
by default.
- rawseconds, rawminutes, rawhours, rawdays, rawweeks
One of these parameters can be used to prevent any calculations and formatting (hence "raw") and just display the given time and unit. This is useful if the input time is not purely numerical (e.g. not a valid {{#expr:}}
string) and would thus normally result in an error, e.g. a time range like "1–3 seconds". Only one out of these five parameters can be used. All other parameters are ignored if one of these is used.
Notes
- The template does not support specifying a lowest unit of the output. Instead, this can easily be achieved by rounding the number of seconds divided by the number of seconds in the target unit, then multiplying that value by the target unit again. For example, to display
3661
(1 小時 1 分鐘 1 秒) in only hours and minutes, use(3661 / 60 round 0) * 60
. To round down or up, usefloor()
orceil()
instead ofround 0
, respectively. To set the lowest unit to hours, use3600
instead of60
, and86400
for days. See below for examples.
Examples
Code | Result |
---|---|
{{duration|41}} |
41 秒 FAIL! (expected: 41 seconds ) |
{{duration|40.75}} |
40.75 秒 FAIL! (expected: 40.75 seconds ) |
{{duration|90}} |
1 分鐘 30 秒 FAIL! (expected: 1 minute 30 seconds ) |
{{duration|1800}} |
30 分鐘 FAIL! (expected: 30 minutes ) |
{{duration|7201}} |
120 分鐘 1 秒 FAIL! (expected: 120 minutes 1 second ) |
{{duration|604800}} |
10,080 分鐘 FAIL! (expected: 10,080 minutes ) |
With unit | |
{{duration|604800|weeks}} |
1 星期 FAIL! (expected: 1 week ) |
{{duration|7201|weeks}} |
2 小時 1 秒 FAIL! (expected: 2 hours 1 second ) |
{{duration|604800|d}} |
7 天 FAIL! (expected: 7 days ) |
{{duration|7201|w}} |
2 小時 1 秒 FAIL! (expected: 2 hours 1 second ) |
Various inputs | |
{{duration|2/3}} |
0.67 秒 FAIL! (expected: 0.67 seconds ) |
{{duration|2/3|round=6}} |
0.666667 秒 FAIL! (expected: 0.666667 seconds ) |
{{duration|(35/11 mod 2) - (10^-2 / pi*3) + ceil(21/6)}} |
4.99 秒 FAIL! (expected: 4.99 seconds ) |
{{duration|{{#time:U|+3 days 2 hours 44 minutes 23 seconds}}-{{#time:U}}|days}} |
3 天 2 小時 44 分鐘 24 秒 |
{{duration|{{#time:U|+2 days 37 hours 98 minutes 63 seconds}}-{{#time:U}}|hours}} |
86 小時 39 分鐘 4 秒 |
{{duration|-41}} |
−41 秒 FAIL! (expected: −41 seconds ) |
{{duration|-3601|days}} |
−1 小時 1 秒 FAIL! (expected: −1 hour 1 second ) |
Near-zero behavior | |
{{duration|0}} |
0 秒 FAIL! (expected: 0 seconds ) |
{{duration|0.001}} |
0 秒 FAIL! (expected: 0 seconds ) |
{{duration|-0.001}} |
−0 秒 FAIL! (expected: −0 seconds ) |
{{duration|0.001|round=5}} |
0.001 秒 FAIL! (expected: 0.001 seconds ) |
Tick[1] input | |
{{duration|60t}} |
1 秒 FAIL! (expected: 1 second ) |
{{duration|20tick}} |
0.33 秒 FAIL! (expected: 0.33 seconds ) |
{{duration| 30 Ticks }} |
0.5 秒 FAIL! (expected: 0.5 seconds ) |
{{duration|140 ticks|round=4}} |
2.3333 秒 FAIL! (expected: 2.3333 seconds ) |
Lowest output unit | |
{{duration|694921|days}} |
8 天 1 小時 2 分鐘 1 秒 FAIL! (expected: 8 days 1 hour 2 minutes 1 second ) |
{{duration|(694921 / 3600 round 0) * 3600|days}} |
8 天 1 小時 FAIL! (expected: 8 days 1 hour ) |
{{duration|ceil(694921 / 3600) * 3600|days}} |
8 天 2 小時 FAIL! (expected: 8 days 2 hours ) |
{{duration|(694921 / 86400 round 0) * 86400|days}} |
8 天 FAIL! (expected: 8 days ) |
"Raw" input | |
{{duration|rawseconds=2.5–6.5}} |
2.5–6.5 秒 FAIL! (expected: 2.5–6.5 seconds ) |
{{duration|rawminutes=0}} |
0 分鐘 FAIL! (expected: 0 minutes ) |
{{duration|rawseconds=0.5–1}} |
0.5–1 秒 FAIL! (expected: 0.5–1 second ) |
{{duration|rawhours=12–24}} |
12–24 小時 FAIL! (expected: 12–24 hours ) |