libtins  4.0
timestamp.h
1 /*
2  * Copyright (c) 2017, Matias Fontanini
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef TINS_TIMESTAMP_H
31 #define TINS_TIMESTAMP_H
32 
33 #include <stdint.h>
34 #include <tins/macros.h>
35 #include <tins/cxxstd.h>
36 #if TINS_IS_CXX11
37  #include <chrono>
38 #endif
39 
40 struct timeval;
41 
42 namespace Tins {
43 
47 class TINS_API Timestamp {
48 public:
49  #ifdef _WIN32
50  typedef long seconds_type;
51  typedef long microseconds_type;
52  #else
53  typedef time_t seconds_type;
54  typedef suseconds_t microseconds_type;
55  #endif
56 
60  static Timestamp current_time();
61 
65  Timestamp();
66 
67  #if TINS_IS_CXX11
68 
71  template<typename Rep, typename Period>
72  Timestamp(const std::chrono::duration<Rep, Period>& ts) {
73  timestamp_ = std::chrono::duration_cast<std::chrono::microseconds>(ts).count();
74  }
75  #endif
76 
82  Timestamp(const timeval& time_val);
83 
87  seconds_type seconds() const;
88 
95  microseconds_type microseconds() const;
96 
97  #if TINS_IS_CXX11
98 
101  operator std::chrono::microseconds() const {
102  return std::chrono::microseconds(timestamp_);
103  }
104  #endif
105 private:
106  Timestamp(uint64_t value);
107 
108  uint64_t timestamp_;
109 };
110 
111 } // Tins
112 
113 #endif // TINS_TIMESTAMP_H
Timestamp(const std::chrono::duration< Rep, Period > &ts)
Definition: timestamp.h:72
The Tins namespace.
Definition: address_range.h:38
Represents a packet timestamp.
Definition: timestamp.h:47