00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <stdbool.h>
00033 #include <string.h>
00034 #include <time.h>
00035 #include "qDecoder.h"
00036 #include "qInternal.h"
00037
00055 char *qTimeGetLocalStrf(char *buf, int size, time_t utctime, const char *format) {
00056 if(utctime == 0) utctime = time(NULL);
00057 struct tm *localtm = localtime(&utctime);
00058
00059 if(strftime(buf, size, format, localtm) == 0) {
00060 snprintf(buf, size, "(buffer small)");
00061 }
00062
00063 return buf;
00064 }
00065
00083 char *qTimeGetLocalStr(time_t utctime) {
00084 int size = sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1);
00085 char *timestr = (char *)malloc(size);
00086 qTimeGetLocalStrf(timestr, size, utctime, "%d-%b-%Y %H:%M:%S %z");
00087 return timestr;
00088 }
00089
00102 const char *qTimeGetLocalStaticStr(time_t utctime) {
00103 static char timestr[sizeof(char) * (CONST_STRLEN("00-Jan-0000 00:00:00 +0000") + 1)];
00104 qTimeGetLocalStrf(timestr, sizeof(timestr), utctime, "%d-%b-%Y %H:%M:%S %z");
00105 return timestr;
00106 }
00107
00123 char *qTimeGetGmtStrf(char *buf, int size, time_t utctime, const char *format) {
00124 if(utctime == 0) utctime = time(NULL);
00125 struct tm *gmtm = gmtime(&utctime);
00126
00127 strftime(buf, size, format, gmtm);
00128 return buf;
00129 }
00130
00148 char *qTimeGetGmtStr(time_t utctime) {
00149 int size = sizeof(char) * (CONST_STRLEN("Mon, 00-Jan-0000 00:00:00 GMT") + 1);
00150 char *timestr = (char*)malloc(size);
00151 qTimeGetGmtStrf(timestr, size, utctime, "%a, %d %b %Y %H:%M:%S GMT");
00152 return timestr;
00153 }
00154
00167 const char *qTimeGetGmtStaticStr(time_t utctime) {
00168 static char timestr[sizeof(char) * (CONST_STRLEN("Mon, 00-Jan-0000 00:00:00 GMT") + 1)];
00169 qTimeGetGmtStrf(timestr, sizeof(timestr), utctime, "%a, %d %b %Y %H:%M:%S GMT");
00170 return timestr;
00171 }
00172
00191 time_t qTimeParseGmtStr(const char *gmtstr) {
00192 struct tm gmtm;
00193 if(strptime(gmtstr, "%a, %d %b %Y %H:%M:%S", &gmtm) == NULL) return 0;
00194 time_t utc = timegm(&gmtm);
00195 if(utc < 0) return -1;
00196
00197
00198 char *p;
00199 if((p = strstr(gmtstr, "+")) != NULL) {
00200 utc -= ((atoi(p + 1) / 100) * 60 * 60);
00201 if(utc < 0) return -1;
00202 } else if((p = strstr(gmtstr, "-")) != NULL) {
00203 utc += ((atoi(p + 1) / 100) * 60 * 60);
00204 }
00205
00206 return utc;
00207 }