summaryrefslogtreecommitdiff
path: root/gpsutils.c
diff options
context:
space:
mode:
authorEric S. Raymond <esr@thyrsus.com>2005-03-22 20:25:28 +0000
committerEric S. Raymond <esr@thyrsus.com>2005-03-22 20:25:28 +0000
commite78f332e63b8cf8292c4b19f3794ebee067a165f (patch)
tree311a71e23eaa612ed9d0cc1a98b98ab21977c780 /gpsutils.c
parent139b5e991313fe736cd735d271b6d439d761af85 (diff)
downloadgpsd-e78f332e63b8cf8292c4b19f3794ebee067a165f.tar.gz
Second step in library refactoring.
We need to make profiling work again, also gpsd.py is broken.
Diffstat (limited to 'gpsutils.c')
-rw-r--r--gpsutils.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/gpsutils.c b/gpsutils.c
new file mode 100644
index 00000000..878e9385
--- /dev/null
+++ b/gpsutils.c
@@ -0,0 +1,59 @@
+/* gpsutils.c -- code shared between low-level and high-level interfaces */
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <math.h>
+#include <string.h>
+#include <stdarg.h>
+#define __USE_XOPEN
+#include <time.h>
+
+#include "gpsd.h"
+
+double iso8661_to_unix(char *isotime)
+/* ISO8661 UTC to Unix local time */
+{
+ char *dp = NULL;
+ struct tm tm;
+ double usec, res;
+ time_t now;
+
+ dp = strptime(isotime, "%Y-%m-%dT%H:%M:%S", &tm);
+ if (*dp == '.')
+ usec = strtod(dp, NULL);
+ else
+ usec = 0;
+#ifdef HAVE_TIMEZONE
+ res = mktime(&tm) - timezone + usec;
+#else
+ res = mktime(&tm) - tm.tm_gmtoff + usec;
+#endif
+ now = time(NULL);
+#ifdef HAVE_DAYLIGHT
+ if (daylight && localtime_r(&now, &tm)->tm_isdst)
+ res -= 3600;
+#else
+ if (localtime_r(&now, &tm)->tm_isdst)
+ res -= 3600;
+#endif
+ return res;
+}
+
+char *unix_to_iso8661(double fixtime, char *isotime)
+/* Unix local time to ISO8661, no timezone adjustment */
+{
+ struct tm when;
+ double integral, fractional;
+ time_t intfixtime;
+ int slen;
+
+ fractional = modf(fixtime, &integral);
+ intfixtime = (time_t)integral;
+ localtime_r(&intfixtime, &when);
+
+ strftime(isotime, 28, "%Y-%m-%dT%H:%M:%S", &when);
+ slen = strlen(isotime);
+ sprintf(isotime + slen, "%.1f", fractional);
+ memcpy(isotime+slen, isotime+slen+1, strlen(isotime+slen+1));
+ return isotime;
+}