~arivigo/schain

ref: f216bd5a36501cd6440dad3cce050c79a04b3ffc schain/date.c -rw-r--r-- 1.6 KiB
f216bd5aAriadna Vigo Makefile: removed syntax not defined by POSIX 7 months ago
                                                                                
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
85
86
87
88
89
90
91
92
93
94
/* See LICENSE file for copyright and license details. */

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

#include "date.h"
#include "strlcpy.h"

const char *
date_str(time_t date)
{
	static char static_date[DATE_SIZE];
	struct tm *time_info;

	time_info = localtime(&date);

	sprintf(static_date, "%04d-%02d-%02d", time_info->tm_year + 1900,
	        time_info->tm_mon + 1, time_info->tm_mday);

	return static_date;
}

int
is_date(const char *str)
{
	char copy[DATE_SIZE];
	char *ptr, *endptr;
	int ybuf, mbuf, dbuf, leap;

	strlcpy(copy, str, DATE_SIZE);

	/* Year */

	if ((ptr = strtok(copy, "-")) == NULL)
		return -1;

	if (strlen(ptr) != 4)
		return -1;

	ybuf = strtol(ptr, &endptr, 10);
	if (endptr[0] != '\0')
		return -1;

	if (ybuf % 400 == 0)
		leap = 1;
	else if (ybuf % 4 == 0 && ybuf % 100 != 0)
		leap = 1;
	else
		leap = 0;

	/* Month */

	if ((ptr = strtok(NULL, "-")) == NULL)
		return -1;

	if (strlen(ptr) != 2)
		return -1;

	mbuf = strtol(ptr, &endptr, 10);
	if (endptr[0] != '\0')
		return -1;

	if (mbuf < 1 || mbuf > 12)
		return -1;

	/* Day */

	if ((ptr = strtok(NULL, "")) == NULL)
		return -1;

	if (strlen(ptr) != 2)
		return -1;

	dbuf = strtol(ptr, &endptr, 10);
	if (endptr[0] != '\0')
		return -1;

	if (dbuf < 0)
		return -1;
	if (leap > 0 && mbuf == 2 && dbuf == 29)
		return 0;
	else if (leap == 0 && mbuf == 2 && dbuf > 28)
		return -1;
	else if ((mbuf == 1 || mbuf == 3 || mbuf == 5 || mbuf == 7 || mbuf == 8
	          || mbuf == 10 || mbuf == 12) && dbuf > 31)
		return -1;
	else if ((mbuf == 4 || mbuf == 6 || mbuf == 9 || mbuf == 11)
	         && dbuf > 30)
		return -1;
	else
		return 0;
}