Rubik's cube 5x5x5 edgeswap added.
[zzandy.git] / time.html
blob678c934262930d604b530ec1468e600bd8cf2ef2
1 <html>
2 <head>
3 <title>Time calculator</title>
4 <link rel="stylesheet" href="style/pixelless.css" />
5 <script type="text/javascript" src="unit-expressions/script/expression.js"></script>
6 </head>
7 <body>
8 <div id="container">
9 <div class="content header">
10 <h1>Time calculator</h1>
11 </div>
12 <div class="content">
13 <p><pre>today - 2 weeks</pre></p>
14 <p><pre>2 weeks ago</pre></p>
15 <p><pre>3 dec 2009 + 11 months 2 days</pre></p>
16 <p><pre>2 feb 1984 + 365 days * 25</pre></p>
17 </div>
18 <div id="empty_footer"></div>
19 </div>
20 <div id="footer">
21 &copy; 2009 Vynogradov
22 </div>
24 <script type="text/javascript">
25 function ripExamples(ex)
27 var shorts = [['yesterday', '1 day ago']];
28 function rectifyAgo(x)
30 var i=-1, n=shorts.length;
31 while(++i<n)
33 x = x.replace(shorts[i][0], shorts[i][1]);
36 return x
37 .replace(/(\d+)\s+(weeks|days|months|years)\s+ago/, 'today - $1 $2')
38 .replace(/(a\s+)?(day|week|month|year)\s+ago/, "today - 1 $2")
41 return new Expression(rectifyAgo(ex.innerHTML));
44 function Span(num, period)
46 // TODO: Save all durations separately in order to be able to
47 // +properly calculate months or year shifts.
48 this.len = num * len[period];
51 function parse(obj)
53 var dat, match;
54 if(obj == 'today')
55 dat = new Date();
56 else if(!!(match = obj.match(/(\d+)\s+(day|month|year|week)s/g)))
58 console.log(match);
59 dat = new Span(match);
61 else
62 dat = new Date(obj);
63 console.log(obj, dat);
64 return dat;
67 function calculate(left, op, right)
71 function evaluate(expr)
73 var left = expr.left;
74 var right = expr.right;
76 if(left instanceof Expression) left = evaluate(left);
77 if(right instanceof Expression) right = evaluate(right);
79 if(typeof left == 'string') left = parse(left);
80 if(typeof right == 'string') right = parse(right);
82 return calculate(left, expr.op, right);
85 var examples = [].map.call(document.getElementsByTagName('pre'), ripExamples);
86 console.log(examples);
87 var values = examples.map(evaluate);
88 </script>
89 </body>
90 </html>