ocssw  1.0
/disk01/web/ocssw/build/src/libgenutils/ydhmsf.c (r8084/r6104)
Go to the documentation of this file.
00001 #include "time_utils.h"
00002 
00003 /************************************************************************
00004 This function converts its input arguments to a string "YYYYDDDHHMMSSFFF"
00005 where YYYY is the year, DDD is the day of the year, HH is the hour,
00006 MM is the minute, SS is the second, and FFF is the fraction of a second.
00007 The first argument represents the number of seconds elapsed since
00008 1-Jan-1970 00:00:00.000 GMT.  The second argument determines whether
00009 the output string represents local time (L) or GMT (G).
00010 ************************************************************************/
00011 char * ydhmsf(double dtime, char zone){
00012 
00013   struct tm     *ts;
00014   time_t        itime;
00015   static char   string[17];
00016 
00017   itime = (time_t) dtime;
00018   switch(zone){
00019     case 'G': ts =    gmtime(&itime); break;
00020     case 'L': ts = localtime(&itime); break;
00021     default:
00022       fprintf(stderr,"-W- %s line %d: ",__FILE__,__LINE__);
00023       fprintf(stderr,"Bad timezone argument passed to ydhmsf().\n");
00024       exit(EXIT_FAILURE);
00025  }
00026   // Add 0.0005 to correct for round off error JMG (08/03/2009)
00027   // Check for fracSec == 1000 JMG (02/05/2012)
00028   int fracSec = floor( 1000 * (dtime - itime) + 0.0005 );
00029   if ( fracSec == 1000) {
00030     fracSec = 0;
00031     ts->tm_sec += 1;
00032   }
00033 
00034   sprintf(string,"%d%03d%02d%02d%02d%03.0f",
00035       ts->tm_year + 1900,
00036       ts->tm_yday + 1,
00037       ts->tm_hour,
00038       ts->tm_min,
00039       ts->tm_sec,
00040       (float) fracSec);
00041   return(string);
00042 }
00043