This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuncs.h
262 lines (191 loc) · 5.53 KB
/
funcs.h
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
funcs.h
This file is part of JDate project.
JDate is a persian jalali calendar date tool for GNU command line.
JDate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JDate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _FUNCS_H
#define _FUNCS_H
void print_error (char *error_msg, int error_code)
{
fprintf(stderr, "Error %02d: %s\n", error_code, error_msg);
exit(EXIT_FAILURE);
}
jDate get_cur_gdate (void)
{
jDate gd;
gd.day = malloc(sizeof(char*));
gd.month = malloc(sizeof(char*));
gd.year = malloc(sizeof(char*));
time_t rawtime = time(NULL);
strftime(gd.day, sizeof(gd.day), "%d", localtime(&rawtime));
strftime(gd.month, sizeof(gd.month), "%m", localtime(&rawtime));
strftime(gd.year, sizeof(gd.year), "%Y", localtime(&rawtime));
return gd;
}
jDate get_cur_jdate (void)
{
jDate jd;
jd.day = malloc(sizeof(char*));
jd.month = malloc(sizeof(char*));
jd.year = malloc(sizeof(char*));
jd = get_cur_gdate();
return gregorian_to_jalali(atoi(jd.year), atoi(jd.month), atoi(jd.day));
}
void print_jdate (jDate jd, char *fmt)
{
int l = 0, i = 0, day_of_year = 0, weekday = 0, week_number = 0;
char *buf = malloc(sizeof(char) * 4096);
buf[0] = '\0';
day_of_year = 0;
for (i = 0; i < atoi(jd.month) - 1; i++) {
day_of_year += j_days_in_month[i];
}
day_of_year += atoi(jd.day);
weekday = (day_of_year % 7) + 1;
week_number = _div(day_of_year, 7) + 1;
while (l < strlen(fmt)) {
if (fmt[l] == '%') {
switch (fmt[l+1]) {
case 'a':
buf = _strconcat(buf, persian_weekday_abbreviation_name(weekday));
break;
case 'A':
buf = _strconcat(buf, persian_weekday_name(weekday));
break;
case 'B':
buf = _strconcat(buf, persian_month_name(atoi(jd.month)));
break;
case 'd':
buf = _strconcat(buf, jd.day);
break;
case 'D':
if (atoi(jd.day) < 10) {
buf = _append(buf, '0');
}
buf = _strconcat(buf, jd.day);
break;
case 'm':
buf = _strconcat(buf, jd.month);
break;
case 'M':
if (atoi(jd.month) < 10) {
buf = _append(buf, '0');
}
buf = _strconcat(buf, jd.month);
break;
case 'y':
if (atoi(jd.year) < 10) {
buf = _append(buf, '0');
}
buf = _strconcat(buf, _substring(jd.year, 2, 2));
break;
case 'Y':
buf = _strconcat(buf, jd.year);
break;
case 'j':
buf = _strconcat(buf, _inttostr(day_of_year));
break;
case 'u':
buf = _strconcat(buf, _inttostr(weekday));
break;
case 'U':
buf = _strconcat(buf, _inttostr(week_number));
break;
default:
buf = _append(buf, fmt[l]);
break;
}
l++;
} else {
buf = _append(buf, fmt[l]);
}
l++;
}
puts(buf);
}
jDate compare_jdate (char *str)
{
jDate j;
int tmp_year1, tmp_year2,
tmp_month1, tmp_month2,
tmp_day1, tmp_day2;
boolean compare_condition = false;
tmp_year1 = atoi(_substring(str, 0, 4));
tmp_month1 = atoi(_substring(str, 5, 2));
tmp_day1 = atoi(_substring(str, 8, 2));
tmp_year2 = atoi(_substring(str, 11, 4));
tmp_month2 = atoi(_substring(str, 16, 2));
tmp_day2 = atoi(_substring(str, 19, 2));
if (tmp_year1 == tmp_year2) {
if (tmp_month1 == tmp_month2) {
if (tmp_day1 == tmp_day2) {
compare_condition = true;
} else if (tmp_day1 > tmp_day2) {
compare_condition = true;
} else if (tmp_day1 < tmp_day2) {
compare_condition = false;
}
} else if (tmp_month1 > tmp_month2) {
compare_condition = true;
} else if (tmp_month1 < tmp_month2) {
compare_condition = false;
}
} else if (tmp_year1 > tmp_year2) {
compare_condition = true;
} else if (tmp_year1 < tmp_year2) {
compare_condition = false;
}
if (compare_condition) {
j.year = _inttostr(tmp_year1);
j.month = _inttostr(tmp_month1);
j.day = _inttostr(tmp_day1);
} else {
j.year = _inttostr(tmp_year2);
j.month = _inttostr(tmp_month2);
j.day = _inttostr(tmp_day2);
}
return j;
}
int jdate_difference (char *str)
{
jDate jd;
int tmp_year1, tmp_year2,
tmp_month1, tmp_month2,
tmp_day1, tmp_day2;
tmp_year1 = atoi(_substring(str, 0, 4));
tmp_month1 = atoi(_substring(str, 5, 2));
tmp_day1 = atoi(_substring(str, 8, 2));
tmp_year2 = atoi(_substring(str, 11, 4));
tmp_month2 = atoi(_substring(str, 16, 2));
tmp_day2 = atoi(_substring(str, 19, 2));
jd.year = _inttostr(tmp_year1);
jd.month = _inttostr(tmp_month1);
jd.day = _inttostr(tmp_day1);
//TODO
return 0;
}
jDate file_modification_date (char *filename)
{
jDate jd;
jd.day = malloc(sizeof(char*));
jd.month = malloc(sizeof(char*));
jd.year = malloc(sizeof(char*));
struct tm *foo;
struct stat attrib;
stat(filename, &attrib);
strftime(jd.day, sizeof(jd.day), "%d", localtime(&(attrib.st_ctime)));
strftime(jd.month, sizeof(jd.month), "%m", localtime(&(attrib.st_ctime)));
strftime(jd.year, sizeof(jd.year), "%Y", localtime(&(attrib.st_ctime)));
return gregorian_to_jalali(atoi(jd.year), atoi(jd.month), atoi(jd.day));
}
#endif //_FUNCS_H