This source file includes following definitions.
- timelib_diff
- timelib_add
- timelib_sub
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 #include "timelib.h"
26 #include <math.h>
27
28 timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
29 {
30 timelib_rel_time *rt;
31 timelib_time *swp;
32 timelib_sll dst_corr = 0 ,dst_h_corr = 0, dst_m_corr = 0;
33 timelib_time one_backup, two_backup;
34
35 rt = timelib_rel_time_ctor();
36 rt->invert = 0;
37 if (one->sse > two->sse) {
38 swp = two;
39 two = one;
40 one = swp;
41 rt->invert = 1;
42 }
43
44
45
46 if (one->zone_type == 3 && two->zone_type == 3
47 && (strcmp(one->tz_info->name, two->tz_info->name) == 0)
48 && (one->z != two->z))
49 {
50 dst_corr = two->z - one->z;
51 dst_h_corr = dst_corr / 3600;
52 dst_m_corr = (dst_corr % 3600) / 60;
53 }
54
55
56 memcpy(&one_backup, one, sizeof(one_backup));
57 memcpy(&two_backup, two, sizeof(two_backup));
58
59 timelib_apply_localtime(one, 0);
60 timelib_apply_localtime(two, 0);
61
62 rt->y = two->y - one->y;
63 rt->m = two->m - one->m;
64 rt->d = two->d - one->d;
65 rt->h = two->h - one->h;
66 rt->i = two->i - one->i;
67 rt->s = two->s - one->s;
68 if (one_backup.dst == 0 && two_backup.dst == 1 && two->sse >= one->sse + 86400 - dst_corr) {
69 rt->h += dst_h_corr;
70 rt->i += dst_m_corr;
71 }
72
73 rt->days = fabs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
74
75 timelib_do_rel_normalize(rt->invert ? one : two, rt);
76
77
78 if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
79 if (two->sse < one->sse + 86400 - dst_corr) {
80 rt->d--;
81 rt->h = 24;
82 } else {
83 rt->h += dst_h_corr;
84 rt->i += dst_m_corr;
85 }
86 }
87
88
89 memcpy(one, &one_backup, sizeof(one_backup));
90 memcpy(two, &two_backup, sizeof(two_backup));
91
92 return rt;
93 }
94
95 timelib_time *timelib_add(timelib_time *old_time, timelib_rel_time *interval)
96 {
97 int bias = 1;
98 timelib_time *t = timelib_time_clone(old_time);
99
100 if (interval->have_weekday_relative || interval->have_special_relative) {
101 memcpy(&t->relative, interval, sizeof(struct timelib_rel_time));
102 } else {
103 if (interval->invert) {
104 bias = -1;
105 }
106 memset(&t->relative, 0, sizeof(struct timelib_rel_time));
107 t->relative.y = interval->y * bias;
108 t->relative.m = interval->m * bias;
109 t->relative.d = interval->d * bias;
110 t->relative.h = interval->h * bias;
111 t->relative.i = interval->i * bias;
112 t->relative.s = interval->s * bias;
113 }
114 t->have_relative = 1;
115 t->sse_uptodate = 0;
116
117 timelib_update_ts(t, NULL);
118
119
120
121 if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
122 t->sse -= old_time->z;
123 t->sse += t->z;
124 }
125
126 timelib_update_from_sse(t);
127 t->have_relative = 0;
128
129 return t;
130 }
131
132 timelib_time *timelib_sub(timelib_time *old_time, timelib_rel_time *interval)
133 {
134 int bias = 1;
135 timelib_time *t = timelib_time_clone(old_time);
136
137 if (interval->invert) {
138 bias = -1;
139 }
140
141 memset(&t->relative, 0, sizeof(struct timelib_rel_time));
142 t->relative.y = 0 - (interval->y * bias);
143 t->relative.m = 0 - (interval->m * bias);
144 t->relative.d = 0 - (interval->d * bias);
145 t->relative.h = 0 - (interval->h * bias);
146 t->relative.i = 0 - (interval->i * bias);
147 t->relative.s = 0 - (interval->s * bias);
148 t->have_relative = 1;
149 t->sse_uptodate = 0;
150
151 timelib_update_ts(t, NULL);
152
153
154 if (old_time->dst == 1 && t->dst == 0 && !interval->y && !interval->m && !interval->d) {
155 t->sse -= old_time->z;
156 t->sse += t->z;
157 }
158
159 if (old_time->dst == 0 && t->dst == 1 && !interval->y && !interval->m && !interval->d ) {
160 t->sse -= old_time->z;
161 t->sse += t->z;
162 }
163
164 timelib_update_from_sse(t);
165
166 t->have_relative = 0;
167
168 return t;
169 }