aboutsummaryrefslogtreecommitdiffstats
path: root/src/timer/timer.cpp
blob: 16d7dc3680a0c0a3f4c4ac3b9dc79ea70a747c08 (plain)
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
/**
* Timestamp Functions
* (C) 1999-2009 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/timer.h>
#include <botan/loadstor.h>
#include <ctime>

namespace Botan {

/**
* Get the system clock
*/
u64bit system_time()
   {
   return static_cast<u64bit>(std::time(0));
   }

/*
* Convert a time_t to a struct tm
*/
std::tm time_t_to_tm(u64bit timer)
   {
   std::time_t time_val = static_cast<std::time_t>(timer);

   std::tm* tm_p = std::gmtime(&time_val);
   if (tm_p == 0)
      throw Encoding_Error("time_t_to_tm could not convert");
   return (*tm_p);
   }

/**
* Read the clock and return the output
*/
void Timer::poll(Entropy_Accumulator& accum)
   {
   const u64bit clock_value = this->clock();
   accum.add(clock_value, 0);
   }

/**
* Combine a two time values into a single one
*/
u64bit Timer::combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz)
   {
   static const u64bit NANOSECONDS_UNITS = 1000000000;

   u64bit res = seconds * NANOSECONDS_UNITS;
   res += parts * (NANOSECONDS_UNITS / parts_hz);
   return res;
   }

/**
* ANSI Clock
*/
u64bit ANSI_Clock_Timer::clock() const
   {
   return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC);
   }

}