luna-sysmgr-common
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Time.h
Go to the documentation of this file.
1 /* @@@LICENSE
2 *
3 * Copyright (c) 2008-2012 Hewlett-Packard Development Company, L.P.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * LICENSE@@@ */
18 
19 
20 
21 
22 #ifndef TIME_H
23 #define TIME_H
24 
25 #include "Common.h"
26 
27 #include <sys/time.h>
28 #include <time.h>
29 #include <stdint.h>
30 
31 class Time
32 {
33 public:
34 
35  static inline uint32_t convertToMs(const struct timeval* time)
36  {
37  return (uint32_t) (time->tv_sec * 1000 + time->tv_usec / 1000);
38  }
39 
40  static inline uint32_t convertToMs(const struct timespec* time)
41  {
42  return (uint32_t) (time->tv_sec * 1000 + time->tv_nsec / 1000000);
43  }
44 
45  static inline uint32_t curTimeMs()
46  {
47  struct timespec curTime;
48  ::clock_gettime(CLOCK_MONOTONIC, &curTime);
49  return convertToMs(&curTime);
50  }
51 /*
52  static inline uint32_t curUpTimeMs()
53  {
54  struct timespec curTime;
55  ::clock_gettime(CLOCK_UPTIME, &curTime);
56  return convertToMs(&curTime);
57  }
58 */
59  static inline int curTime(struct timespec* time)
60  {
61  return ::clock_gettime(CLOCK_MONOTONIC, time);
62  }
63 
64  static inline int curTime(struct timeval* time)
65  {
66  struct timespec tsTime;
67  if (!curTime(&tsTime)) {
68  time->tv_sec = tsTime.tv_sec;
69  time->tv_usec = tsTime.tv_nsec / 1000;
70  return 0;
71  }
72  return -1;
73  }
74 
75  static inline uint32_t curSysTimeMs() {
76  struct timeval currTime;
77  ::gettimeofday(&currTime, NULL);
78  return convertToMs(&currTime);
79  }
80 };
81 
82 #endif /* TIME_H */