OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
unix2isodate.c
Go to the documentation of this file.
1 #include <timeutils.h>
2 #include <genutils.h>
3 
10 char * unix2isodate(double dtime, char zone) {
11  struct tm *ts;
12  time_t itime;
13  static char string[30];
14  static char tzone[4];
15 
16  if(dtime == BAD_FLT) {
17  return "Undefined time";
18  }
19 
20  itime = (time_t) dtime;
21  switch (zone) {
22  case 'G':
23  ts = gmtime(&itime);
24  break;
25  case 'L':
26  ts = localtime(&itime);
27  break;
28  default:
29  fprintf(stderr, "-W- %s line %d: ", __FILE__, __LINE__);
30  fprintf(stderr, "Bad timezone argument passed to unix2isodate().\n");
31  exit(EXIT_FAILURE);
32  }
33  // Add 0.0005 to correct for round off error JMG (08/03/2009)
34  // Check for fracSec == 1000 JMG (02/05/2012)
35  int fracSec = floor(1000 * (dtime - itime) + 0.0005);
36  if (fracSec == 1000) {
37  fracSec = 0;
38  ts->tm_sec += 1;
39  }
40 
41  sprintf(string, "%d-%02d-%02dT%02d:%02d:%02d.%03.0f", ts->tm_year + 1900, ts->tm_mon + 1,
42  ts->tm_mday, ts->tm_hour, ts->tm_min, ts->tm_sec, (float) fracSec);
43 
44  switch (zone) {
45  case 'G':
46  strcat(string, "Z");
47  break;
48  case 'L':
49  sprintf(tzone, "%+03d", (int) ts->tm_gmtoff / 3600);
50  strcat(string, tzone);
51  break;
52  default:
53  fprintf(stderr, "-W- %s line %d: ", __FILE__, __LINE__);
54  fprintf(stderr, "Bad timezone argument passed to unix2isodate().\n");
55  exit(EXIT_FAILURE);
56  }
57 
58  return (string);
59 }
60 
61 double isodate2unix(const char *isodate) {
62  struct tm trec = {0};
63  strptime(isodate, "%Y-%m-%dT%H:%M:%S", &trec);
64  double secSince = mktime(&trec) - gmt_offset();
65  char* ptr = strchr(isodate, '.');
66  if (ptr) {
67  double fracsecs = atof(ptr);
68  secSince += fracsecs;
69  }
70  return secSince;
71 }
time_t gmt_offset(void)
Definition: gmt_offset.c:8
float tm[MODELMAX]
char * unix2isodate(double dtime, char zone)
Definition: unix2isodate.c:10
double isodate2unix(const char *isodate)
Definition: unix2isodate.c:61
#define BAD_FLT
Definition: jplaeriallib.h:19