uves_time.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 # include <config.h>
00029 #endif
00030
00031
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
00047
00048
00049
00050 #define GET_CENTURY(d) (int) ( (d) / 1000000L)
00051
00052 #define GET_CCYEAR(d) (int) ( (d) / 10000L)
00053
00054 #define GET_YEAR(d) (int) (((d) % 1000000L) / 10000L)
00055
00056 #define GET_MONTH(d) (int) (((d) % 10000L) / 100)
00057
00058 #define GET_DAY(d) (int) ( (d) % 100)
00059
00060
00061 #define GET_HOUR(t) (int) ( (t) / 1000000L)
00062
00063 #define GET_MINUTE(t) (int) (((t) % 1000000L) / 10000L)
00064
00065 #define GET_SECOND(t) (int) (((t) % 10000L) / 100)
00066
00067 #define GET_CENTI(t) (int) ( (t) % 100)
00068
00069
00070 #define MAKE_DATE(c,y,m,d) (long) (c) * 1000000L + \
00071 (long) (y) * 10000L + \
00072 (long) (m) * 100 + (d)
00073
00074 #define MAKE_TIME(h,m,s,c) (long) (h) * 1000000L + \
00075 (long) (m) * 10000L + \
00076 (long) (s) * 100 + (c)
00077
00078
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
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
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
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
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