-
Notifications
You must be signed in to change notification settings - Fork 270
/
time.h
105 lines (77 loc) · 3.09 KB
/
time.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* Copyright (C) 2017-2018 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once
// ----- Defines -----
// ----- Includes -----
// System Includes
#include <stdint.h>
// ----- Structs -----
typedef struct Time {
uint32_t ms;
uint32_t ticks;
} Time;
typedef struct TickStore {
Time last_tick; // Time of last tick
Time tick_duration; // Duration of a tick
uint32_t ticks_since_start; // How many ticks since start of tick sequence
uint32_t max_ticks; // Tick limit for this TickStore
uint8_t fresh_store; // Set to 1, if no updates have been attempted
} TickStore;
// ----- Variables -----
extern const uint32_t Time_maxTicks;
extern const uint32_t Time_maxTicks_ms;
extern const char* Time_ticksPer_ns_str;
// ----- Functions -----
// Get current time
Time Time_now();
Time Time_init();
uint8_t Time_add( Time *current, Time add );
int8_t Time_compare( Time base, Time compare );
// Conversions
uint32_t Time_days( Time time );
uint32_t Time_hours( Time time );
uint32_t Time_minutes( Time time );
uint32_t Time_seconds( Time time );
uint32_t Time_ms( Time time );
uint32_t Time_us( Time time );
uint32_t Time_ns( Time time );
uint32_t Time_ticks( Time time );
Time Time_from_days( uint32_t days );
Time Time_from_hours( uint32_t hours );
Time Time_from_minutes( uint32_t minutes );
Time Time_from_seconds( uint32_t seconds );
Time Time_from_ms( uint32_t ms );
// Durations
Time Time_duration( Time since );
Time Time_duration_rollover( Time now, Time since );
Time Time_duration_rollover_now( Time since );
uint32_t Time_duration_ticks( Time since );
uint32_t Time_duration_days( Time since );
uint32_t Time_duration_hours( Time since );
uint32_t Time_duration_minutes( Time since );
uint32_t Time_duration_seconds( Time since );
uint32_t Time_duration_ms( Time since );
uint32_t Time_duration_us( Time since );
uint32_t Time_duration_ns( Time since );
// Tick Functions
void Time_tick_start( TickStore *store, Time duration, uint32_t max_ticks );
void Time_tick_reset( TickStore *store );
uint32_t Time_tick_update( TickStore *store );