Permalink
Browse files

ngx like time parse

  • Loading branch information...
1 parent 06c114c commit 131b3796df2b29b8eece5f16a9464da6a3af3494 @kindy committed Jul 20, 2012
Showing with 71 additions and 0 deletions.
  1. +22 −0 htmls/misc/2012-07/parse-time.lua
  2. +49 −0 htmls/misc/2012-07/parse-time.lua.html
@@ -0,0 +1,22 @@
+-- nginx like time parse
+local _tmap = {
+ y = 60 * 60 * 24 * 365,
+ M = 60 * 60 * 24 * 30,
+ w = 60 * 60 * 24 * 7,
+ d = 60 * 60 * 24,
+ h = 60 * 60,
+ m = 60,
+ s = 1,
+}
+function parse_time(s)
+ local t = 0
+ for n, u in string.gmatch(s, "(%d+)([yMwdhms])") do
+ local x = tonumber(n) * assert(_tmap[u])
+ t = t + x
+ end
+
+ return t
+end
+
+assert(parse_time("1h 30m") == 1 * 3600 + 30 * 60, "test 1")
+assert(parse_time("2y 1h 30m") == 2 * 86400 * 365 + 1 * 3600 + 30 * 60, "test 2")
@@ -0,0 +1,49 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+<html>
+<head>
+<meta http-equiv="content-type" content="text/html; charset=UTF-8">
+<title>~/h/github/kindy/ngx.so/htmls/misc/2012-07/parse-time.lua.html</title>
+<meta name="Generator" content="Vim/7.3">
+<meta name="plugin-version" content="vim7.3_v6">
+<meta name="syntax" content="lua">
+<meta name="settings" content="use_css,number_lines">
+<style type="text/css">
+<!--
+pre { font-family: monospace; color: #ffffff; background-color: #000000; }
+body { font-family: monospace; color: #ffffff; background-color: #000000; }
+.lnr { color: #00ff00; background-color: #000000; }
+.Identifier { color: #008080; font-weight: bold; }
+.Constant { color: #00ffff; }
+.Type { color: #008000; }
+.Statement { color: #804000; font-weight: bold; }
+.Comment { color: #a8a8a8; background-color: #0000c0; }
+-->
+</style>
+</head>
+<body>
+<pre>
+<span class="lnr"> 1 </span><span class="Comment">-- nginx like time parse</span>
+<span class="lnr"> 2 </span><span class="Statement">local</span> _tmap = <span class="Type">{</span>
+<span class="lnr"> 3 </span> y = <span class="Constant">60</span> * <span class="Constant">60</span> * <span class="Constant">24</span> * <span class="Constant">365</span>,
+<span class="lnr"> 4 </span> M = <span class="Constant">60</span> * <span class="Constant">60</span> * <span class="Constant">24</span> * <span class="Constant">30</span>,
+<span class="lnr"> 5 </span> w = <span class="Constant">60</span> * <span class="Constant">60</span> * <span class="Constant">24</span> * <span class="Constant">7</span>,
+<span class="lnr"> 6 </span> d = <span class="Constant">60</span> * <span class="Constant">60</span> * <span class="Constant">24</span>,
+<span class="lnr"> 7 </span> h = <span class="Constant">60</span> * <span class="Constant">60</span>,
+<span class="lnr"> 8 </span> m = <span class="Constant">60</span>,
+<span class="lnr"> 9 </span> s = <span class="Constant">1</span>,
+<span class="lnr">10 </span><span class="Type">}</span>
+<span class="lnr">11 </span><span class="Identifier">function</span> parse_time(s)
+<span class="lnr">12 </span> <span class="Statement">local</span> t = <span class="Constant">0</span>
+<span class="lnr">13 </span> <span class="Statement">for</span> n, u <span class="Statement">in</span> <span class="Identifier">string.gmatch</span>(s, <span class="Constant">&quot;(%d+)([yMwdhms])&quot;</span>) <span class="Statement">do</span>
+<span class="lnr">14 </span> <span class="Statement">local</span> x = <span class="Identifier">tonumber</span>(n) * <span class="Identifier">assert</span>(_tmap[u])
+<span class="lnr">15 </span> t = t + x
+<span class="lnr">16 </span> <span class="Statement">end</span>
+<span class="lnr">17 </span>
+<span class="lnr">18 </span> <span class="Statement">return</span> t
+<span class="lnr">19 </span><span class="Identifier">end</span>
+<span class="lnr">20 </span>
+<span class="lnr">21 </span><span class="Identifier">assert</span>(parse_time(<span class="Constant">&quot;1h 30m&quot;</span>) == <span class="Constant">1</span> * <span class="Constant">3600</span> + <span class="Constant">30</span> * <span class="Constant">60</span>, <span class="Constant">&quot;test 1&quot;</span>)
+<span class="lnr">22 </span><span class="Identifier">assert</span>(parse_time(<span class="Constant">&quot;2y 1h 30m&quot;</span>) == <span class="Constant">2</span> * <span class="Constant">86400</span> * <span class="Constant">365</span> + <span class="Constant">1</span> * <span class="Constant">3600</span> + <span class="Constant">30</span> * <span class="Constant">60</span>, <span class="Constant">&quot;test 2&quot;</span>)
+</pre>
+</body>
+</html>

0 comments on commit 131b379

Please sign in to comment.