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