Apollo  v5.5.0
Open source self driving car software
time_util.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 The Apollo Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *****************************************************************************/
16 #pragma once
17 
18 #include <sys/time.h>
19 
20 namespace apollo {
21 namespace perception {
22 namespace lib {
23 
24 class TimeUtil {
25  public:
26  // @brief: UNIX timestamp to GPS timestamp, in seconds.
27  static double Unix2Gps(double unix_time) {
28  double gps_time = unix_time - UNIX_GPS_DIFF;
29  if (unix_time < LEAP_SECOND_TIMESTAMP) {
30  gps_time -= 1.0;
31  }
32  return gps_time;
33  }
34 
35  // @brief: GPS timestamp to UNIX timestamp, in seconds.
36  static double Gps2Unix(double gps_time) {
37  double unix_time = gps_time + UNIX_GPS_DIFF;
38  if (unix_time + 1 < LEAP_SECOND_TIMESTAMP) {
39  unix_time += 1.0;
40  }
41  return unix_time;
42  }
43 
44  static double GetCurrentTime() {
45  struct timeval tv;
46  gettimeofday(&tv, nullptr);
47  const double timestamp =
48  static_cast<double>(tv.tv_sec * 1000000 + tv.tv_usec);
49  return timestamp / 1000000;
50  }
51 
52  TimeUtil(const TimeUtil &) = delete;
53  TimeUtil &operator=(const TimeUtil &) = delete;
54 
55  private:
56  // unix timestamp(1970.01.01) is different from gps timestamp(1980.01.06)
57  static const int UNIX_GPS_DIFF = 315964782;
58  // unix timestamp(2016.12.31 23:59:59(60) UTC/GMT)
59  static const int LEAP_SECOND_TIMESTAMP = 1483228799;
60 };
61 
62 } // namespace lib
63 } // namespace perception
64 } // namespace apollo
Definition: blob.h:72
TimeUtil & operator=(const TimeUtil &)=delete
static double Gps2Unix(double gps_time)
Definition: time_util.h:36
Definition: time_util.h:24
TimeUtil(const TimeUtil &)=delete
static double GetCurrentTime()
Definition: time_util.h:44
static double Unix2Gps(double unix_time)
Definition: time_util.h:27