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>
9 <div class=
"content header">
10 <h1>Time calculator
</h1>
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>
18 <div id=
"empty_footer"></div>
21 © 2009 Vynogradov
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
;
33 x
= x
.replace(shorts
[i
][0], shorts
[i
][1]);
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
];
56 else if(!!(match
= obj
.match(/(\d+)\s+(day|month|year|week)s/g)))
59 dat
= new Span(match
);
63 console
.log(obj
, dat
);
67 function calculate(left
, op
, right
)
71 function evaluate(expr
)
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
);