/* text/convert/nonint/sstime.c version librock_CHISEL _summary Parse date/time strings Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 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. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 REGENTS 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. */ #ifdef librock_NOTLIBROCK /**************************************************************/ /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #define librock_ISOLATED #define librock_PTR #define librock_CONST const #include #include #define librock_STRUCT_TM struct tm #define librock_TIME_T time_t /**************************************************************/ #endif #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_sstime #include #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_sstime /* License description features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_sstime[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /* Function wrapping and tracing features are documented at http://www.mibsoftware.com/librock/ */ /**************************************************************/ #define librock_body_ssgmtime librock_ssgmtime /* For librock_WRAP section */ /**************************************************************/ #endif #ifndef librock_NOIMPL_ssgmtime /**************************************************************/ int librock_body_ssgmtime(const char librock_PTR *sptr,librock_STRUCT_TM *argtmptr,librock_TIME_T *argtime) {/* Copyright 1998-2002, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_ssgmtime librock_CHISEL subject /text/convert/nonint/ librock_CHISEL subject /hard/time/ /* librock_ssgmtime - Parse a date/time string, storing to struct tm or time_t */ /**/ #include #include int librock_ssgmtime( const char librock_PTR *sptr, librock_STRUCT_TM *tmptr, librock_TIME_T *time); /* Scans strings of the following formats, with the part from HH:MM:SS optional MM/DD/YYYY HH:MM:SS MM/DD/YY HH:MM:SS DD MonthNAME YYYY HH:MM:SS -/+NNNN (NNNN are digits specifing the offset from GMT) DD MonthNAME YYYY HH:MM:SS GMT Two digit years less than 60 will be windowed into the range 2000-2059, otherwise in the range 1960-1999. Note that this does not parse the day of week. Returns the number of characters processed. Typical use is */ #ifdef librock_TYPICAL_USE_ssgmtime #include #include time_t t; char *ptr = "02/20/02 01:03:44"; ptr += librock_ssgmtime(ptr,NULL,(librock_TIME_T *)&t); printf("%ld\n",(long) t); ptr = "20 Feb 2002 02:03:44 +0100"; ptr += librock_ssgmtime(ptr,NULL,(librock_TIME_T *)&t); printf("%ld\n",(long) t); #endif /* tolower gmtime librock_CHISEL _usesrc "text/count.c" librock_countbl librock_counttoch librock_CHISEL _usesrc "text/convert/integer/ss.c" librock_ssdec librock_CHISEL _usesrc "text/compsearch/strfn.c" librock_wordcmp librock_CHISEL _usesrc "hard/time/gmtime.c" librock_mkgmtime librock_CHISEL _usesrc "hard/time/ssmonth.c" librock_ssmonth */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ const char librock_PTR *ptr; const char librock_PTR *afterdate; struct tm tmbuf; struct tm *ptmbuf = &tmbuf; int cnt = 0; int offset; struct tm *tmptr = (struct tm *) argtmptr; time_t *time = (time_t *) argtime; if (time) { *time = 0; /* default; */ } if (!ptmbuf) { return 0; } ptr = sptr; /* Parse for the date. */ ptr += librock_ssdec(ptr,&ptmbuf->tm_mon); if ((*ptr != '-')&&(*ptr != '/')) { if (*ptr == ' ') { ptr += librock_countbl(ptr); ptmbuf->tm_mday = ptmbuf->tm_mon; cnt = librock_ssmonth(ptr,&ptmbuf->tm_mon); if (cnt) { ptr += cnt; ptr += librock_countbl(ptr); goto year; } } return 0; /* invalid format for line */ } ptr++; ptr += librock_ssdec(ptr,&ptmbuf->tm_mday); if ((*ptr != '-')&&(*ptr != '/')) { return 0; /* invalid format for line */ } ptr++; year: ptr += librock_ssdec(ptr,&ptmbuf->tm_year); if (ptmbuf->tm_year < 60) { ptmbuf->tm_year += 2000; } else if (ptmbuf->tm_year < 1900) { ptmbuf->tm_year += 1900; } afterdate = ptr; ptr += librock_ssdec(ptr,&ptmbuf->tm_hour); if (*ptr == ':') { ptr++; ptr += librock_ssdec(ptr,&ptmbuf->tm_min); if (*ptr == ':') { ptr++; /* 12-09-96 bug fix*/ ptr += librock_ssdec(ptr,&ptmbuf->tm_sec); } else { ptmbuf->tm_sec = 0; } if (tolower(*ptr) == 'p') { if (ptmbuf->tm_hour == 12) { ptmbuf->tm_hour = 0; } ptmbuf->tm_hour += 12; ptr++; } else if (tolower(*ptr) == 'a') { if (ptmbuf->tm_hour == 12) { ptmbuf->tm_hour = 0; } ptr++; } } else { ptr = afterdate; ptmbuf->tm_hour = 0; ptmbuf->tm_min = 0; ptmbuf->tm_sec = 0; } afterdate = ptr; offset =0; if (cnt) { /* Look for GMT identifier */ ptr += librock_countbl(ptr); if ((*ptr == '-')||(*ptr == '+')) { offset = (ptr[1]-'0')*60*10 + (ptr[2]-'0')*60+ (ptr[3]-'0')*10+ (ptr[4]-'0'); if (*ptr == '-') { offset = -offset; } offset *= 60; ptr += 5; afterdate = ptr; ptr += librock_countbl(ptr); if (*ptr == '(') { cnt = librock_counttoch(ptr,')'); if (ptr[cnt] == ')') { ptr += cnt+1; } } else { ptr = afterdate; } cnt = 1; } else if (!librock_wordcmp(ptr,"GMT")) { ptr += 3; } else { ptr = afterdate; } } /* Convert to time. */ ptmbuf->tm_year -= 1900; if (cnt) { ptmbuf->tm_isdst = 0; } else { ptmbuf->tm_isdst = -1; } ptmbuf->tm_mon--; /* NOTE: This does not normalize values. */ if (tmptr) { *tmptr = *ptmbuf; } if (time) { librock_mkgmtime((librock_STRUCT_TM *)ptmbuf,(librock_TIME_T *)time); } if (offset) { *time = (time_t) ((long) *time - offset); if (argtmptr) { *((struct tm *)argtmptr) = *gmtime(time); } } return ptr-sptr; } /* ssgmtime */ /**************************************************************/ #endif /* NOIMP section */ /* ssgmtime consistency checker */ #if 0 _test_ssgmtime() { int year; int month; time_t t; struct tm *tmptr; char buf[80]; char buf2[80]; for(year = 1970;year < 2038;year++) { for(month = 1;month <= 12;month++) { sprintf(buf,"%d/%02d/%04d 16:59:20 GMT", month, 17, year); ssgmtime(buf,NULL,&t); tmptr = gmtime(&t); sprintf(buf2,"%d/%02d/%04d %02d:%02d:%02d GMT", tmptr->tm_mon+1, tmptr->tm_mday, tmptr->tm_year+1900, tmptr->tm_hour, tmptr->tm_min, tmptr->tm_sec); if (strcmp(buf,buf2) { printf("%s != %s\n",buf,buf2); } else { printf("%s\n",buf); } } } } #endif /* $Log: sstime.c,v $ Revision 1.6 2002/08/01 22:06:04 forrest@mibsoftware.com rights=#1 Cast during copy of *gmtime(). Minor fix to test_gmtime #if bracketing. Revision 1.5 2002/08/01 15:56:22 forrest@mibsoftware.com rights=#1 Fixed defect: +/-offset was not handled. Updated TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.4 2002/04/09 03:27:59 forrest@mibsoftware.com rights=#1 FNTYPE. Update LICENSE in manual. Revision 1.3 2002/02/05 21:31:28 forrest@mibsoftware.com rights=#1 Continue changes from recent commit. Revision 1.2 2002/02/05 21:28:44 forrest@mibsoftware.com rights=#1 Split out code for a librock_mkgmtime, and call it. Standardized chg log. Replaced TABS, spaces at EOL Revision 1.1 2002/01/29 14:25:28 forrest@mibsoftware.com rights=#1 initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */