timestamp.h 2.97 KB
Newer Older
Axel Auweter's avatar
Axel Auweter committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
 * dcdbtimestamp.h
 *
 *  Created on: Feb 18, 2015
 *      Author: Axel Auweter
 */

/**
 * @file
 * @brief This file is a companion to the sensordatastore API.
 * It contains the DCDBTimeStamp class definition that helps in
 * creating and modifying timestamps in the SensorDataStore.
 */

#include <cstdint>
16
#include <ctime>
Axel Auweter's avatar
Axel Auweter committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
#include <stdexcept>

#ifndef DCDBTIMESTAMP_H
#define DCDBTIMESTAMP_H

class DCDBTimeStampConversionException : public std::runtime_error
{
public:
  DCDBTimeStampConversionException():runtime_error("Time stamp conversion error.") {}
};

/**
 * @brief The DCDBTimeStamp class contains a single TimeStamp.
 */
class DCDBTimeStamp
{
protected:
  uint64_t raw;         /**< The raw timestamp data (nanoseconds since Unix Epoch) */

  /**
   * @brief Parses a string and tries to derive the time from it by
   * guessing the format. Throws DCDBTimeStampException on failure.
   * @param timestr   A string containing a representation of time
41
   * @param localTime Denotes if the timestr contains local time instead of UTC
Axel Auweter's avatar
Axel Auweter committed
42
   */
43
  void guessFromString(std::string timestr, bool localTime = false);
Axel Auweter's avatar
Axel Auweter committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

public:

  /**
   * @brief Standard constructor. Initializes the object with the current time.
   */
  DCDBTimeStamp();

  /**
   * @brief Raw constructor. Initializes the object with an existing raw time.
   */
  DCDBTimeStamp(uint64_t ts);

  /**
   * @brief String constructor. Initializes the object by best guess from a time string.
   */
60
  DCDBTimeStamp(std::string ts, bool localTime = false);
Axel Auweter's avatar
Axel Auweter committed
61

62
63
64
65
66
  /**
   * @brief Time_t constructor. Initializes the object from a time_t struct.
   */
  DCDBTimeStamp(time_t ts);

Axel Auweter's avatar
Axel Auweter committed
67
68
69
70
71
72
73
74
75
76
  /**
   * @brief Standard destructor.
   */
  virtual ~DCDBTimeStamp();

  /**
   * @brief Sets the object's value to the current time.
   */
  void setNow(void);

77
78
79
80
81
82
83
84
85
86
  /**
   * @brief Convert from local time to UTC
   */
  void convertFromLocal(void);

  /**
   * @brief Convert from UTC to local time
   */
  void convertToLocal(void);

Axel Auweter's avatar
Axel Auweter committed
87
88
89
90
91
92
  /**
   * @brief Returns the raw time stamp value.
   * @return The object's value as uint64_t.
   */
  uint64_t getRaw(void);

93
94
95
96
97
98
  /**
   * @brief Returns the time stamp's value as human readable string
   * @return The object's value as std::string.
   */
  std::string getString(void);

99
100
101
102
103
104
  /**
   * @brief Returns the "weekstamp" corresponding to the object's value
   * @return The week number of the timestamp.
   */
  uint16_t getWeekstamp(void);

Axel Auweter's avatar
Axel Auweter committed
105
106
107
108
109
110
111
112
113
114
115
  /* Overloaded operators (compare raw values) */
  inline bool operator == (const DCDBTimeStamp& rhs) const {return raw == rhs.raw;}
  inline bool operator != (const DCDBTimeStamp& rhs) const {return raw != rhs.raw;}
  inline bool operator <  (const DCDBTimeStamp& rhs) const {return raw <  rhs.raw;}
  inline bool operator >  (const DCDBTimeStamp& rhs) const {return raw >  rhs.raw;}
  inline bool operator <= (const DCDBTimeStamp& rhs) const {return raw <= rhs.raw;}
  inline bool operator >= (const DCDBTimeStamp& rhs) const {return raw >= rhs.raw;}
};


#endif /* DCDBTIMESTAMP_H */