Vuo  2.4.0
VuoRelativeTime.c
Go to the documentation of this file.
1
10#include "type.h"
11#include "VuoRelativeTime.h"
13
15#ifdef VUO_COMPILER
17 "title" : "Relative Date-Time",
18 "description" : "An offset from a Date-Time.",
19 "keywords" : [ ],
20 "version" : "1.0.0",
21 "dependencies" : [
22 "VuoList_VuoRelativeTime",
23 "VuoList_VuoText",
24 "VuoText"
25 ]
26 });
27#endif
29
36{
37 return json_object_get_double(js);
38}
39
44{
45 return json_object_new_double(value);
46}
47
52{
53 const char *after = " after";
54 const char *before = " before";
55
56 if (fabs(value) < 0.00001)
57 return strdup("no offset");
58
59 VuoInteger years;
60 VuoInteger months;
61 VuoInteger days;
62 VuoInteger hours;
63 VuoInteger minutes;
64 VuoReal seconds;
65 VuoRelativeTime_getComponents(value, &years, &months, &days, &hours, &minutes, &seconds);
66
68 VuoRetain(components);
69
70 if (years)
71 {
72 char *s = VuoText_format("%lld year%s", llabs(years), llabs(years) == 1 ? "" : "s");
74 free(s);
75 }
76
77 if (months)
78 {
79 char *s = VuoText_format("%lld month%s", llabs(months), llabs(months) == 1 ? "" : "s");
81 free(s);
82 }
83
84 if (days)
85 {
86 char *s = VuoText_format("%lld day%s", llabs(days), llabs(days) == 1 ? "" : "s");
88 free(s);
89 }
90
91 if (hours)
92 {
93 char *s = VuoText_format("%lld hour%s", llabs(hours), llabs(hours) == 1 ? "" : "s");
95 free(s);
96 }
97
98 if (minutes)
99 {
100 char *s = VuoText_format("%lld minute%s", llabs(minutes), llabs(minutes) == 1 ? "" : "s");
102 free(s);
103 }
104
105 if (seconds)
106 {
107 char *s = VuoText_format("%g second%s", fabs(seconds), fabs(seconds) == 1 ? "" : "s");
109 free(s);
110 }
111
112 int componentCount = VuoListGetCount_VuoText(components);
113 if (componentCount == 1)
114 {
115 char *s = VuoText_format("%s%s",
116 VuoListGetValue_VuoText(components, 1),
117 value > 0 ? after : before);
118 VuoRelease(components);
119 return s;
120 }
121 else if (componentCount == 2)
122 {
123 char *s = VuoText_format("%s and %s%s",
124 VuoListGetValue_VuoText(components, 1),
125 VuoListGetValue_VuoText(components, 2),
126 value > 0 ? after : before);
127 VuoRelease(components);
128 return s;
129 }
130 else
131 {
132 VuoText componentsAndSeparators[componentCount*2];
133 for (int i = 1; i <= componentCount; ++i)
134 {
135 componentsAndSeparators[(i-1)*2] = VuoListGetValue_VuoText(components, i);
136 if (i < componentCount)
137 componentsAndSeparators[(i-1)*2 + 1] = (i == componentCount - 1) ? ", and " : ", ";
138 }
139 componentsAndSeparators[componentCount*2 - 1] = value > 0 ? after : before;
140 VuoText t = VuoText_append(componentsAndSeparators, componentCount*2);
141 VuoRetain(t);
142 char *s = strdup(t);
143 VuoRelease(t);
144 VuoRelease(components);
145 return s;
146 }
147}
148
153{
154 return valueA == valueB;
155}
156
161{
162 return valueA < valueB;
163}
164
171{
172 return (((years * 365 + months * 30 + days) * 24 + hours) * 60 + minutes) * 60 + seconds;
173}
174
188void VuoRelativeTime_getComponents(VuoRelativeTime relativeTime, VuoInteger *years, VuoInteger *months, VuoInteger *days, VuoInteger *hours, VuoInteger *minutes, VuoReal *seconds)
189{
190 VuoInteger minuteLength = 60;
191 VuoInteger hourLength = minuteLength * 60;
192 VuoInteger dayLength = hourLength * 24;
193 VuoInteger monthLength = dayLength * 30;
194 VuoInteger yearLength = dayLength * 365;
195
196 if (years)
197 *years = (VuoInteger)relativeTime / yearLength;
198 relativeTime = fmod(relativeTime, yearLength);
199
200 if (months)
201 *months = (VuoInteger)relativeTime / monthLength;
202 relativeTime = fmod(relativeTime, monthLength);
203
204 if (days)
205 *days = (VuoInteger)relativeTime / dayLength;
206 relativeTime = fmod(relativeTime, dayLength);
207
208 if (hours)
209 *hours = (VuoInteger)relativeTime / hourLength;
210 relativeTime = fmod(relativeTime, hourLength);
211
212 if (minutes)
213 *minutes = (VuoInteger)relativeTime / minuteLength;
214
215 if (seconds)
216 *seconds = fmod(relativeTime, 60);
217}