tobicore  7.0.0
 All Classes Functions Variables Typedefs Enumerator Friends Groups Pages
TCTimestamp.cpp
1 /*
2  Copyright (C) 2009-2011 EPFL (Ecole Polytechnique Fédérale de Lausanne)
3  Michele Tavella <michele.tavella@epfl.ch>
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef TCTIMESTAMP_CPP
20 #define TCTIMESTAMP_CPP
21 
22 #include "TCTimestamp.hpp"
23 #include <time.h>
24 #include <stdio.h>
25 #include <string.h>
26 
28  this->Unset();
29 }
30 
32 }
33 
34 void TCTimestamp::Unset(void) {
35  timerclear(&this->timestamp);
36 }
37 
38 bool TCTimestamp::IsSet(void) {
39  return(timerisset(&this->timestamp) != 0);
40 }
41 
42 void TCTimestamp::Tic(void) {
43  gettimeofday(&this->timestamp, NULL);
44 }
45 
46 double TCTimestamp::Toc(TCTimeval* timestamp) {
47  TCTimeval toc;
48  gettimeofday(&toc, NULL);
49 
50  return double((toc.tv_sec - timestamp->tv_sec)*1000000 +
51  toc.tv_usec - timestamp->tv_usec)/1000;
52 }
53 
54 double TCTimestamp::Toc(void) {
55  return this->Toc(&this->timestamp);
56 }
57 
58 void TCTimestamp::Get(std::string* timestamp) const{
59  char cache[256];
60  sprintf(cache, "%ld,%ld",
61  this->timestamp.tv_sec,
62  this->timestamp.tv_usec);
63 
64  timestamp->clear();
65  timestamp->assign(cache);
66 }
67 
68 bool TCTimestamp::Set(const std::string& timestamp) {
69  int status = sscanf(timestamp.c_str(), "%ld,%ld",
70  &this->timestamp.tv_sec,
71  &this->timestamp.tv_usec);
72  return(status == 2);
73 }
74 
75 void TCTimestamp::Set(const TCTimeval* timestamp) {
76  this->timestamp.tv_sec = timestamp->tv_sec;
77  this->timestamp.tv_usec = timestamp->tv_usec;
78 }
79 
80 #endif