00001 /* 00002 Copyright 2016 Otavio Rodolfo Piske 00003 00004 Licensed under the Apache License, Version 2.0 (the "License"); 00005 you may not use this file except in compliance with the License. 00006 You may obtain a copy of the License at 00007 00008 http://www.apache.org/licenses/LICENSE-2.0 00009 00010 Unless required by applicable law or agreed to in writing, software 00011 distributed under the License is distributed on an "AS IS" BASIS, 00012 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 See the License for the specific language governing permissions and 00014 limitations under the License. 00015 */ 00016 #ifndef GRU_TIME_UTILS_H 00017 #define GRU_TIME_UTILS_H 00018 00019 #include <ctype.h> 00020 #include <inttypes.h> 00021 #include <math.h> 00022 #include <stdint.h> 00023 #include <stdlib.h> 00024 #include <time.h> 00025 00026 #if !defined(_WIN32) && !defined(_WIN64) 00027 #include <sys/time.h> 00028 #include <unistd.h> 00029 #else 00030 #include <windows.h> 00031 00032 #include "time/other/gru_time_portable.h" 00033 #endif 00034 00035 #include "common/gru_alloc.h" 00036 #include "common/gru_portable.h" 00037 #include "gru_localtime.h" 00038 00039 #ifdef __cplusplus 00040 extern "C" { 00041 #endif 00042 00044 #define GRU_TS_STRING_SEC_MAX 16 00045 00047 #define GRU_TS_STRING_USEC_MAX 7 00048 00050 #define GRU_TS_STRING_MAX GRU_TS_STRING_SEC_MAX + GRU_TS_STRING_USEC_MAX + 1 00051 00055 typedef struct timeval gru_timestamp_t; 00056 00062 gru_export void gru_time_add_microseconds(gru_timestamp_t *t, uint64_t count); 00063 00069 gru_export void gru_time_add_seconds(gru_timestamp_t *t, uint64_t count); 00070 00076 gru_export void gru_time_add_minutes(gru_timestamp_t *t, uint64_t count); 00077 00083 gru_export void gru_time_add_hours(gru_timestamp_t *t, uint64_t count); 00084 00090 gru_export void gru_time_add_days(gru_timestamp_t *t, uint64_t count); 00091 00100 gru_export gru_timestamp_t gru_time_read_str(const char *str); 00101 00107 gru_export char *gru_time_write_str(const gru_timestamp_t *t); 00108 00109 00118 gru_export char *gru_time_write_format(const gru_timestamp_t *t, 00119 const char *format, 00120 gru_status_t *status); 00121 00126 gru_export gru_timestamp_t gru_time_now(); 00127 00132 gru_export uint64_t gru_time_now_milli(); 00133 00140 gru_export gru_timestamp_t gru_time_from_milli_char(const char *ts); 00141 00147 gru_export gru_timestamp_t gru_time_from_milli(int64_t timestamp); 00148 00154 gru_export int64_t gru_time_to_milli(const gru_timestamp_t *ts); 00155 00163 gru_export int64_t gru_time_elapsed_secs(gru_timestamp_t start, gru_timestamp_t end); 00164 00165 00173 gru_export int64_t gru_time_elapsed_milli(gru_timestamp_t start, gru_timestamp_t end); 00174 00175 #ifdef __cplusplus 00176 } 00177 #endif 00178 00179 #endif /* GRU_TIME_UTILS_H */