/*- * Copyright (c) 1999, 2000, 2001, 2002, 2004 Lev Walkin . * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: timetostr.c,v 1.2 2005/05/25 21:55:10 vlm Exp $ */ #ifndef __EXTENSIONS__ #define __EXTENSIONS__ #endif #include #include #include #include #include #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef HAVE_UNISTD_H #include #endif #ifdef HAVE_TIME_H #include #endif #ifdef HAVE_SYS_TIME_H #include #endif #ifdef HAVE_ERRNO_H #include #else #ifndef EINVAL #define EINVAL 1 #endif #endif #ifndef HAVE_SNPRINTF #ifdef HAVE__SNPRINTF #define snprintf _snprintf #else #error "This system has neither snprintf() nor _snprintf()" #endif #endif #include #ifndef HAVE_TM_ZONE #ifndef HAVE_TZNAME #warning "Assume, there is char *tzname[2]" #define HAVE_TZNAME #warning "Please, please report this situation to vlm@lionet.info!" #warning "This iz due to my lazyness in checking the unusual states..." #endif #endif #ifndef HAVE_LOCALTIME_R /* * Emulate localtime_r() using localtime(). */ static struct tm * _sf_localtime_r(const time_t *clock, struct tm *result) { struct tm *tmp; tmp = localtime(clock); if(tmp) /* Assume result is not NULL */ memcpy(result, tmp, sizeof(struct tm)); return result; } #define localtime_r _sf_localtime_r #endif #ifndef HAVE_GMTIME_R /* * Emulate gmtime_r() using gmtime(). * Used in timetostr(). */ static struct tm * _sf_gmtime_r(const time_t *clock, struct tm *result) { struct tm *tmp; tmp = gmtime(clock); if(tmp) /* Assume result is not NULL */ memcpy(result, tmp, sizeof(struct tm)); return result; } #define gmtime_r _sf_gmtime_r #endif /******************/ /* TIME TO STRING */ /******************/ char * timetostr(time_t value, int flags) { #define SF_INT_TS_SIZE 64 static char ts[SF_INT_TS_SIZE]; static struct tm tm; static char *wd[]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; static char *mn[]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; int uf = 0; int ogmt = 0; /* Splitting the value */ if((flags & TFMT_LOCAL) && !(flags & TFMT_OLD822GMT)) (void)localtime_r(&value, &tm); else { (void)gmtime_r(&value, &tm); } if(flags & TFMT_UF) uf=1; if(flags & TFMT_OLD822GMT) ogmt=1; flags = flags & (int)0xff; if(flags == TFMT_RFC822) { char *fmts; #ifdef HAVE_TM_ZONE if(!tm.tm_zone) uf = 0; #else #ifndef HAVE_TZNAME uf = 0; #endif #endif if(ogmt) fmts = "%s, %02d %s %4d %02d:%02d:%02d GMT"; else fmts = "%s, %02d %s %4d %02d:%02d:%02d"; fmts = ts + snprintf(ts, SF_INT_TS_SIZE, fmts, wd[tm.tm_wday], tm.tm_mday, mn[tm.tm_mon], tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec); if(!ogmt) { fmts += snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), " %c%02ld%02ld", #ifdef HAVE_TM_ZONE (tm.tm_gmtoff<0)?'-':'+', (long)( (labs(tm.tm_gmtoff) % 86400) / 3600 ), (long)( (labs(tm.tm_gmtoff) % 3600) / 60 )); if(uf) fmts += snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), " (%s)", tm.tm_zone); #else #ifdef HAVE_TZNAME (timezone>0)?'-':'+', (long)( (labs(timezone) % 86400) / 3600 ), (long)( (labs(timezone) % 3600) / 60 )); if(uf) fmts += snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), " (%s)", tzname[0]); #else 0, 0); #endif #endif /* HAVE_TM_ZONE */ } } else if(flags == TFMT_ISO8601 || flags == TFMT_X208) { char *fmts; if(flags == TFMT_ISO8601) { if(uf) fmts="%04d-%02d-%02dT%02d:%02d:%02d"; else fmts="%04d%02d%02dT%02d%02d%02d"; } else { fmts="%04d%02d%02d%02d%02d%02d"; } fmts = ts + snprintf(ts, SF_INT_TS_SIZE, fmts, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); #ifdef HAVE_TM_ZONE if(tm.tm_gmtoff) snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), "%c%02ld%02ld", (tm.tm_gmtoff<0)?'-':'+', (long)( (labs(tm.tm_gmtoff) % 86400) / 3600 ), (long)( (labs(tm.tm_gmtoff) % 3600) / 60 )); else snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), "Z"); #else if(timezone) snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), "%c%02ld%02ld", (timezone>0)?'-':'+', (long)( (labs(timezone) % 86400) / 3600 ), (long)( (labs(timezone) % 3600) / 60 )); else snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), "Z"); #endif /* HAVE_TM_ZONE */ } else if(flags == TFMT_CTIME) { char *fmts; fmts = ts + snprintf(ts, SF_INT_TS_SIZE, "%s %s %02d %02d:%02d:%02d %04d", wd[tm.tm_wday], mn[tm.tm_mon], tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_year + 1900); #ifdef HAVE_TM_ZONE if(uf) snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), " %c%02ld%02ld", (tm.tm_gmtoff<0)?'-':'+', (long)( (tm.tm_gmtoff % 86400) / 3600 ), (long)( (tm.tm_gmtoff % 3600) / 60 )); #else if(uf) snprintf(fmts, SF_INT_TS_SIZE - (fmts - ts), " %c%02ld%02ld", (timezone>0)?'-':'+', (long)( (timezone % 86400) / 3600 ), (long)( (timezone % 3600) / 60 )); #endif /* HAVE_TM_ZONE */ } else { sprintf(ts, "%ld", value); } return ts; }