User:Stux/sandbox2

From Homestar Runner Wiki

(Difference between revisions)
Jump to: navigation, search
(it works! :D)
(add scrollbars to the pre tag! Also start abstracting into a template)
Line 54: Line 54:
* For now let's do more complicated examples (with hand-coded zero-padding):
* For now let's do more complicated examples (with hand-coded zero-padding):
-
<pre>
+
<pre style="white-space: pre-wrap; word-break: normal; overflow: scroll;">
{{#ifexpr: (3601>=3600)|{{#expr: (3601-(3601 mod 3600))/3600}}:|}}{{#ifexpr: (((3601-(3601 mod 60))/60) mod 60)<10|0|}}{{#expr: ((3601-(3601 mod 60))/60) mod 60}}:{{#ifexpr: (3601 mod 60)<10|0|}}{{#expr:3601 mod 60}} <br/>  
{{#ifexpr: (3601>=3600)|{{#expr: (3601-(3601 mod 3600))/3600}}:|}}{{#ifexpr: (((3601-(3601 mod 60))/60) mod 60)<10|0|}}{{#expr: ((3601-(3601 mod 60))/60) mod 60}}:{{#ifexpr: (3601 mod 60)<10|0|}}{{#expr:3601 mod 60}} <br/>  
<!-- 3601 seconds to hh:mm:ss -->
<!-- 3601 seconds to hh:mm:ss -->
Line 72: Line 72:
{{#ifexpr: (90001>=3600)|{{#expr: (90001-(90001 mod 3600))/3600}}:|}}{{#ifexpr: (((90001-(90001 mod 60))/60) mod 60<10)|0|}}{{#expr: ((90001-(90001 mod 60))/60) mod 60}}:{{#ifexpr: (90001 mod 60)<10|0|}}{{#expr:90001 mod 60}} <br/>  
{{#ifexpr: (90001>=3600)|{{#expr: (90001-(90001 mod 3600))/3600}}:|}}{{#ifexpr: (((90001-(90001 mod 60))/60) mod 60<10)|0|}}{{#expr: ((90001-(90001 mod 60))/60) mod 60}}:{{#ifexpr: (90001 mod 60)<10|0|}}{{#expr:90001 mod 60}} <br/>  
<!-- 90001 seconds to hh:mm:ss -->
<!-- 90001 seconds to hh:mm:ss -->
 +
 +
* This will become a new template:
 +
<pre style="white-space: pre-wrap; word-break: normal; overflow: scroll;">
 +
{{#ifexpr: ({{{1}}}>=3600)|{{#expr: ({{{1}}}-({{{1}}} mod 3600))/3600}}:|}}{{#ifexpr: ((({{{1}}}-({{{1}}} mod 60))/60) mod 60)<10|0|}}{{#expr: (({{{1}}}-({{{1}}} mod 60))/60) mod 60}}:{{#ifexpr: ({{{1}}} mod 60)<10|0|}}{{#expr:{{{1}}} mod 60}}
 +
</pre>
 +
* With comments:
 +
<pre style="white-space: pre-wrap; word-break: normal; overflow: scroll;">
 +
{{#ifexpr: ({{{1}}}>=3600)|{{#expr: ({{{1}}}-({{{1}}} mod 3600))/3600}}:|}}<!-- /* Calculate the hours and only display more than 0 hours. */
 +
-->{{#ifexpr: ((({{{1}}}-({{{1}}} mod 60))/60) mod 60)<10|0|}}<!-- /* Calculate the minutes and pad with leading zero if less than 10. */
 +
-->{{#expr: (({{{1}}}-({{{1}}} mod 60))/60) mod 60}}:<!-- /* Calculate the minutes. */
 +
-->{{#ifexpr: ({{{1}}} mod 60)<10|0|}}<!-- /* Calculate the seconds and pad with leading zero if less than 10. */
 +
-->{{#expr:{{{1}}} mod 60}}<!-- /* Calculate the seconds. */ -->
 +
</pre>
 +
* New template name: {{t|format seconds}}
 +
* Related templates: {{t|hcount}} and {{t|vcount}}
 +
 +
----
----

Revision as of 23:31, 1 January 2025

Testing time:

{{#time: z | 20071031}}

Yields: 303


  • 121 seconds?
{{#time: i:s | 121}}

Yields: Error: invalid time

  • So time doesn't support converting seconds to "hh:mm:ss" format, unfortunately. We gotta do it manually using Help:ParserFunctions##expr:, but now we have two more problems:
    • There is no modulo operation, and we can't use "round" to easily emulate it.
    • So, here goes nothing:
{{#expr: 121/60}} <br/><!-- convert 121 seconds to minutes -->
{{#expr: 119/60}} <br/><!-- convert 119 seconds to minutes -->
{{#expr: 121/60 round 0}} <br/><!-- round 121 seconds to minutes -->
{{#expr: 119/60 round 0}} <br/><!-- round 119 seconds to minutes -->

2.0166666666667
1.9833333333333
2
2

  • Fortunately, we should be able to use the above logic for IF statements. Unfortunately, this crazy logic may time out...
  • But wait! Turns out there is module after all:
{{#expr: 121%60}} <br/><!-- confirm there is no modulo symbol -->
{{#expr: 121 mod 60}} <br/><!-- confirm there is a modulo operator -->
{{#expr: 119 mod 60}} <br/><!-- confirm there is a modulo operator -->

Expression error: Unrecognised punctuation character "%"
1
59

  • So we can now "easily" convert seconds to hh:mm:ss:
{{#expr: (121-(121 mod 60))/60}}:{{#expr:121 mod 60}} <br/> <!-- 121 seconds to mm:ss -->
{{#expr: (119-(119 mod 60))/60}}:{{#expr:119 mod 60}} <br/> <!-- 119 seconds to mm:ss -->

2:1
1:59

  • Okay, so this doesn't do zero padding :-/


  • For now let's do more complicated examples (with hand-coded zero-padding):
{{#ifexpr: (3601>=3600)|{{#expr: (3601-(3601 mod 3600))/3600}}:|}}{{#ifexpr: (((3601-(3601 mod 60))/60) mod 60)<10|0|}}{{#expr: ((3601-(3601 mod 60))/60) mod 60}}:{{#ifexpr: (3601 mod 60)<10|0|}}{{#expr:3601 mod 60}} <br/> 
<!-- 3601 seconds to hh:mm:ss -->
{{#ifexpr: (3599>=3600)|{{#expr: (3599-(3599 mod 3600))/3600}}:|}}{{#ifexpr: (((3599-(3599 mod 60))/60) mod 60)<10|0|}}{{#expr: ((3599-(3599 mod 60))/60) mod 60}}:{{#ifexpr: (3599 mod 60)<10|0|}}{{#expr:3599 mod 60}} <br/> 
<!-- 3599 seconds to hh:mm:ss -->
{{#ifexpr: (7199>=3600)|{{#expr: (7199-(7199 mod 3600))/3600}}:|}}{{#ifexpr: (((7199-(7199 mod 60))/60) mod 60)<10|0|}}{{#expr: ((7199-(7199 mod 60))/60) mod 60}}:{{#ifexpr: (7199 mod 60)<10|0|}}{{#expr:7199 mod 60}} <br/> 
<!-- 7199 seconds to hh:mm:ss -->
{{#ifexpr: (90001>=3600)|{{#expr: (90001-(90001 mod 3600))/3600}}:|}}{{#ifexpr: (((90001-(90001 mod 60))/60) mod 60<10)|0|}}{{#expr: ((90001-(90001 mod 60))/60) mod 60}}:{{#ifexpr: (90001 mod 60)<10|0|}}{{#expr:90001 mod 60}} <br/> 
<!-- 90001 seconds to hh:mm:ss -->

1:00:01
59:59
1:59:59
25:00:01

  • This will become a new template:
{{#ifexpr: ({{{1}}}>=3600)|{{#expr: ({{{1}}}-({{{1}}} mod 3600))/3600}}:|}}{{#ifexpr: ((({{{1}}}-({{{1}}} mod 60))/60) mod 60)<10|0|}}{{#expr: (({{{1}}}-({{{1}}} mod 60))/60) mod 60}}:{{#ifexpr: ({{{1}}} mod 60)<10|0|}}{{#expr:{{{1}}} mod 60}}
  • With comments:
{{#ifexpr: ({{{1}}}>=3600)|{{#expr: ({{{1}}}-({{{1}}} mod 3600))/3600}}:|}}<!-- /* Calculate the hours and only display more than 0 hours. */
-->{{#ifexpr: ((({{{1}}}-({{{1}}} mod 60))/60) mod 60)<10|0|}}<!-- /* Calculate the minutes and pad with leading zero if less than 10. */
-->{{#expr: (({{{1}}}-({{{1}}} mod 60))/60) mod 60}}:<!-- /* Calculate the minutes. */
-->{{#ifexpr: ({{{1}}} mod 60)<10|0|}}<!-- /* Calculate the seconds and pad with leading zero if less than 10. */
-->{{#expr:{{{1}}} mod 60}}<!-- /* Calculate the seconds. */ -->




Personal tools