SINFONI Pipeline Reference Manual  2.6.0
sinfo_time.c
1 /* $Id: sinfo_time.c,v 1.5 2012-03-03 10:17:31 amodigli Exp $
2  *
3  * This file is part of the ESO QFITS Library
4  * Copyright (C) 2001-2004 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: amodigli $
23  * $Date: 2012-03-03 10:17:31 $
24  * $Revision: 1.5 $
25  * $Name: not supported by cvs2svn $
26  */
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30 /*-----------------------------------------------------------------------------
31  Includes
32  -----------------------------------------------------------------------------*/
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <pwd.h>
39 #include <unistd.h>
40 #include <sys/time.h>
41 
42 #include "sinfo_time.h"
43 #include "sinfo_globals.h"
44 
45 /*-----------------------------------------------------------------------------
46  Macros
47  -----------------------------------------------------------------------------*/
48 
49 /* Get century from a date in long format */
50 #define GET_CENTURY(d) (int) ( (d) / 1000000L)
51 /* Get century year from a date in long format */
52 #define GET_CCYEAR(d) (int) ( (d) / 10000L)
53 /* Get year from a date in long format */
54 #define GET_YEAR(d) (int) (((d) % 1000000L) / 10000L)
55 /* Get month from a date in long format */
56 #define GET_MONTH(d) (int) (((d) % 10000L) / 100)
57 /* Get day from a date in long format */
58 #define GET_DAY(d) (int) ( (d) % 100)
59 
60 /* Get hours from a date in long format */
61 #define GET_HOUR(t) (int) ( (t) / 1000000L)
62 /* Get minutes from a date in long format */
63 #define GET_MINUTE(t) (int) (((t) % 1000000L) / 10000L)
64 /* Get seconds from a date in long format */
65 #define GET_SECOND(t) (int) (((t) % 10000L) / 100)
66 /* Get centi-seconds from a date in long format */
67 #define GET_CENTI(t) (int) ( (t) % 100)
68 
69 /* Make date in long format from its components */
70 #define MAKE_DATE(c,y,m,d) (long) (c) * 1000000L + \
71  (long) (y) * 10000L + \
72  (long) (m) * 100 + (d)
73 /* Make time in long format from its components */
74 #define MAKE_TIME(h,m,s,c) (long) (h) * 1000000L + \
75  (long) (m) * 10000L + \
76  (long) (s) * 100 + (c)
77 
78 /* Interval values, specified in centiseconds */
79 #define INTERVAL_CENTI 1
80 #define INTERVAL_SEC 100
81 #define INTERVAL_MIN 6000
82 #define INTERVAL_HOUR 360000L
83 #define INTERVAL_DAY 8640000L
84 
85 /*-----------------------------------------------------------------------------
86  Private to this module
87  -----------------------------------------------------------------------------*/
88 
89 static long timer_to_date(time_t time_secs) ;
90 static long timer_to_time(time_t time_secs) ;
91 static long sinfo_time_now(void) ;
92 static long sinfo_date_now (void) ;
93 
95 /*----------------------------------------------------------------------------*/
102 /*----------------------------------------------------------------------------*/
103 
104 /*-----------------------------------------------------------------------------
105  Function codes
106  -----------------------------------------------------------------------------*/
107 
108 /*----------------------------------------------------------------------------*/
117 /*----------------------------------------------------------------------------*/
118 char * sinfo_get_datetime_iso8601(void)
119 {
120  static char date_iso8601[MAX_NAME_SIZE] ;
121  long curdate ;
122  long curtime ;
123 
124  curdate = sinfo_date_now() ;
125  curtime = sinfo_time_now() ;
126 
127  snprintf(date_iso8601, MAX_NAME_SIZE-1,
128  "%04d-%02d-%02dT%02d:%02d:%02d",
129  GET_CCYEAR(curdate),
130  GET_MONTH(curdate),
131  GET_DAY(curdate),
132  GET_HOUR(curtime),
133  GET_MINUTE(curtime),
134  GET_SECOND(curtime));
135  return date_iso8601 ;
136 }
137 
140 /*----------------------------------------------------------------------------*/
152 /*----------------------------------------------------------------------------*/
153 static long sinfo_date_now (void)
154 {
155  return (timer_to_date (time (NULL)));
156 }
157 
158 /*----------------------------------------------------------------------------*/
168 /*----------------------------------------------------------------------------*/
169 static long sinfo_time_now(void)
170 {
171  struct timeval time_struct;
172 
173  gettimeofday (&time_struct, 0);
174  return (timer_to_time (time_struct.tv_sec)
175  + time_struct.tv_usec / 10000);
176 }
177 
178 /*----------------------------------------------------------------------------*/
190 /*----------------------------------------------------------------------------*/
191 static long timer_to_date(time_t time_secs)
192 {
193  struct tm *time_struct;
194 
195  if (time_secs == 0) {
196  return 0;
197  } else {
198  /* Convert into a long value CCYYMMDD */
199  time_struct = localtime (&time_secs);
200  if (time_struct) {
201  time_struct-> tm_year += 1900;
202  return (MAKE_DATE ( time_struct-> tm_year / 100,
203  time_struct-> tm_year % 100,
204  time_struct-> tm_mon + 1,
205  time_struct-> tm_mday));
206  } else {
207  return (19700101);
208  }
209  }
210 }
211 
212 /*----------------------------------------------------------------------------*/
224 /*----------------------------------------------------------------------------*/
225 static long timer_to_time(time_t time_secs)
226 {
227  struct tm *time_struct;
228 
229  if (time_secs == 0) {
230  return 0;
231  } else {
232  /* Convert into a long value HHMMSS00 */
233  time_struct = localtime (&time_secs);
234  if (time_struct) {
235  return (MAKE_TIME (time_struct-> tm_hour,
236  time_struct-> tm_min,
237  time_struct-> tm_sec,
238  0));
239  } else {
240  return 0;
241  }
242  }
243 }
244