uves_time.c

00001 /* $Id: uves_time.c,v 1.2 2009/04/22 15:16:37 amodigli Exp $
00002  *
00003  * This file is part of the ESO UVES Library
00004  * Copyright (C) 2001-2004 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: amodigli $
00023  * $Date: 2009/04/22 15:16:37 $
00024  * $Revision: 1.2 $
00025  * $Name: uves-5_0_0 $
00026  */
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 /*-----------------------------------------------------------------------------
00031                                    Includes
00032  -----------------------------------------------------------------------------*/
00033 
00034 #include <stdio.h>
00035 #include <stdlib.h>
00036 #include <string.h>
00037 #include <time.h>
00038 #include <pwd.h>
00039 #include <unistd.h>
00040 #include <sys/time.h>
00041 
00042 #include "uves_time.h"
00043 #include "uves_globals.h"
00044 
00045 /*-----------------------------------------------------------------------------
00046                                    Macros
00047  -----------------------------------------------------------------------------*/
00048 
00049 /* Get century from a date in long format */
00050 #define GET_CENTURY(d)      (int) ( (d) / 1000000L)
00051 /* Get century year from a date in long format */
00052 #define GET_CCYEAR(d)       (int) ( (d) / 10000L)
00053 /* Get year from a date in long format */
00054 #define GET_YEAR(d)         (int) (((d) % 1000000L) / 10000L)
00055 /* Get month from a date in long format */
00056 #define GET_MONTH(d)        (int) (((d) % 10000L) / 100)
00057 /* Get day from a date in long format */
00058 #define GET_DAY(d)          (int) ( (d) % 100)
00059 
00060 /* Get hours from a date in long format */
00061 #define GET_HOUR(t)         (int) ( (t) / 1000000L)
00062 /* Get minutes from a date in long format */
00063 #define GET_MINUTE(t)       (int) (((t) % 1000000L) / 10000L)
00064 /* Get seconds from a date in long format */
00065 #define GET_SECOND(t)       (int) (((t) % 10000L) / 100)
00066 /* Get centi-seconds from a date in long format */
00067 #define GET_CENTI(t)        (int) ( (t) % 100)
00068 
00069 /* Make date in long format from its components */
00070 #define MAKE_DATE(c,y,m,d)  (long) (c) * 1000000L +                          \
00071                             (long) (y) * 10000L +                            \
00072                             (long) (m) * 100 + (d)
00073 /* Make time in long format from its components */
00074 #define MAKE_TIME(h,m,s,c)  (long) (h) * 1000000L +                          \
00075                             (long) (m) * 10000L +                            \
00076                             (long) (s) * 100 + (c)
00077 
00078 /*  Interval values, specified in centiseconds */
00079 #define INTERVAL_CENTI      1
00080 #define INTERVAL_SEC        100
00081 #define INTERVAL_MIN        6000
00082 #define INTERVAL_HOUR       360000L
00083 #define INTERVAL_DAY        8640000L
00084 
00085 /*-----------------------------------------------------------------------------
00086                             Private to this module
00087  -----------------------------------------------------------------------------*/
00088 
00089 static long timer_to_date(time_t time_secs) ;
00090 static long timer_to_time(time_t time_secs) ;
00091 static long uves_time_now(void) ;
00092 static long uves_date_now (void) ;
00093 
00094 /*----------------------------------------------------------------------------*/
00101 /*----------------------------------------------------------------------------*/
00104 /*-----------------------------------------------------------------------------
00105                               Function codes
00106  -----------------------------------------------------------------------------*/
00107 
00108 /*----------------------------------------------------------------------------*/
00117 /*----------------------------------------------------------------------------*/
00118 char * uves_get_datetime_iso8601(void)
00119 {
00120     static char date_iso8601[MAX_NAME_SIZE] ;
00121     long        curdate ;
00122     long        curtime ;
00123 
00124     curdate  = uves_date_now() ;
00125     curtime  = uves_time_now() ;
00126 
00127     snprintf(date_iso8601, MAX_NAME_SIZE-1,
00128             "%04d-%02d-%02dT%02d:%02d:%02d",
00129             GET_CCYEAR(curdate),
00130             GET_MONTH(curdate),
00131             GET_DAY(curdate),
00132             GET_HOUR(curtime),
00133             GET_MINUTE(curtime),
00134             GET_SECOND(curtime));
00135     return date_iso8601 ;
00136 }
00137 
00140 /*----------------------------------------------------------------------------*/
00152 /*----------------------------------------------------------------------------*/
00153 static long uves_date_now (void)
00154 {
00155     return (timer_to_date (time (NULL)));
00156 }
00157 
00158 /*----------------------------------------------------------------------------*/
00168 /*----------------------------------------------------------------------------*/
00169 static long uves_time_now(void)
00170 {
00171     struct timeval time_struct;
00172 
00173     gettimeofday (&time_struct, 0);
00174     return (timer_to_time (time_struct.tv_sec)
00175                          + time_struct.tv_usec / 10000);
00176 }
00177 
00178 /*----------------------------------------------------------------------------*/
00190 /*----------------------------------------------------------------------------*/
00191 static long timer_to_date(time_t time_secs)
00192 {
00193     struct tm *time_struct;
00194 
00195     if (time_secs == 0) {
00196         return 0;
00197     } else {
00198         /*  Convert into a long value CCYYMMDD */
00199         time_struct = localtime (&time_secs);
00200         if (time_struct) {
00201             time_struct-> tm_year += 1900;
00202             return (MAKE_DATE (    time_struct-> tm_year / 100,
00203                                 time_struct-> tm_year % 100,
00204                                 time_struct-> tm_mon + 1,
00205                                 time_struct-> tm_mday));
00206         } else {
00207             return (19700101);
00208         }
00209     }
00210 }
00211 
00212 /*----------------------------------------------------------------------------*/
00224 /*----------------------------------------------------------------------------*/
00225 static long timer_to_time(time_t time_secs)
00226 {
00227     struct tm *time_struct;
00228 
00229     if (time_secs == 0) {
00230         return 0;
00231     } else {
00232         /*  Convert into a long value HHMMSS00 */
00233         time_struct = localtime (&time_secs);
00234         if (time_struct) {
00235             return (MAKE_TIME (time_struct-> tm_hour,
00236                                time_struct-> tm_min,
00237                                time_struct-> tm_sec,
00238                                0));
00239         } else {
00240             return 0;
00241         }
00242     }
00243 }
00244 

Generated on 9 Mar 2012 for UVES Pipeline Reference Manual by  doxygen 1.6.1