libtins  3.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
timestamp.h
1 /*
2  * Copyright (c) 2016, 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 #ifdef _WIN32
34  #include <winsock2.h>
35 #else
36  #include <sys/time.h>
37 #endif
38 #include "cxxstd.h"
39 #if TINS_IS_CXX11
40  #include <chrono>
41 #endif
42 
43 namespace Tins {
47 class 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 
61  #ifdef _WIN32
62  //fixme
63  return Timestamp();
64  #else
65  timeval tv;
66  gettimeofday(&tv, 0);
67  return tv;
68  #endif
69  }
70 
74  Timestamp() : tv() {}
75 
76  #if TINS_IS_CXX11
77 
80  template<typename Rep, typename Period>
81  Timestamp(const std::chrono::duration<Rep, Period>& ts) {
82  using std::chrono::duration_cast;
83  using std::chrono::microseconds;
84  using std::chrono::seconds;
85 
86  tv.tv_sec = duration_cast<seconds>(ts).count();
87  tv.tv_usec = duration_cast<microseconds>(
88  ts - seconds(tv.tv_sec)).count();
89  }
90  #endif
91 
96  Timestamp(const timeval& time_val) : tv(time_val) {}
97 
101  seconds_type seconds() const {
102  return tv.tv_sec;
103  }
104 
108  microseconds_type microseconds() const {
109  return tv.tv_usec;
110  }
111 
112  #if TINS_IS_CXX11
113 
116  operator std::chrono::microseconds() const {
117  return std::chrono::seconds(seconds()) +
118  std::chrono::microseconds(microseconds());
119  }
120  #endif
121 private:
122  timeval tv;
123 };
124 
125 } // Tins
126 
127 #endif // TINS_TIMESTAMP_H
microseconds_type microseconds() const
Definition: timestamp.h:108
Timestamp()
Definition: timestamp.h:74
Timestamp(const std::chrono::duration< Rep, Period > &ts)
Definition: timestamp.h:81
static Timestamp current_time()
Constructs a Timestamp which will hold the current time.
Definition: timestamp.h:60
Timestamp(const timeval &time_val)
Definition: timestamp.h:96
Represents a packet timestamp.
Definition: timestamp.h:47
seconds_type seconds() const
Definition: timestamp.h:101