aboutsummaryrefslogtreecommitdiffstats
path: root/libgeome_get_from_time.c
blob: b512a346b48e80ded5e5f1bfba0a6fd1126e0349 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* See LICENSE file for copyright and license details. */
#include "common.h"


int
libgeome_get_from_time(struct libgeome_context *ctx, struct libgeome_data *out)
{
	time_t utc, local, uoff;
	struct tm tm;

	utc = time(NULL);
	if (utc < 0) {
		ctx->print_error(ctx, "time: %s\n", strerror(errno));
		return -1;
        }
        if (!localtime_r(&utc, &tm)) {
		ctx->print_error(ctx, "localtime_r: %s\n", strerror(errno));
                return -1;
        }

	local = (time_t)(tm.tm_hour * 60 * 60);
	local += (time_t)(tm.tm_min * 60);
	local += (time_t)tm.tm_sec;
	utc %= (time_t)(24 * 60 * 60);
	uoff = local - utc;

	out->requested_data &= LIBGEOME_DATUM_LONGITUDE;
	if (out->requested_data) {
		out->longitude = (double)uoff / (24 * 60 * 60 / 360);
		out->longitude = fmod(out->longitude, 360);
		if (out->longitude < -180)
			out->longitude += 360;
		else if (out->longitude > 180)
			out->longitude -= 360;
	}

	return 0;
}