File: parse_date.c

package info (click to toggle)
hodie 1.4-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 144 kB
  • ctags: 104
  • sloc: ansic: 1,132; makefile: 75
file content (82 lines) | stat: -rw-r--r-- 1,442 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
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
#include "includes.h"

#define YMD 0
#define VERB 1
#define DMY 2
#define MDY 3

int
parse_date(int argc, char** argv, int pos, struct tm* p_ts)
{
    int i,ret;
    char flag,tmp1,tmp2;

    i=pos; i++;
    if(i>=argc)
    {
	print_usage();
	exit(1);
    }
    if(strcmp(argv[i],"ymd")==0)
    {
	flag=YMD;
	ret=pos+2;
    }
    else if(strcmp(argv[i],"verbose")==0)
    {	
	flag=VERB;
	ret=pos+7;
    }
    else if(strcmp(argv[i],"dmy")==0)
    {
	flag=DMY;
	ret=pos+2;
    }
    else if(strcmp(argv[i],"mdy")==0)
    {	
	flag=MDY;
	ret=pos+2;
    }
    else
    {
	print_usage();
	exit(1);
    }

    i++;
    
    switch(flag)
    {
    case YMD:
	sscanf(argv[i],"%d%c%d%c%d",&(p_ts->tm_year),&tmp1,
	       &(p_ts->tm_mon),&tmp2,&(p_ts->tm_mday));
	break;
    case VERB:
	for(;i<argc;i++)
	{
	    if(strcmp(argv[i],"--year")==0 || strcmp(argv[i],"-y")==0)
	    {
		p_ts->tm_year=atoi(argv[i+1]);
	    }
	    else if(strcmp(argv[i],"--month")==0 || strcmp(argv[i],"-m")==0)
	    {
		p_ts->tm_mon=atoi(argv[i+1]);
	    }
	    else if(strcmp(argv[i],"--day")==0 || strcmp(argv[i],"-d")==0)
	    {
		p_ts->tm_mday=atoi(argv[i+1]);
	    }
	}
	break;
    case DMY:
	sscanf(argv[i],"%d%c%d%c%d",&(p_ts->tm_mday),&tmp1,
	       &(p_ts->tm_mon),&tmp2,&(p_ts->tm_year));
	break;
    case MDY:
	sscanf(argv[i],"%d%c%d%c%d",&(p_ts->tm_mon),&tmp1,
	       &(p_ts->tm_mday),&tmp2,&(p_ts->tm_year));
	break;
    }
    p_ts->tm_mon--;
    return ret;
}