aboutsummaryrefslogtreecommitdiffstats
path: root/src/expire.c
blob: 68278ca5b71fad44c55b9299cc9fdb0f058c5b0c (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author José Bollo <jose.bollo@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <time.h>
#include <string.h>
#include <stdio.h>

static const int SEC = 1;
static const int MIN = 60;
static const int HOUR = 60*60;
static const int DAY = 24*60*60;
static const int WEEK = 7*24*60*60;
static const int YEAR = 365*24*60*60;

time_t txt2exp(const char *txt)
{
	time_t r, x;

	/* infinite time */
	if (!strcmp(txt, "always") || !strcmp(txt, "forever") || !strcmp(txt, "*"))
		return 0;

	/* parse */
	r = time(NULL);
	while(*txt) {
		x = 0;
		while('0' <= *txt && *txt <= '9')
			x = 10 * x + (time_t)(*txt++ - '0');
		switch(*txt) {
		case 'y': r += x * YEAR; txt++; break;
		case 'w': r += x * WEEK; txt++; break;
		case 'd': r += x * DAY; txt++; break;
		case 'h': r += x * HOUR; txt++; break;
		case 'm': r += x * MIN; txt++; break;
		case 's': txt++; /*@fallthrough@*/
		case 0: r += x * SEC; break;
		default: return -1;
		}
	}
	return r;
}

size_t exp2txt(time_t expire, char *buffer, size_t buflen)
{
	char b[100];
	size_t l;
	int n;

	if (!expire)
		strncpy(b, "forever", sizeof b);
	else {
		expire -= time(NULL);
		n = 0;
#define ADD(C,U) \
  if (expire >= U) { \
    n += snprintf(&b[n], sizeof b - n, "%lld" #C, (long long)(expire / U)); \
    expire %= U; \
  }
		ADD(y,YEAR)
		ADD(w,WEEK)
		ADD(d,DAY)
		ADD(h,HOUR)
		ADD(m,MIN)
		ADD(s,SEC)
#undef ADD
	}
	l = strlen(b);
	strncpy(buffer, b, buflen);
	return l;
}