FreeNOS
SystemClock.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <Types.h>
19 #include <stdio.h>
20 #include <sys/time.h>
21 #include "SystemClock.h"
22 
24 {
25  m_timeval.tv_sec = 0;
26  m_timeval.tv_usec = 0;
27 }
28 
29 void SystemClock::value(struct timeval & val) const
30 {
31  val.tv_sec = m_timeval.tv_sec;
33 }
34 
36 {
37  struct timezone tz;
38 
39  if (gettimeofday(&m_timeval, &tz) != 0)
40  {
41  return SystemClock::IOError;
42  }
43  else
44  {
45  return SystemClock::Success;
46  }
47 }
48 
49 void SystemClock::printDiff(const SystemClock & clock) const
50 {
51  printDiff(clock.m_timeval);
52 }
53 
54 void SystemClock::printDiff(const struct timeval & stamp) const
55 {
56  const u64 usec1 = (m_timeval.tv_sec * 1000000) + (m_timeval.tv_usec);
57  const u64 usec2 = (stamp.tv_sec * 1000000) + (stamp.tv_usec);
58 
59  // Print time measured
60  printf("%us %uusec\n",
61  (uint) ((usec2 - usec1) / 1000000),
62  (uint) ((usec2 - usec1) % 1000000));
63 }
SystemClock::IOError
@ IOError
Definition: SystemClock.h:44
SystemClock.h
timezone
Time zone information.
Definition: time.h:47
Types.h
SystemClock::value
void value(struct timeval &val) const
Get time value.
Definition: SystemClock.cpp:29
timeval::tv_usec
uint tv_usec
Microseconds.
Definition: time.h:41
SystemClock::m_timeval
struct timeval m_timeval
Time value.
Definition: SystemClock.h:85
SystemClock::printDiff
void printDiff(const SystemClock &clock) const
Print difference between two clocks to stdout.
Definition: SystemClock.cpp:49
time.h
uint
unsigned int uint
Unsigned integer number.
Definition: Types.h:44
SystemClock
Provides an abstract interface to the system clock.
Definition: SystemClock.h:34
u64
unsigned long long u64
Unsigned 64-bit number.
Definition: Types.h:50
gettimeofday
int gettimeofday(struct timeval *tv, struct timezone *tz)
Get current time of day.
Definition: gettimeofday.cpp:22
printf
int printf(const char *format,...)
Output a formatted string to standard output.
Definition: printf.cpp:22
stdio.h
SystemClock::Success
@ Success
Definition: SystemClock.h:43
SystemClock::SystemClock
SystemClock()
Constructor.
Definition: SystemClock.cpp:23
timeval::tv_sec
time_t tv_sec
Seconds.
Definition: time.h:38
timeval
Time value information.
Definition: time.h:35
SystemClock::now
Result now()
Get the current time.
Definition: SystemClock.cpp:35
SystemClock::Result
Result
Result codes.
Definition: SystemClock.h:41