Add a datetime module.

This commit is contained in:
Jef Driesen 2010-02-01 21:19:55 +00:00
parent 098b229bc6
commit 54410dab8d
6 changed files with 187 additions and 0 deletions

View File

@ -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

View File

@ -192,6 +192,10 @@
RelativePath="..\src\cressi_edy.c"
>
</File>
<File
RelativePath="..\src\datetime.c"
>
</File>
<File
RelativePath="..\src\device.c"
>
@ -378,6 +382,10 @@
RelativePath="..\src\cressi_edy.h"
>
</File>
<File
RelativePath="..\src\datetime.h"
>
</File>
<File
RelativePath="..\src\device-private.h"
>

View File

@ -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 \

112
src/datetime.c Normal file
View File

@ -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 <time.h>
#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;
}

58
src/datetime.h Normal file
View File

@ -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 */

View File

@ -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