File: parse_time.c

package info (click to toggle)
reiserfsprogs 1%3A3.6.27-3
  • links: PTS
  • area: main
  • in suites: buster
  • size: 3,148 kB
  • sloc: ansic: 27,337; sh: 4,143; makefile: 87
file content (39 lines) | stat: -rw-r--r-- 890 bytes parent folder | download | duplicates (4)
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
#define _XOPEN_SOURCE
#define _DEFAULT_SOURCE
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "parse_time.h"
#include "misc.h"
#include "reiserfs_lib.h"

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

time_t parse_time(char *str)
{
	struct tm ts;

	if (strcmp(str, "now") == 0) {
		return (time(NULL));
	}
	memset(&ts, 0, sizeof(ts));
#ifdef HAVE_STRPTIME
	strptime(str, "%Y%m%d%H%M%S", &ts);
#else
	sscanf(str, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
	       &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
	ts.tm_year -= 1900;
	ts.tm_mon -= 1;
	if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
	    ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
	    ts.tm_min > 59 || ts.tm_sec > 61)
		ts.tm_mday = 0;
#endif
	if (ts.tm_mday == 0)
		reiserfs_warning(stderr, "Couldn't parse date/time "
				 "specifier: %s", str);
	return (mktime(&ts));
}