Line data Source code
1 : #include "rational.h"
2 :
3 : /**
4 : * @brief Current time in milliseconds
5 : * @return Returns long long int the number of milliseconds since the UNIX epoch
6 : */
7 0 : long long int cur_time_ms(void)
8 : {
9 : struct timeval t;
10 0 : gettimeofday(&t,NULL);
11 0 : long long mt = (long long)t.tv_sec * 1000 + t.tv_usec / 1000;
12 0 : return(mt);
13 : }
14 :
15 : /**
16 : * @brief Current time in nanoseconds
17 : * @return long long int number of nanoseconds, count starts at the Unix Epoch on January 1st, 1970 at UTC
18 : * @details Source: https://stackoverflow.com/questions/39439268/printing-time-since-epoch-in-nanoseconds
19 : */
20 330 : long long int cur_time_ns(void)
21 : {
22 : long long int ns;
23 : time_t sec;
24 : struct timespec spec;
25 330 : clock_gettime(CLOCK_REALTIME,&spec);
26 330 : sec = spec.tv_sec;
27 330 : ns = spec.tv_nsec;
28 330 : return(((long long int)sec * 1000000000LL) + ns);
29 : }
30 :
31 : /**
32 : *
33 : * @brief Convert from UNIXtime seconds to ISO datetimes
34 : * @param seconds - if a parameter is passed in the form of milliseconds,
35 : * then exactly the specified time will be converted to ISO format.
36 : * If 0 is passed, the current time will be printed out in ISO format.
37 : *
38 : */
39 264 : char *seconds_to_ISOdate(time_t seconds)
40 : {
41 : struct timeval curTime;
42 264 : gettimeofday(&curTime,NULL);
43 :
44 : // String to store converted time
45 : static char str_t[sizeof "2011-10-18 07:07:09"] = "";
46 264 : str_t[0] = '\0'; /* Initialize buffer as empty string */
47 :
48 : // Pointer to a structure with local time
49 : struct tm cur_time;
50 :
51 : // Convert system time to local time
52 264 : localtime_r(&seconds,&cur_time);
53 :
54 : // Create a string with date and time accurate to seconds
55 264 : strftime(str_t,sizeof(str_t),"%Y-%m-%d %H:%M:%S",&cur_time);
56 :
57 : #if 0
58 : printf("current time: %s \n",str_t);
59 : #endif
60 :
61 264 : return(str_t);
62 : }
63 :
64 : /**
65 : *
66 : * @brief "As a date", Convert nanoseconds to date format
67 : *
68 : */
69 : __attribute__((always_inline)) static inline Date asadate(const long long int nanoseconds)
70 : {
71 : /// Number of nanoseconds in a year
72 : /// 365*24*60*60*1000*1000*1000
73 1 : const long long int ns_in_year = 31536000000000000LL;
74 :
75 : /// Number of nanoseconds in a month
76 : /// ns_in_year/12
77 1 : const long long int ns_in_month = 2628000000000000LL;
78 :
79 : /// Number of nanoseconds in a week
80 : /// 7*24*60*60*1000*1000*1000
81 1 : const long long int ns_in_week = 604800000000000LL;
82 :
83 : /// Number of nanoseconds in a day
84 : /// 24*60*60*1000*1000*1000
85 1 : const long long int ns_in_day = 86400000000000LL;
86 :
87 : /// Number of nanoseconds in an hour
88 : /// 60*60*1000*1000*1000
89 1 : const long long int ns_in_hour = 3600000000000LL;
90 :
91 : /// Number of nanoseconds in a minute
92 : /// 60*1000*1000*1000
93 1 : const long long int ns_in_minute = 60000000000LL;
94 :
95 : /// Number of nanoseconds in a second
96 : /// 1000*1000*1000
97 1 : const long long int ns_in_second = 1000000000LL;
98 :
99 : /// Number of nanoseconds in a millisecond
100 : /// 1000*1000
101 1 : const long long int ns_in_millisecond = 1000000LL;
102 :
103 : /// Number of nanoseconds in a microsecond
104 : /// 1000
105 1 : const long long int ns_in_microsecond = 1000LL;
106 :
107 : // Initializing the structure that will be returned from the function
108 1 : Date date = {0};
109 :
110 1 : date.years = nanoseconds/ns_in_year;
111 :
112 1 : const long long int years_ns = date.years * ns_in_year;
113 1 : date.months = (nanoseconds - years_ns)/ns_in_month;
114 :
115 1 : const long long int months_ns = date.months * ns_in_month;
116 1 : date.weeks = (nanoseconds - years_ns - months_ns)/ns_in_week;
117 :
118 1 : const long long int weeks_ns = date.weeks * ns_in_week;
119 1 : date.days = (nanoseconds - years_ns - months_ns - weeks_ns)/ns_in_day;
120 :
121 1 : const long long int days_ns = date.days * ns_in_day;
122 1 : date.hours = (nanoseconds - years_ns - months_ns - weeks_ns - days_ns)/ns_in_hour;
123 :
124 1 : const long long int hours_ns = date.hours * ns_in_hour;
125 1 : date.minutes = (nanoseconds - years_ns - months_ns - weeks_ns - days_ns - hours_ns)/ns_in_minute;
126 :
127 1 : const long long int minutes_ns = date.minutes * ns_in_minute;
128 1 : date.seconds = (nanoseconds - years_ns - months_ns - weeks_ns - days_ns - hours_ns - minutes_ns)/ns_in_second;
129 :
130 1 : const long long int seconds_ns = date.seconds * ns_in_second;
131 1 : date.milliseconds = (nanoseconds - years_ns - months_ns - weeks_ns - days_ns - hours_ns - minutes_ns - seconds_ns)/ns_in_millisecond;
132 :
133 1 : const long long int milliseconds_ns = date.milliseconds * ns_in_millisecond;
134 1 : date.microseconds = (nanoseconds - years_ns - months_ns - weeks_ns - days_ns - hours_ns - minutes_ns - seconds_ns - milliseconds_ns)/ns_in_microsecond;
135 :
136 1 : const long long int microseconds_ns = date.microseconds * ns_in_microsecond;
137 1 : date.nanoseconds = (nanoseconds - years_ns - months_ns - weeks_ns - days_ns - hours_ns - minutes_ns - seconds_ns - milliseconds_ns - microseconds_ns);
138 :
139 1 : return(date);
140 : }
141 :
142 : /**
143 : *
144 : * The function for convert nanoseconds to a readable date. The function
145 : * generates a string if the structure element contains time data greater
146 : * than zero.
147 : *
148 : */
149 10 : static void catdate(
150 : char *const result,
151 : const long long int number,
152 : const char *const suffix)
153 : {
154 10 : if(number > 0LL)
155 : {
156 : // Temporary array
157 : char tmp[MAX_CHARACTERS];
158 : // Put a number into the temporary string array
159 4 : snprintf(tmp,sizeof(tmp),"%lld",number);
160 : // Copy the tmp line to the end of the result line
161 4 : strcat(result,tmp);
162 : // Add suffix
163 4 : strcat(result,suffix);
164 : // Add a space after the suffix
165 4 : strcat(result," ");
166 : }
167 10 : }
168 :
169 : /**
170 : *
171 : * Convert nanoseconds to human-readable date as a string
172 : *
173 : */
174 1 : char *form_date(const long long int nanoseconds)
175 : {
176 : // Zero out a static memory area with a string array
177 : static char result[MAX_CHARACTERS];
178 1 : result[0] = '\0'; /* Initialize buffer as empty string */
179 :
180 : // If the time passed as argument is less than one nanosecond
181 1 : if(nanoseconds == 0LL)
182 : {
183 0 : strcat(result,"0ns");
184 0 : return(result);
185 : }
186 :
187 : Date date = asadate(nanoseconds);
188 :
189 1 : catdate(result,date.years,"y");
190 1 : catdate(result,date.months,"mon");
191 1 : catdate(result,date.weeks,"w");
192 1 : catdate(result,date.days,"d");
193 1 : catdate(result,date.hours,"h");
194 1 : catdate(result,date.minutes,"min");
195 1 : catdate(result,date.seconds,"s");
196 1 : catdate(result,date.milliseconds,"ms");
197 :
198 : #if 0
199 :
200 : // Print out microseconds and nanoseconds only
201 : // when larger units of time are not exist.
202 : if(nanoseconds < 1000LL*1000LL)
203 : {
204 : #endif
205 1 : catdate(result,date.microseconds,"μs");
206 1 : catdate(result,date.nanoseconds,"ns");
207 : #if 0
208 : }
209 : #endif
210 :
211 : // Remove space at the end of a line
212 1 : result[strlen(result) - 1ULL] = '\0';
213 :
214 1 : return(result);
215 : }
216 : #if 0
217 : /// Test
218 :
219 : /// To build
220 : /// gcc -I../../logger/lib/ time.c
221 :
222 : /// 339800645368118513 = ((365*24*60*60*1000*1000*1000)*10) + (((365*24*60*60*1000*1000*1000)/12)*9)+((7*24*60*60*1000*1000*1000)*1)+((24*60*60*1000*1000*1000)*2)+((60*60*1000*1000*1000)*3)+((60*1000*1000*1000)*4)+((1000*1000*1000)*5)+((1000*1000)*368)+((1000)*118)+513
223 : /// Should be 10y 9mon 1w 2d 3h 4min 5s 368ms 118μs 513ns
224 :
225 : int main(void)
226 : {
227 :
228 : long long int ns = 339800645368118513LL;
229 : printf("%s\n",form_date(ns));
230 :
231 : printf("%s\n",form_date(273522528));
232 :
233 : return 0;
234 : }
235 : #endif
|