DateString.c

Go to the documentation of this file.
00001 /*
00002 *  Copyright (C) 2007 David Chin, Jolien Creighton
00003 *
00004 *  This program is free software; you can redistribute it and/or modify
00005 *  it under the terms of the GNU General Public License as published by
00006 *  the Free Software Foundation; either version 2 of the License, or
00007 *  (at your option) any later version.
00008 *
00009 *  This program is distributed in the hope that it will be useful,
00010 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 *  GNU General Public License for more details.
00013 *
00014 *  You should have received a copy of the GNU General Public License
00015 *  along with with program; see the file COPYING. If not, write to the
00016 *  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00017 *  MA  02111-1307  USA
00018 */
00019 
00020 /* <lalVerbatim file="DateStringCV">
00021 Author: David Chin <dwchin@umich.edu> +1-734-709-9119
00022 $Id: DateString.c,v 1.16 2007/06/08 14:41:43 bema Exp $
00023 </lalVerbatim> */
00024 
00025 /* <lalLaTeX>
00026 
00027 \subsection{Module \texttt{DateString.c}}
00028 \label{ss:DateString.c}
00029 
00030 Returns a formatted string for the date and time in ISO 8601 format,
00031 given the date in an \texttt{LALDate} structure.
00032 
00033 \subsection*{Prototypes}
00034 \vspace{0.1in}
00035 \input{DateStringCP}
00036 \idx{LALDateString()}
00037 
00038 \subsubsection*{Description}
00039 
00040 Returns a formatted string for the date and time in ISO 8601 format,
00041 given the date in an \texttt{LALDate} structure.  A date and time in
00042 ISO 8601 format looks like 2001-03-04 17:03:52 for March 4, 2001,
00043 5:03:52 pm.  The advantage of this format, besides avoiding Y2K
00044 issues, is that a numerical-order sort of dates will result in a
00045 chronologically ordered sort.  This routine is a replacement for
00046 \texttt{strftime(3c)}.
00047 
00048 \subsubsection*{Algorithms}
00049 
00050 Trivial.
00051 
00052 \subsubsection*{Uses}
00053 
00054 Suppose we would like to form a timestamp string for the current time.
00055 The following program would accomplish this:
00056 
00057 \begin{verbatim}
00058 #include <lal/Date.h>
00059 int main(int argc, char *argv[])
00060 {
00061     static LALStatus status;
00062     LIGOTimeGPS  gpstime;
00063     LALDate      laldate;
00064     LALUnixDate  utimestruct;
00065     CHARVector  *utc = NULL;
00066     time_t       ticks;
00067 
00068     INITSTATUS (status, "printone", TESTUTOGPSC);
00069 
00070     LALCHARCreateVector(status, &utc, (UINT4)64);
00071 
00072     ticks = time(NULL);
00073     gmtime_r(&ticks, &(laldate->unixDate));
00074     DateString(status, utc, &laldate);
00075 
00076     printf("%s\n", utc->data);
00077 
00078     CHARDestroyVector(status, &utc);
00079     
00080     RETURN (status);
00081 }
00082 \end{verbatim}
00083 
00084 \subsubsection*{Notes}
00085 
00086 See the official ISO document at
00087 \url{http://www.iso.ch/markete/8601.pdf}, and
00088 an overview of the format at
00089 \url{http://www.cl.cam.ac.uk/~mgk25/iso-time.html}. 
00090 
00091 </lalLaTeX> */
00092 
00093 #include <lal/LALRCSID.h>
00094 #include <lal/Date.h>
00095 
00096 
00097 NRCSID (DATESTRINGC, "$Id: DateString.c,v 1.16 2007/06/08 14:41:43 bema Exp $");
00098 
00099 
00100 /* <lalVerbatim file="DateStringCP"> */
00101 void
00102 LALDateString (LALStatus     *status,
00103                CHARVector    *timestamp,
00104                const LALDate *date)
00105 { /* </lalVerbatim> */
00106   CHAR tmpmon[3];
00107   CHAR tmpmday[3];
00108   CHAR tmphour[3];
00109   CHAR tmpmin[3];
00110   CHAR tmpsec[3];
00111   CHAR tmpwday[4];
00112 
00113   INITSTATUS (status, "LALDateString", DATESTRINGC);
00114 
00115   /*
00116    * Check pointer to input variable
00117    */
00118   ASSERT (date != (LALDate *)NULL, status,
00119           DATEH_ENULLINPUT, DATEH_MSGENULLINPUT);
00120 
00121   /*
00122    * Check that timestamp buffer is large enough
00123    */
00124   ASSERT (timestamp->length >= 32, status,
00125           DATEH_EBUFFTOOSMALL, DATEH_MSGEBUFFTOOSMALL);
00126 
00127   /*
00128    * Use strftime (3) to form ISO8601-format time stamp, plus day name
00129    */
00130   /*
00131     strftime(timestamp->data, timestamp->length,
00132            "%Y-%m-%d %H:%M:%S UTC %a", &(date->unixDate));
00133   */
00134 
00135   /*
00136    * AVOID STRFTIME() since it causes seg faults on Solaris.  We give
00137    * the locale-specific day name.
00138    */
00139   if (date->unixDate.tm_mon >= LALMONTH_OCT) {
00140     sprintf(tmpmon, "%2d", date->unixDate.tm_mon + 1);
00141   } else {
00142     sprintf(tmpmon, "0%1d", date->unixDate.tm_mon + 1);
00143   }
00144 
00145   if (date->unixDate.tm_mday >= 10) {
00146     sprintf(tmpmday, "%2d", date->unixDate.tm_mday);
00147   } else {
00148     sprintf(tmpmday, "0%1d", date->unixDate.tm_mday);
00149   }
00150 
00151   if (date->unixDate.tm_hour >= 10) {
00152     sprintf(tmphour, "%2d", date->unixDate.tm_hour);
00153   } else {
00154     sprintf(tmphour, "0%1d", date->unixDate.tm_hour);
00155   }
00156 
00157   if (date->unixDate.tm_min >= 10) {
00158     sprintf(tmpmin, "%2d", date->unixDate.tm_min);
00159   } else {
00160     sprintf(tmpmin, "0%1d", date->unixDate.tm_min);
00161   }
00162 
00163   if (date->unixDate.tm_sec >= 10) {
00164     sprintf(tmpsec, "%2d", date->unixDate.tm_sec);
00165   } else {
00166     sprintf(tmpsec, "0%1d", date->unixDate.tm_sec);
00167   }
00168 
00169   switch(date->unixDate.tm_wday) {
00170   case 0:
00171     sprintf(tmpwday, "Sun");
00172     break;
00173 
00174   case 1:
00175     sprintf(tmpwday, "Mon");
00176     break;
00177 
00178   case 2:
00179     sprintf(tmpwday, "Tue");
00180     break;
00181 
00182   case 3:
00183     sprintf(tmpwday, "Wed");
00184     break;
00185 
00186   case 4:
00187     sprintf(tmpwday, "Thu");
00188     break;
00189 
00190   case 5:
00191     sprintf(tmpwday, "Fri");
00192     break;
00193 
00194   case 6:
00195     sprintf(tmpwday, "Sat");
00196     break;
00197 
00198   default:
00199     sprintf(tmpwday, "%c", '\0');
00200     break;
00201   }
00202   
00203   sprintf(timestamp->data, "%d-%s-%s %s:%s:%s UTC %s",
00204           date->unixDate.tm_year + 1900,
00205           tmpmon, tmpmday, tmphour, tmpmin, tmpsec, tmpwday);
00206 
00207   RETURN (status);
00208 } /* END LALDateString() */
00209 
00210     

Generated on Sat Sep 6 03:06:48 2008 for LAL by  doxygen 1.5.2