qLibc
qtime.c
Go to the documentation of this file.
1 /******************************************************************************
2  * qLibc
3  *
4  * Copyright (c) 2010-2015 Seungyoung Kim.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
28 
29 /**
30  * @file qtime.c Time handling APIs.
31  */
32 
33 //#define __USE_XOPEN
34 //#define _XOPEN_SOURCE
35 //#define _BSD_SOURCE
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <stdbool.h>
39 #include <string.h>
40 #include <sys/time.h>
41 #include "qinternal.h"
42 #include "utilities/qtime.h"
43 
44 /**
45  * Returns the current time in milliseconds.
46  *
47  * @return current time in milliseconds.
48  */
49 long qtime_current_milli(void) {
50  struct timeval tv;
51  gettimeofday(&tv, NULL);
52  long time = (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
53  return time;
54 }
55 
56 /**
57  * Get custom formmatted local time string.
58  *
59  * @param buf save buffer
60  * @param size buffer size
61  * @param utctime 0 for current time, universal time for specific time
62  * @param format format for strftime()
63  *
64  * @return string pointer of buf
65  *
66  * @code
67  * char *timestr = qtime_localtime_strf(0, "%H:%M:%S"); // HH:MM:SS
68  * free(timestr);
69  * char *timestr = qtime_localtime_strf(0, "%Y%m%d%H%M%S"); // YYMMDDhhmmss
70  * free(timestr);
71  * @endcode
72  */
73 char *qtime_localtime_strf(char *buf, int size, time_t utctime,
74  const char *format) {
75  if (utctime == 0)
76  utctime = time(NULL);
77  struct tm *localtm = localtime(&utctime);
78 
79  if (strftime(buf, size, format, localtm) == 0) {
80  snprintf(buf, size, "(buffer small)");
81  }
82 
83  return buf;
84 }
85 
86 /**
87  * Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
88  *
89  * @param utctime 0 for current time, universal time for specific time
90  *
91  * @return mallocked string pointer of time string
92  *
93  * @code
94  * char *timestr;
95  * timestr = qtime_localtime_str(0); // now
96  * free(timestr);
97  * timestr = qtime_localtime_str(time(NULL)); // same as above
98  * free(timestr);
99  * timestr = qtime_localtime_str(time(NULL) - 86400)); // 1 day before
100  * free(timestr);
101  * @endcode
102  */
103 char *qtime_localtime_str(time_t utctime) {
104  int size = sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1);
105  char *timestr = (char *) malloc(size);
106  qtime_localtime_strf(timestr, size, utctime, "%d-%b-%Y %H:%M:%S %z");
107  return timestr;
108 }
109 
110 /**
111  * Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
112  *
113  * @param utctime 0 for current time, universal time for specific time
114  *
115  * @return internal static string pointer of time string
116  *
117  * @code
118  * printf("%s", qtime_localtime_staticstr(0)); // now
119  * printf("%s", qtime_localtime_staticstr(time(NULL) + 86400)); // 1 day later
120  * @endcode
121  */
122 const char *qtime_localtime_staticstr(time_t utctime) {
123  static char timestr[sizeof(char)
124  * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1)];
125  qtime_localtime_strf(timestr, sizeof(timestr), utctime,
126  "%d-%b-%Y %H:%M:%S %z");
127  return timestr;
128 }
129 
130 /**
131  * Get custom formmatted GMT time string.
132  *
133  * @param buf save buffer
134  * @param size buffer size
135  * @param utctime 0 for current time, universal time for specific time
136  * @param format format for strftime()
137  *
138  * @return string pointer of buf
139  *
140  * @code
141  * char timestr[8+1];
142  * qtime_gmt_strf(buf, sizeof(buf), 0, "%H:%M:%S"); // HH:MM:SS
143  * @endcode
144  */
145 char *qtime_gmt_strf(char *buf, int size, time_t utctime, const char *format) {
146  if (utctime == 0)
147  utctime = time(NULL);
148  struct tm *gmtm = gmtime(&utctime);
149 
150  strftime(buf, size, format, gmtm);
151  return buf;
152 }
153 
154 /**
155  * Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
156  *
157  * @param utctime 0 for current time, universal time for specific time
158  *
159  * @return malloced string pointer which points GMT time string.
160  *
161  * @code
162  * char *timestr;
163  * timestr = qtime_gmt_str(0); // now
164  * free(timestr);
165  * timestr = qtime_gmt_str(time(NULL)); // same as above
166  * free(timestr);
167  * timestr = qtime_gmt_str(time(NULL) - 86400)); // 1 day before
168  * free(timestr);
169  * @endcode
170  */
171 char *qtime_gmt_str(time_t utctime) {
172  int size = sizeof(char)
173  * (CONST_STRLEN("Mon, 00 Jan 0000 00:00:00 GMT") + 1);
174  char *timestr = (char *) malloc(size);
175  qtime_gmt_strf(timestr, size, utctime, "%a, %d %b %Y %H:%M:%S GMT");
176  return timestr;
177 }
178 
179 /**
180  * Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
181  *
182  * @param utctime 0 for current time, universal time for specific time
183  *
184  * @return internal static string pointer which points GMT time string.
185  *
186  * @code
187  * printf("%s", qtime_gmt_staticstr(0)); // now
188  * printf("%s", qtime_gmt_staticstr(time(NULL) + 86400)); // 1 day later
189  * @endcode
190  */
191 const char *qtime_gmt_staticstr(time_t utctime) {
192  static char timestr[sizeof(char)
193  * (CONST_STRLEN("Mon, 00-Jan-0000 00:00:00 GMT") + 1)];
194  qtime_gmt_strf(timestr, sizeof(timestr), utctime,
195  "%a, %d %b %Y %H:%M:%S GMT");
196  return timestr;
197 }
198 
199 /**
200  * This parses GMT/Timezone(+/-) formatted time sting like
201  * 'Sun, 04 May 2008 18:50:39 GMT', 'Mon, 05 May 2008 03:50:39 +0900'
202  * and returns as universal time.
203  *
204  * @param gmtstr GMT/Timezone(+/-) formatted time string
205  *
206  * @return universal time(UTC). in case of conversion error, returns -1.
207  *
208  * @code
209  * time_t t = time(NULL);
210  * char *s = qtime_parse_gmtstr(t);
211  * printf("%d\n", t);
212  * printf("%s\n", s);
213  * printf("%d\n", qtime_parse_gmtstr(s)); // this must be same as t
214  * free(s);
215  * @endcode
216  */
217 time_t qtime_parse_gmtstr(const char *gmtstr) {
218  struct tm gmtm;
219  if (strptime(gmtstr, "%a, %d %b %Y %H:%M:%S", &gmtm) == NULL)
220  return 0;
221  time_t utc = timegm(&gmtm);
222  if (utc < 0)
223  return -1;
224 
225 // parse timezone
226  char *p;
227  if ((p = strstr(gmtstr, "+")) != NULL) {
228  utc -= ((atoi(p + 1) / 100) * 60 * 60);
229  if (utc < 0)
230  return -1;
231  } else if ((p = strstr(gmtstr, "-")) != NULL) {
232  utc += ((atoi(p + 1) / 100) * 60 * 60);
233  }
234 
235  return utc;
236 }
char * qtime_localtime_strf(char *buf, int size, time_t utctime, const char *format)
Get custom formmatted local time string.
Definition: qtime.c:73
char * qtime_gmt_str(time_t utctime)
Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
Definition: qtime.c:171
char * qtime_localtime_str(time_t utctime)
Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
Definition: qtime.c:103
const char * qtime_gmt_staticstr(time_t utctime)
Get GMT time string formatted like 'Wed, 11-Nov-2007 23:19:25 GMT'.
Definition: qtime.c:191
const char * qtime_localtime_staticstr(time_t utctime)
Get local time string formatted like '02-Nov-2007 16:37:39 +0900'.
Definition: qtime.c:122
long qtime_current_milli(void)
Returns the current time in milliseconds.
Definition: qtime.c:49
time_t qtime_parse_gmtstr(const char *gmtstr)
This parses GMT/Timezone(+/-) formatted time sting like 'Sun, 04 May 2008 18:50:39 GMT'...
Definition: qtime.c:217
char * qtime_gmt_strf(char *buf, int size, time_t utctime, const char *format)
Get custom formmatted GMT time string.
Definition: qtime.c:145