BRL-CAD
date-time.c
Go to the documentation of this file.
1 /* D A T E - T I M E . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <time.h>
24 #include <string.h>
25 
26 #include "bu/time.h"
27 #include "bu/parallel.h"
28 #include "bu/vls.h"
29 
30 #include "y2038/time64.h"
31 
32 void
33 bu_utctime(struct bu_vls *vls_gmtime, const int64_t time_val)
34 {
35  static const char *nulltime = "0000-00-00T00:00:00Z";
36  struct tm loctime;
37  struct tm* retval;
38  Time64_T some_time;
39  int fail = 0;
40 
41  if (!vls_gmtime)
42  return;
43 
44  BU_CK_VLS(vls_gmtime);
45 
46  some_time = (Time64_T)time_val;
47  if (some_time == (Time64_T)(-1)) {
48  /* time error: but set something, an invalid "NULL" time. */
49  bu_vls_sprintf(vls_gmtime, nulltime);
50  return;
51  }
52 
53  memset(&loctime, 0, sizeof(loctime));
54 
56  retval = gmtime64(&some_time);
57  if (retval)
58  loctime = *retval; /* struct copy */
59  else
60  fail = 1;
62 
63  if (fail) {
64  /* time error: but set something, an invalid "NULL" time. */
65  bu_vls_sprintf(vls_gmtime, nulltime);
66  return;
67  }
68 
69  /* put the UTC time in the desired ISO format: "yyyy-mm-ddThh:mm:ssZ" */
70  bu_vls_sprintf(vls_gmtime, "%04d-%02d-%02dT%02d:%02d:%02dZ",
71  loctime.tm_year + 1900,
72  loctime.tm_mon + 1,
73  loctime.tm_mday,
74  loctime.tm_hour,
75  loctime.tm_min,
76  loctime.tm_sec);
77 }
78 
79 
80 /*
81  * Local Variables:
82  * mode: C
83  * tab-width: 8
84  * indent-tabs-mode: t
85  * c-file-style: "stroustrup"
86  * End:
87  * ex: shiftwidth=4 tabstop=8
88  */
void bu_semaphore_acquire(unsigned int i)
Definition: semaphore.c:180
#define BU_SEM_DATETIME
Definition: parallel.h:184
Header file for the BRL-CAD common definitions.
#define BU_CK_VLS(_vp)
Definition: vls.h:69
void * memset(void *s, int c, size_t n)
void bu_vls_sprintf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:707
void bu_utctime(struct bu_vls *vls_gmtime, const int64_t time_val)
Definition: date-time.c:33
INT_64_T Time64_T
Definition: time64.h:24
struct TM * gmtime64(const Time64_T *ytime)
Definition: time64.c:878
void bu_semaphore_release(unsigned int i)
Definition: semaphore.c:218
Definition: vls.h:56