1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * time value
 29  * @Class
 30  * @extends cc.Class
 31  */
 32 cc.timeval = cc.Class.extend(/** @lends cc.timeval# */{
 33     /**
 34      * seconds
 35      * @type Number
 36      */
 37     tv_sec:0,
 38     /**
 39      * microseconds
 40      * @type Number
 41      */
 42     tv_usec:0//
 43 });
 44 
 45 /**
 46  * @namespace
 47  */
 48 cc.Time = {};
 49 
 50 /**
 51  * get time of day
 52  * @return {cc.timeval}
 53  */
 54 cc.Time.gettimeofdayCocos2d = function (timeValue) {
 55     var timeval = timeValue || new cc.timeval();
 56     var tmp = Date.now();
 57     timeval.tv_usec = (tmp % 1000) * 1000;
 58     timeval.tv_sec = Math.floor(tmp / 1000);
 59     return timeval;
 60 };
 61 
 62 /**
 63  * get system date (alias to Date.now())
 64  * @return {Date}
 65  */
 66 cc.Time.now = function (){
 67     return Date.now();
 68 };
 69 
 70 /**
 71  * timer sub
 72  * @param {cc.timeval | Number} start start value
 73  * @param {cc.timeval | Number} end end value
 74  * @return {cc.timeval | Number}
 75  */
 76 cc.Time.timersubCocos2d = function (start, end) {
 77     if (!out || !start || !end) {
 78         return -1;
 79     }
 80     if (start instanceof cc.timeval && end instanceof cc.timeval) {
 81         var out = new cc.timeval();
 82         out.tv_sec = end.tv_sec - start.tv_sec;
 83         out.tv_usec = end.tv_usec - start.tv_usec;
 84         if (end.tv_usec < start.tv_usec) {
 85             out.tv_usec += 1000000;
 86             out.tv_sec--;
 87         }
 88         return out;
 89     }
 90     else if (!isNaN(start) && !isNaN(end)) {
 91         return end - start;
 92     }
 93 };
 94