aboutsummaryrefslogtreecommitdiffstats
path: root/timevaltostr.c
blob: 702b46bf88569258a095f9817315367eb8b4a249 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* See LICENSE file for copyright and license details. */
#include "libsimple.h"
#ifndef TEST


char *
libsimple_timevaltostr(char *restrict buf, const struct timeval *restrict tv)
{
	time_t s = tv->tv_sec;
	long int us = tv->tv_usec;
	char sign[2] = "+";

	if (!buf) {
		buf = malloc(INTSTRLEN(time_t) + sizeof("-.999999"));
		if (!buf)
			return NULL;
	}

	if (tv->tv_usec < 0 || tv->tv_usec >= 1000000L) {
		errno = EINVAL;
		return NULL;
	}

	if (s == TIME_MIN && !us) {
		sprintf(buf, "%lli.000000", (long long int)s);
		return buf;
	}

	if (s < 0) {
		s = -s;
		*sign = '-';
		if (us) {
			s -= 1;
			us = 1000000L - us;
		}
	}

	sprintf(buf, "%s%lli.%06li", sign, (long long int)s, us);
	return buf;
}


#else
#include "test.h"

int
main(void)
{
	char buf[1000];

	/* Mostly tested in libsimple.c */

	errno = 0;
	assert(!libsimple_timevaltostr(buf, &(struct timeval){.tv_sec = 0, .tv_usec = -1}) && errno == EINVAL);
	errno = 0;
	assert(!libsimple_timevaltostr(buf, &(struct timeval){.tv_sec = 0, .tv_usec = -2}) && errno == EINVAL);
	errno = 0;
	assert(!libsimple_timevaltostr(buf, &(struct timeval){.tv_sec = 0, .tv_usec = 1000000L}) && errno == EINVAL);
	errno = 0;
	assert(!libsimple_timevaltostr(buf, &(struct timeval){.tv_sec = 0, .tv_usec = 1000001L}) && errno == EINVAL);

	return 0;
}

#endif