From 54410dab8dc0965797e44c05a2fd3e50ec939d0d Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Mon, 1 Feb 2010 21:19:55 +0000 Subject: [PATCH] Add a datetime module. --- configure.ac | 3 + msvc/libdivecomputer.vcproj | 8 +++ src/Makefile.am | 2 + src/datetime.c | 112 ++++++++++++++++++++++++++++++++++++ src/datetime.h | 58 +++++++++++++++++++ src/libdivecomputer.symbols | 4 ++ 6 files changed, 187 insertions(+) create mode 100644 src/datetime.c create mode 100644 src/datetime.h diff --git a/configure.ac b/configure.ac index 34cba11..a9c4005 100644 --- a/configure.ac +++ b/configure.ac @@ -38,6 +38,9 @@ AM_CONDITIONAL([IRDA], [test "$irda_win32" = "yes" || test "$irda_linux" = "yes" AM_CONDITIONAL([IRDA_WIN32], [test "$irda_win32" = "yes"]) AM_CONDITIONAL([IRDA_LINUX], [test "$irda_linux" = "yes"]) +# Checks for library functions. +AC_CHECK_FUNCS([localtime_r gmtime_r]) + AC_CONFIG_HEADERS([config.h]) AC_CONFIG_FILES([ libdivecomputer.pc diff --git a/msvc/libdivecomputer.vcproj b/msvc/libdivecomputer.vcproj index 1d62dae..3c46f11 100644 --- a/msvc/libdivecomputer.vcproj +++ b/msvc/libdivecomputer.vcproj @@ -192,6 +192,10 @@ RelativePath="..\src\cressi_edy.c" > + + @@ -378,6 +382,10 @@ RelativePath="..\src\cressi_edy.h" > + + diff --git a/src/Makefile.am b/src/Makefile.am index b8d6720..ae847b1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,6 +8,7 @@ libdivecomputer_HEADERS = \ buffer.h \ device.h \ parser.h \ + datetime.h \ units.h \ suunto.h \ suunto_solution.h \ @@ -48,6 +49,7 @@ libdivecomputer_la_LDFLAGS = \ libdivecomputer_la_SOURCES = \ device.h device-private.h device.c \ parser.h parser-private.h parser.c \ + datetime.h datetime.c \ suunto.h \ suunto_common.h suunto_common.c \ suunto_common2.h suunto_common2.c \ diff --git a/src/datetime.c b/src/datetime.c new file mode 100644 index 0000000..619051c --- /dev/null +++ b/src/datetime.c @@ -0,0 +1,112 @@ +/* + * libdivecomputer + * + * Copyright (C) 2010 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "datetime.h" + +static struct tm * +dc_localtime_r (const time_t *t, struct tm *tm) +{ +#ifdef HAVE_LOCALTIME_R + return localtime_r (t, tm); +#else + struct tm *p = localtime (t); + if (p == NULL) + return NULL; + + if (tm) + *tm = *p; + + return tm; +#endif +} + +static struct tm * +dc_gmtime_r (const time_t *t, struct tm *tm) +{ +#ifdef HAVE_GMTIME_R + return gmtime_r (t, tm); +#else + struct tm *p = gmtime (t); + if (p == NULL) + return NULL; + + if (tm) + *tm = *p; + + return tm; +#endif +} + +dc_ticks_t +dc_datetime_now (void) +{ + return time (NULL); +} + +dc_datetime_t * +dc_datetime_localtime (dc_datetime_t *result, + dc_ticks_t ticks) +{ + time_t t = ticks; + + struct tm tm; + if (dc_localtime_r (&t, &tm) == NULL) + return NULL; + + if (result) { + result->year = tm.tm_year + 1900; + result->month = tm.tm_mon + 1; + result->day = tm.tm_mday; + result->hour = tm.tm_hour; + result->minute = tm.tm_min; + result->second = tm.tm_sec; + } + + return result; +} + +dc_datetime_t * +dc_datetime_gmtime (dc_datetime_t *result, + dc_ticks_t ticks) +{ + time_t t = ticks; + + struct tm tm; + if (dc_gmtime_r (&t, &tm) == NULL) + return NULL; + + if (result) { + result->year = tm.tm_year + 1900; + result->month = tm.tm_mon + 1; + result->day = tm.tm_mday; + result->hour = tm.tm_hour; + result->minute = tm.tm_min; + result->second = tm.tm_sec; + } + + return result; +} diff --git a/src/datetime.h b/src/datetime.h new file mode 100644 index 0000000..ba4dcfd --- /dev/null +++ b/src/datetime.h @@ -0,0 +1,58 @@ +/* + * libdivecomputer + * + * Copyright (C) 2010 Jef Driesen + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifndef DC_DATETIME_H +#define DC_DATETIME_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifdef _WIN32 +typedef __int64 dc_ticks_t; +#else +typedef long long int dc_ticks_t; +#endif + +typedef struct dc_datetime_t { + int year; + int month; + int day; + int hour; + int minute; + int second; +} dc_datetime_t; + +dc_ticks_t +dc_datetime_now (void); + +dc_datetime_t * +dc_datetime_localtime (dc_datetime_t *result, + dc_ticks_t ticks); + +dc_datetime_t * +dc_datetime_gmtime (dc_datetime_t *result, + dc_ticks_t ticks); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* DC_DATETIME_H */ diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 0df2290..450c1b9 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -9,6 +9,10 @@ dc_buffer_slice dc_buffer_get_size dc_buffer_get_data +dc_datetime_now +dc_datetime_localtime +dc_datetime_gmtime + parser_get_type parser_set_data parser_samples_foreach