-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_arg.c
106 lines (82 loc) · 2.42 KB
/
time_arg.c
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
95
96
97
98
99
100
101
102
103
104
105
106
#include "time_arg.h"
time_spec_t *seconds_to_time_spec( long num )
{ long tmp; // temprory variable for holdig calculations
time_spec_t *temp = malloc(sizeof(time_spec_t));
if(num>=0)//dont execute if seconds are negative
{ /* in this the number fo seconds is divide by firstly by
year/month/day/hour/min and the
resultant is then multiplied againg with
year/month/day/hour/min and subtract the num to obtain the
resultant seconds and store the Numerator in time spec*/
if (num>=year)
{
tmp=(num/year);
temp->y=tmp;
num = num-year*(tmp);
}
if (num>=month)
{
tmp=(num/month);
temp->n=tmp;
num =num - month*(tmp) ;
}
if(num>=day)
{
tmp=(num/day);
temp->d=tmp;
num =num - day*(tmp);
}
if(num>=hour)
{
tmp=(num/hour);
temp->h=tmp;
num =num - hour*(tmp);
}
if(num>=min)
{
tmp=(num/min);
temp->m=tmp;
num = num -min*(tmp);
}
if(num<min)
{temp->s=num;
}
return temp;
}
else return NULL;
}
num_seconds_t time_spec_to_seconds( time_spec_t *temp)
{ num_seconds_t num ,y,n,d,h,m,s;
y=temp->y;
n=temp->n;
d=temp->d;
h=temp->h;
m=temp->m;
s=temp->s;
num = (((y)*year)+((n)*month)+((d)*day)+
((h)*hour)+((m)*min)+((s)*1));
return num ;
}
bool get_time_arg( char *in_str, num_seconds_t *seconds_p )
{
if(is_legal_time_spec(in_str)!=0)
{
time_spec1 = string_to_time_spec( in_str);
num_sec1= time_spec_to_seconds(time_spec1);
seconds_p=&num_sec1;
if(valid_seconds_gen(num_sec1)!=0)
{ strcpy( format, "%" );
strcat( format, PRId64 );
printf(format,*seconds_p);//print the num of seconds
printf("\n");}
else
fprintf(stderr, "%s", "error: time range exceeded\n");
return 1;
}
else return 0;
}
int valid_seconds_gen(long int num)
{ if(num<Max&&num>=0)
return 1;
else return 0;
}