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
28
29void SystemClock::value(struct timeval & val) const
30{
33}
34
36{
37 struct timezone tz;
38
39 if (gettimeofday(&m_timeval, &tz) != 0)
40 {
42 }
43 else
44 {
46 }
47}
48
49void SystemClock::printDiff(const SystemClock & clock) const
50{
51 printDiff(clock.m_timeval);
52}
53
54void 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}
Provides an abstract interface to the system clock.
Definition SystemClock.h:35
SystemClock()
Constructor.
struct timeval m_timeval
Time value.
Definition SystemClock.h:85
void value(struct timeval &val) const
Get time value.
Result now()
Get the current time.
void printDiff(const SystemClock &clock) const
Print difference between two clocks to stdout.
Result
Result codes.
Definition SystemClock.h:42
C int printf(const char *format,...)
Output a formatted string to standard output.
Definition printf.cpp:22
C int gettimeofday(struct timeval *tv, struct timezone *tz)
Get current time of day.
unsigned int uint
Unsigned integer number.
Definition Types.h:44
unsigned long long u64
Unsigned 64-bit number.
Definition Types.h:50
Time value information.
Definition time.h:36
uint tv_usec
Microseconds.
Definition time.h:41
time_t tv_sec
Seconds.
Definition time.h:38
Time zone information.
Definition time.h:48