Vuo 2.4.2
Loading...
Searching...
No Matches
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 VuoListAppendValue_VuoText(components, VuoText_makeWithoutCopying(VuoText_format("%lld year%s", llabs(years), llabs(years) == 1 ? "" : "s")));
72
73 if (months)
74 VuoListAppendValue_VuoText(components, VuoText_makeWithoutCopying(VuoText_format("%lld month%s", llabs(months), llabs(months) == 1 ? "" : "s")));
75
76 if (days)
77 VuoListAppendValue_VuoText(components, VuoText_makeWithoutCopying(VuoText_format("%lld day%s", llabs(days), llabs(days) == 1 ? "" : "s")));
78
79 if (hours)
80 VuoListAppendValue_VuoText(components, VuoText_makeWithoutCopying(VuoText_format("%lld hour%s", llabs(hours), llabs(hours) == 1 ? "" : "s")));
81
82 if (minutes)
83 VuoListAppendValue_VuoText(components, VuoText_makeWithoutCopying(VuoText_format("%lld minute%s", llabs(minutes), llabs(minutes) == 1 ? "" : "s")));
84
85 if (seconds)
86 VuoListAppendValue_VuoText(components, VuoText_makeWithoutCopying(VuoText_format("%g second%s", fabs(seconds), fabs(seconds) == 1 ? "" : "s")));
87
88 int componentCount = VuoListGetCount_VuoText(components);
89 if (componentCount == 1)
90 {
91 char *s = VuoText_format("%s%s",
92 VuoListGetValue_VuoText(components, 1),
93 value > 0 ? after : before);
94 VuoRelease(components);
95 return s;
96 }
97 else if (componentCount == 2)
98 {
99 char *s = VuoText_format("%s and %s%s",
100 VuoListGetValue_VuoText(components, 1),
101 VuoListGetValue_VuoText(components, 2),
102 value > 0 ? after : before);
103 VuoRelease(components);
104 return s;
105 }
106 else
107 {
108 VuoText componentsAndSeparators[componentCount*2];
109 for (int i = 1; i <= componentCount; ++i)
110 {
111 componentsAndSeparators[(i-1)*2] = VuoListGetValue_VuoText(components, i);
112 if (i < componentCount)
113 componentsAndSeparators[(i-1)*2 + 1] = (i == componentCount - 1) ? ", and " : ", ";
114 }
115 componentsAndSeparators[componentCount*2 - 1] = value > 0 ? after : before;
116 VuoText t = VuoText_append(componentsAndSeparators, componentCount*2);
117 VuoRetain(t);
118 char *s = strdup(t);
119 VuoRelease(t);
120 VuoRelease(components);
121 return s;
122 }
123}
124
129{
130 return valueA == valueB;
131}
132
137{
138 return valueA < valueB;
139}
140
147{
148 return (((years * 365 + months * 30 + days) * 24 + hours) * 60 + minutes) * 60 + seconds;
149}
150
164void VuoRelativeTime_getComponents(VuoRelativeTime relativeTime, VuoInteger *years, VuoInteger *months, VuoInteger *days, VuoInteger *hours, VuoInteger *minutes, VuoReal *seconds)
165{
166 VuoInteger minuteLength = 60;
167 VuoInteger hourLength = minuteLength * 60;
168 VuoInteger dayLength = hourLength * 24;
169 VuoInteger monthLength = dayLength * 30;
170 VuoInteger yearLength = dayLength * 365;
171
172 if (years)
173 *years = (VuoInteger)relativeTime / yearLength;
174 relativeTime = fmod(relativeTime, yearLength);
175
176 if (months)
177 *months = (VuoInteger)relativeTime / monthLength;
178 relativeTime = fmod(relativeTime, monthLength);
179
180 if (days)
181 *days = (VuoInteger)relativeTime / dayLength;
182 relativeTime = fmod(relativeTime, dayLength);
183
184 if (hours)
185 *hours = (VuoInteger)relativeTime / hourLength;
186 relativeTime = fmod(relativeTime, hourLength);
187
188 if (minutes)
189 *minutes = (VuoInteger)relativeTime / minuteLength;
190
191 if (seconds)
192 *seconds = fmod(relativeTime, 60);
193}