flexPTP 1.0
An IEEE 1588 PTP implementation designed for microcontrollers
Loading...
Searching...
No Matches
timeutils.c
Go to the documentation of this file.
1/* (C) András Wiesner, 2020 */
2
3#include "timeutils.h"
4
5#include <flexptp_options.h>
6
7// TimestampU -> TimestampI
9{
10 ti->sec = (int64_t)tu->sec;
11 ti->nanosec = (int32_t)tu->nanosec;
12 return ti;
13}
14
16{
17 tu->sec = (uint64_t)ti->sec;
18 tu->nanosec = (uint32_t)ti->nanosec;
19 return tu;
20}
21
22
23// r = a + b;
25{
26 int64_t ns = nsI(a) + nsI(b);
27 nsToTsI(r, ns);
28 return r;
29}
30
31// r = a - b;
33{
34 int64_t ns = nsI(a) - nsI(b);
35 nsToTsI(r, ns);
36 return r;
37}
38
39TimestampI *divTime(TimestampI *r, const TimestampI *a, int divisor) {
40 int64_t ns = a->sec * NANO_PREFIX + a->nanosec;
41 ns = ns / divisor; // így a pontosság +-0.5ns
42 r->sec = ns / NANO_PREFIX;
43 r->nanosec = ns - r->sec * NANO_PREFIX;
44 return r;
45}
46
47uint64_t nsU(const TimestampU *t)
48{
49 return t->sec * NANO_PREFIX + t->nanosec;
50}
51
52int64_t nsI(const TimestampI *t)
53{
54 return t->sec * NANO_PREFIX + t->nanosec;
55}
56
58 uint32_t s = t->nanosec / NANO_PREFIX;
59 t->sec += s;
60 t->nanosec -= s * NANO_PREFIX;
61}
62
63int64_t tsToTick(const TimestampI * ts, uint32_t tps) {
64 int64_t ns = ts->sec * NANO_PREFIX + ts->nanosec;
65 int64_t ticks = (ns * tps) / NANO_PREFIX;
66 return ticks;
67}
68
69TimestampI *nsToTsI(TimestampI *r, int64_t ns) {
70 r->sec = ns / NANO_PREFIX;
71 r->nanosec = ns % NANO_PREFIX;
72 return r;
73}
74
75bool nonZeroI(const TimestampI *a) {
76 return a->sec != 0 || a->nanosec != 0;
77}
78
79static unsigned FIRST_DAY_OF_MONTH[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
80
81void tsPrint(char * str, const TimestampI * ts) {
82 unsigned fouryear, year, month, day, hour, minute, sec, rem;
83
84 fouryear = ts->sec / T_SEC_PER_FOURYEAR; // determine elapsed four year blocks from 1970 (YYLY)
85 rem = ts->sec - fouryear * T_SEC_PER_FOURYEAR; // compute remaining seconds
86 year = fouryear * 4; // calculate years fouryear-block part
87
88 // split up four-year blocks into distinct years
89 if (rem > T_SEC_PER_YEAR) {
90 year++;
91 rem -= T_SEC_PER_YEAR;
92 }
93
94 if (rem > T_SEC_PER_YEAR) {
95 year++;
96 rem -= T_SEC_PER_YEAR;
97 }
98
99 if (rem > T_SEC_PER_LEAPYEAR) {
100 year++;
101 rem -= T_SEC_PER_LEAPYEAR;
102 }
103
104 // convert remaining seconds to days
105 day = rem / T_SEC_PER_DAY;
106 rem -= day * T_SEC_PER_DAY;
107 day++;
108
109 year += 1970;
110 bool leapyear = year % 4 == 0;
111
112 // get month from days
113 unsigned i = 0;
114 for (i = 0; i < 12; i++) {
115 unsigned first1 = FIRST_DAY_OF_MONTH[i] + (((i >= 2) && leapyear) ? 1 : 0);
116 unsigned first2 = FIRST_DAY_OF_MONTH[i + 1] + (((i + 1 >= 2) && leapyear) ? 1 : 0);
117
118 if (day > first1 && day <= first2) {
119 month = i + 1;
120 day = day - first1;
121 break;
122 }
123 }
124
125 // get time
126 hour = rem / T_SEC_PER_HOUR;
127 rem -= hour * T_SEC_PER_HOUR;
128 minute = rem / T_SEC_PER_MINUTE;
129 rem -= minute * T_SEC_PER_MINUTE;
130 sec = rem;
131
132 // -------------------------------
133
134 // print datetime
135 FLEXPTP_SNPRINTF(str, 20, "%02d-%02d-%04d %02d:%02d:%02d", day, month, year, hour, minute, sec);
136}
#define FLEXPTP_SNPRINTF(...)
Timestamp (signed)
Definition: timeutils.h:33
int32_t nanosec
nanoseconds
Definition: timeutils.h:35
int64_t sec
seconds
Definition: timeutils.h:34
Timestamp (unsigned)
Definition: timeutils.h:24
uint32_t nanosec
nanoseconds
Definition: timeutils.h:26
uint64_t sec
seconds
Definition: timeutils.h:25
uint64_t nsU(const TimestampU *t)
Definition: timeutils.c:47
TimestampU * tsIToU(TimestampU *tu, const TimestampI *ti)
Definition: timeutils.c:15
TimestampI * addTime(TimestampI *r, const TimestampI *a, const TimestampI *b)
Definition: timeutils.c:24
static unsigned FIRST_DAY_OF_MONTH[]
Definition: timeutils.c:79
TimestampI * divTime(TimestampI *r, const TimestampI *a, int divisor)
Definition: timeutils.c:39
void normTime(TimestampI *t)
Definition: timeutils.c:57
int64_t tsToTick(const TimestampI *ts, uint32_t tps)
Definition: timeutils.c:63
TimestampI * nsToTsI(TimestampI *r, int64_t ns)
Definition: timeutils.c:69
TimestampI * tsUToI(TimestampI *ti, const TimestampU *tu)
Definition: timeutils.c:8
TimestampI * subTime(TimestampI *r, const TimestampI *a, const TimestampI *b)
Definition: timeutils.c:32
void tsPrint(char *str, const TimestampI *ts)
Definition: timeutils.c:81
bool nonZeroI(const TimestampI *a)
Definition: timeutils.c:75
int64_t nsI(const TimestampI *t)
Definition: timeutils.c:52
This module defines storage classes for timestamps and operations on time values.
#define T_SEC_PER_MINUTE
seconds per minute
Definition: timeutils.h:41
#define NANO_PREFIX
Integer nano prefix.
Definition: timeutils.h:38
#define T_SEC_PER_FOURYEAR
seconds per four years
Definition: timeutils.h:46
#define T_SEC_PER_YEAR
seconds per year
Definition: timeutils.h:44
#define T_SEC_PER_HOUR
seconds per hour
Definition: timeutils.h:42
#define T_SEC_PER_DAY
seconds per day
Definition: timeutils.h:43
#define T_SEC_PER_LEAPYEAR
seconds per leapyear
Definition: timeutils.h:45