Due to the lapse in federal government funding, NASA is not updating this website. We sincerely regret this inconvenience.
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  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 }
72 
73 void isodate2ymds(const char* isodate, int16_t *year, int16_t *month, int16_t *day, double *sec) {
74  if (isodate == NULL) {
75  *year = BAD_INT;
76  *month = BAD_INT;
77  *day = BAD_INT;
78  *sec = BAD_FLT;
79  return;
80  }
81 
82  struct tm trec = {0};
83  if (strptime(isodate, "%Y-%m-%dT%H:%M:%S", &trec) == NULL) {
84  *year = BAD_INT;
85  *month = BAD_INT;
86  *day = BAD_INT;
87  *sec = BAD_FLT;
88  }
89  else {
90  *year = trec.tm_year + 1900;
91  *month = trec.tm_mon + 1;
92  *day = trec.tm_mday;
93  *sec = trec.tm_hour * 3600 + trec.tm_min * 60 + trec.tm_sec;
94 
95  // check for fraction of a second
96  char* ptr = strchr(isodate, '.');
97  if (ptr) {
98  double fracsecs = atof(ptr);
99  *sec += fracsecs;
100  }
101 
102  }
103 
104 }
int32_t day
time_t gmt_offset(void)
Definition: gmt_offset.c:8
#define NULL
Definition: decode_rs.h:63
float tm[MODELMAX]
void isodate2ymds(const char *isodate, int16_t *year, int16_t *month, int16_t *day, double *sec)
Definition: unix2isodate.c:73
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
#define BAD_INT
Definition: genutils.h:23