OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
ydhmsf.c
Go to the documentation of this file.
1 #include <timeutils.h>
2 #include <genutils.h>
3 
4 /************************************************************************
5 This function converts its input arguments to a string "YYYYDDDHHMMSSFFF"
6 where YYYY is the year, DDD is the day of the year, HH is the hour,
7 MM is the minute, SS is the second, and FFF is the fraction of a second.
8 The first argument represents the number of seconds elapsed since
9 1-Jan-1970 00:00:00.000 GMT. The second argument determines whether
10 the output string represents local time (L) or GMT (G).
11  ************************************************************************/
12 char * ydhmsf(double dtime, char zone) {
13  struct tm *ts;
14  time_t itime;
15  static char string[17];
16 
17  if(dtime == BAD_FLT) {
18  return "Undefined time";
19  }
20 
21  itime = (time_t) dtime;
22  switch (zone) {
23  case 'G': ts = gmtime(&itime);
24  break;
25  case 'L': ts = localtime(&itime);
26  break;
27  default:
28  fprintf(stderr, "-W- %s line %d: ", __FILE__, __LINE__);
29  fprintf(stderr, "Bad timezone argument passed to ydhmsf().\n");
30  exit(EXIT_FAILURE);
31  }
32  // Add 0.0005 to correct for round off error JMG (08/03/2009)
33  // Check for fracSec == 1000 JMG (02/05/2012)
34  int fracSec = floor(1000 * (dtime - itime) + 0.0005);
35  if (fracSec == 1000) {
36  fracSec = 0;
37  ts->tm_sec += 1;
38  }
39 
40  sprintf(string, "%d%03d%02d%02d%02d%03.0f",
41  ts->tm_year + 1900,
42  ts->tm_yday + 1,
43  ts->tm_hour,
44  ts->tm_min,
45  ts->tm_sec,
46  (float) fracSec);
47  return (string);
48 }
char * ydhmsf(double dtime, char zone)
Definition: ydhmsf.c:12
float tm[MODELMAX]
#define BAD_FLT
Definition: jplaeriallib.h:19