(file) Return to Time.cpp CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 martin 1.4 //%LICENSE////////////////////////////////////////////////////////////////
  2 martin 1.5 //
  3 martin 1.4 // Licensed to The Open Group (TOG) under one or more contributor license
  4            // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  5            // this work for additional information regarding copyright ownership.
  6            // Each contributor licenses this file to you under the OpenPegasus Open
  7            // Source License; you may not use this file except in compliance with the
  8            // License.
  9 martin 1.5 //
 10 martin 1.4 // Permission is hereby granted, free of charge, to any person obtaining a
 11            // copy of this software and associated documentation files (the "Software"),
 12            // to deal in the Software without restriction, including without limitation
 13            // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 14            // and/or sell copies of the Software, and to permit persons to whom the
 15            // Software is furnished to do so, subject to the following conditions:
 16 martin 1.5 //
 17 martin 1.4 // The above copyright notice and this permission notice shall be included
 18            // in all copies or substantial portions of the Software.
 19 martin 1.5 //
 20 martin 1.4 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21 martin 1.5 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22 martin 1.4 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23            // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 24            // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 25            // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 26            // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 27 martin 1.5 //
 28 martin 1.4 //////////////////////////////////////////////////////////////////////////
 29 mike   1.2 //
 30            //%/////////////////////////////////////////////////////////////////////////////
 31            
 32            #include "Time.h"
 33            
 34            #if defined(PEGASUS_OS_TYPE_WINDOWS)
 35            # include <windows.h>
 36            # include <sys/timeb.h>
 37            #else
 38            # define USE_GETTIMEOFDAY
 39            #endif
 40            
 41            #if defined(USE_NTP_GETTIME)
 42            # include <sys/timex.h>
 43            # include <errno.h>
 44            #endif
 45            
 46            #if defined(USE_GETTIMEOFDAY)
 47            # include <sys/time.h>
 48            # include <errno.h>
 49            #endif
 50 mike   1.2 
 51            PEGASUS_NAMESPACE_BEGIN
 52            
 53            int Time::gettimeofday(timeval* tv)
 54            {
 55            #if defined(USE_GETTIMEOFDAY)
 56            
 57 kumpf  1.3     if (tv == NULL)
 58 mike   1.2         return EINVAL;
 59            
 60                struct timeval tmp;
 61            
 62                if (::gettimeofday(&tmp, NULL) == 0)
 63                {
 64                    tv->tv_sec = tmp.tv_sec;
 65                    tv->tv_usec = tmp.tv_usec;
 66                    return 0;
 67                }
 68            
 69                return -1;
 70            
 71            #elif defined(USE_NTP_GETTIME)
 72            
 73                if (tv == NULL)
 74                    return EINVAL;
 75            
 76                struct ntptimeval ntp;
 77            
 78                if (ntp_gettime(&ntp) == 0)
 79 mike   1.2     {
 80                    tv->tv_sec = ntp.time.tv_sec;
 81                    tv->tv_usec = ntp.time.tv_usec;
 82                    return 0;
 83                }
 84            
 85                return -1;
 86            
 87            #elif defined(PEGASUS_OS_TYPE_WINDOWS)
 88            
 89                struct _timeb timebuffer;
 90            
 91                if (tv == NULL)
 92                    return -1;
 93            
 94                _ftime(&timebuffer);
 95                tv->tv_sec = long(timebuffer.time);
 96                tv->tv_usec = timebuffer.millitm * 1000;
 97            
 98                return 0;
 99            
100 mike   1.2 #endif
101            }
102            
103            int Time::subtract(timeval* result, timeval* x, timeval* y)
104            {
105 kumpf  1.3     /* Perform the carry for the later subtraction by updating Y. */
106                if (x->tv_usec < y->tv_usec)
107                {
108                    int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
109                    y->tv_usec -= 1000000 * nsec;
110                    y->tv_sec += nsec;
111                }
112                if (x->tv_usec - y->tv_usec > 1000000)
113                {
114                    int nsec = (x->tv_usec - y->tv_usec) / 1000000;
115                    y->tv_usec += 1000000 * nsec;
116                    y->tv_sec -= nsec;
117                }
118            
119                /* Compute the time remaining to wait.
120                    `tv_usec' is certainly positive. */
121                result->tv_sec = x->tv_sec - y->tv_sec;
122                result->tv_usec = x->tv_usec - y->tv_usec;
123 mike   1.2 
124 kumpf  1.3     /* Return 1 if result is negative. */
125                return x->tv_sec < y->tv_sec;
126 mike   1.2 }
127            
128            PEGASUS_NAMESPACE_END

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2