Statistics
| Branch: | Revision:

root / include / platdep / timeutil.h @ 01873262

History | View | Annotate | Download (3.86 KB)

1
//=========================================================================
2
//  PLATDEP/TIME.H - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga, 2005
8
//          Dept. of Electrical and Computer Systems Engineering,
9
//          Monash University, Melbourne, Australia
10
//
11
//=========================================================================
12

    
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2005-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20

    
21
#ifndef __PLATDEP_TIME_H
22
#define __PLATDEP_TIME_H
23

    
24
#include <time.h>  // localtime()
25
#include <stdio.h> // sprintf()
26
#include <math.h>  // fmod()
27

    
28
//
29
// Platform-independent gettimeofday(), and some basic timeval operations
30
//
31

    
32
#ifndef _WIN32
33
# include <sys/time.h>
34
# include <unistd.h>
35
#else
36
# include <sys/types.h>
37
# include <sys/timeb.h>  // ftime(), timeb
38

    
39
// timeval is declared in <winsock.h> and <winsock2.h>; they're mutually exclusive
40
# if !defined(_WINSOCKAPI_) && !defined(_WINSOCK2API_)
41
#  ifdef WANT_WINSOCK2
42
#   include <winsock2.h>
43
#  else
44
#   include <winsock.h>
45
#  endif
46
#  undef min
47
#  undef max
48
# endif
49

    
50
// Windows doesn't have gettimeofday(), so emulate it with ftime()
51
inline int gettimeofday(struct timeval *tv, struct timezone *)
52
{
53
    struct timeb tb;
54
    ftime(&tb);
55
    tv->tv_sec = (long) tb.time;
56
    tv->tv_usec = tb.millitm * 1000UL;
57
    return 0;
58
}
59
#endif
60

    
61
inline timeval timeval_add(const timeval& a, const timeval& b)
62
{
63
    timeval res;
64
    res.tv_sec = a.tv_sec + b.tv_sec;
65
    res.tv_usec = a.tv_usec + b.tv_usec;
66
    if (res.tv_usec>1000000) {
67
        res.tv_sec++;
68
        res.tv_usec -= 1000000;
69
    }
70
    return res;
71
}
72

    
73
inline timeval timeval_add(const timeval& a, double b)
74
{
75
    double bInt;
76
    double bFrac = modf(b, &bInt);
77
    timeval res;
78
    res.tv_sec = a.tv_sec + (long)bInt;
79
    res.tv_usec = a.tv_usec + (long)floor(1000000.0*bFrac);
80
    if (res.tv_usec>1000000) {
81
        res.tv_sec++;
82
        res.tv_usec -= 1000000;
83
    }
84
    return res;
85
}
86

    
87
inline timeval timeval_substract(const timeval& a, const timeval& b)
88
{
89
    timeval res;
90
    res.tv_sec = a.tv_sec - b.tv_sec;
91
    res.tv_usec = a.tv_usec - b.tv_usec;
92
    if (res.tv_usec<0) {
93
        res.tv_sec--;
94
        res.tv_usec += 1000000;
95
    }
96
    return res;
97
}
98

    
99
inline timeval timeval_substract(const timeval& a, double b)
100
{
101
    double bInt;
102
    double bFrac = modf(b, &bInt);
103
    timeval res;
104
    res.tv_sec = a.tv_sec - (long)bInt;
105
    res.tv_usec = a.tv_usec - (long)floor(1000000.0*bFrac);
106
    if (res.tv_usec<0) {
107
        res.tv_sec--;
108
        res.tv_usec += 1000000;
109
    }
110
    return res;
111
}
112

    
113
inline bool timeval_greater(const timeval& a, const timeval& b)
114
{
115
    if (a.tv_sec==b.tv_sec)
116
        return a.tv_usec > b.tv_usec;
117
    else
118
        return (unsigned long)a.tv_sec > (unsigned long)b.tv_sec;
119
}
120

    
121
// On 32-bit architectures, good up to ~1200 hours
122
inline unsigned long timeval_msec(const timeval& a)
123
{
124
    return 1000*a.tv_sec + (a.tv_usec/1000);
125
}
126

    
127
// User must take care that result doesn't overflow!
128
inline long timeval_diff_usec(const timeval& t2, const timeval& t1)
129
{
130
    long sec = t2.tv_sec - t1.tv_sec;
131
    long usec = t2.tv_usec - t1.tv_usec;
132
    return sec*1000000L + usec;
133
}
134

    
135
inline timeval operator+(const timeval& a, const timeval& b) {return timeval_add(a,b);}
136
inline timeval operator-(const timeval& a, const timeval& b) {return timeval_substract(a,b);}
137

    
138
// prints time in "yyyy-mm-dd hh:mm:ss" format
139
inline char *opp_asctime(time_t t, char *buf)
140
{
141
    struct tm now = *localtime(&t);
142
    sprintf(buf,"%d-%02d-%02d %02d:%02d:%02d",
143
                now.tm_year+1900, now.tm_mon+1, now.tm_mday,
144
                now.tm_hour, now.tm_min, now.tm_sec);
145
    return buf;
146
}
147

    
148
#endif