2de0cf2e13c8eaf95e093615dd65bd80ec5b62e1
[platform/framework/web/crosswalk-tizen.git] /
1 define(function () {
2
3     /**
4      * convert time into another unit
5      */
6     function convert(val, sourceUnitName, destinationUnitName){
7         destinationUnitName = destinationUnitName || 'ms';
8         return (val * getUnit(sourceUnitName)) / getUnit(destinationUnitName);
9     }
10
11
12     //TODO: maybe extract to a separate module
13     function getUnit(unitName){
14         switch(unitName){
15             case 'ms':
16             case 'millisecond':
17                 return 1;
18             case 's':
19             case 'second':
20                  return 1000;
21             case 'm':
22             case 'minute':
23                  return 60000;
24             case 'h':
25             case 'hour':
26                  return 3600000;
27             case 'd':
28             case 'day':
29                  return 86400000;
30             case 'w':
31             case 'week':
32                  return 604800000;
33             default:
34                 throw new Error('"'+ unitName + '" is not a valid unit');
35         }
36     }
37
38
39     return convert;
40
41 });