Home | History | Annotate | Download | only in src
      1 /*---------------------------------------------------------------------------*
      2  *  ptimestamp.c  *
      3  *                                                                           *
      4  *  Copyright 2007, 2008 Nuance Communciations, Inc.                               *
      5  *                                                                           *
      6  *  Licensed under the Apache License, Version 2.0 (the 'License');          *
      7  *  you may not use this file except in compliance with the License.         *
      8  *                                                                           *
      9  *  You may obtain a copy of the License at                                  *
     10  *      http://www.apache.org/licenses/LICENSE-2.0                           *
     11  *                                                                           *
     12  *  Unless required by applicable law or agreed to in writing, software      *
     13  *  distributed under the License is distributed on an 'AS IS' BASIS,        *
     14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
     15  *  See the License for the specific language governing permissions and      *
     16  *  limitations under the License.                                           *
     17  *                                                                           *
     18  *---------------------------------------------------------------------------*/
     19 
     20 #include "ptimestamp.h"
     21 #include "pmutex.h"
     22 
     23 #ifdef _WIN32
     24 #include "sys/timeb.h"
     25 #endif
     26 
     27 void PTimeStampSet(PTimeStamp *timestamp)
     28 {
     29 
     30 #ifdef DISABLE_TIMESTAMPS
     31   timestamp->secs  = 0;
     32   timestamp->msecs = 0;
     33 #else
     34 
     35 #ifdef _WIN32
     36   struct _timeb now;
     37 
     38   _ftime(&now);
     39   timestamp->secs = now.time;
     40   timestamp->msecs = now.millitm;
     41 #elif defined(POSIX)
     42   struct timespec now;
     43   clock_gettime(CLOCK_REALTIME, &now);
     44   timestamp->secs = now.tv_sec;
     45   timestamp->msecs = now.tv_nsec / MSECOND2NSECOND;
     46 #else
     47 #error "PTimeStampSet not defined for this platform."
     48 #endif
     49 
     50 #endif
     51 }
     52 
     53 PINLINE int PTimeStampDiff(const PTimeStamp *a, const PTimeStamp *b)
     54 {
     55   return (a->secs - b->secs) * 1000 + a->msecs - b->msecs;
     56 }
     57