aboutsummaryrefslogtreecommitdiffstats
path: root/fsa/src/vespa/fsa/timestamp.h
blob: 774afb680bd51dd3e763bc9c69137d53d2683053 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
 * @author  Peter Boros
 * @date    2004/08/20
 * @version $Id$
 * @file    timestamp.h
 * @brief   Simple timestamp class.
 */

#pragma once

#include <sys/time.h>
#include <time.h>

namespace fsa {

// {{{ class TimeStamp

/**
 * @class TimeStamp
 * @brief Simple timestamp class.
 */
class TimeStamp {
private:
  struct timeval _ts;
public:
  /**
   * @brief Constructor, registers current time.
   */
  TimeStamp() {
    gettimeofday(&_ts,NULL);
  }
  /**
   * @brief Destructor.
   */
  ~TimeStamp() {}

  /**
   * @brief Reset timestamp.
   *
   * Set timestamp value to current time.
   */
  void reset()
  {
    gettimeofday(&_ts,NULL);
  }

  /**
   * @brief Get timestamp value (= object creation or last reset time).
   *
   * @return Timestamp value in seconds.
   */
  double getVal() const
  {
    return double(_ts.tv_sec)+double(_ts.tv_usec)/1000000.0;
  }

  /**
   * @brief Get elapsed time (since object creation time).
   *
   * @return Elapsed time in seconds.
   */
  double elapsed() const
  {
    struct timeval now;
    gettimeofday(&now,NULL);
    return double(now.tv_sec)-double(_ts.tv_sec)+
      (double(now.tv_usec)-double(_ts.tv_usec))/1000000.0;
  }

  /**
   * @brief Calculate difference between timestamps.
   *
   * @return Difference between timestamps in seconds.
   */
  double operator-(const TimeStamp &other) const
  {
    return double(_ts.tv_sec)-double(other._ts.tv_sec)+
      (double(_ts.tv_usec)-double(other._ts.tv_usec))/1000000.0;
  }
};

} // namespace fsa