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 pathjdate.h
263 lines (209 loc) · 4.36 KB
/
jdate.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
262
/*
jdate.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 _JDATE_H
#define _JDATE_H
#define PROGRAM_NAME "jdate"
#define VERSION "0.0.1"
typedef struct
{
char *year,
*month,
*day;
//TODO
//*hour,
//*minute,
//*seconds;
}jDate;
int g_days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int j_days_in_month[] = {31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29};
char *persian_weekday_name (int weekday)
{
char *buf;
switch (weekday) {
case 1:
buf = "شنبه";
break;
case 2:
buf = "یکشنبه";
break;
case 3:
buf = "دوشنبه";
break;
case 4:
buf = "سه شنبه";
break;
case 5:
buf = "چهار شنبه";
break;
case 6:
buf = "پنجشنبه";
break;
case 7:
buf = "جمعه";
break;
}
return buf;
}
char *persian_weekday_abbreviation_name (int weekday)
{
char *buf;
switch (weekday) {
case 1:
buf = "ش";
break;
case 2:
buf = "ی";
break;
case 3:
buf = "د";
break;
case 4:
buf = "س";
break;
case 5:
buf = "چ";
break;
case 6:
buf = "پ";
break;
case 7:
buf = "ج";
break;
}
return buf + '\0';
}
char *persian_month_name (int month)
{
char *buf;
switch (month) {
case 1:
buf = "فروردین";
break;
case 2:
buf = "اردیبهشت";
break;
case 3:
buf = "خرداد";
break;
case 4:
buf = "مرداد";
break;
case 5:
buf = "تیر";
break;
case 6:
buf = "شهریور";
break;
case 7:
buf = "مهر";
break;
case 8:
buf = "آبان";
break;
case 9:
buf = "آذر";
break;
case 10:
buf = "دی";
break;
case 11:
buf = "بهمن";
break;
case 12:
buf = "اسفند";
break;
}
return buf;
}
jDate jalali_to_gregorian(int j_y, int j_m, int j_d)
{
int i;
int jy = j_y - 979;
int jm = j_m - 1;
int jd = j_d - 1;
int j_day_no = 365 * jy + _div(jy, 33) * 8 + _div(jy % 33 + 3, 4);
for (i = 0; i < jm; ++i) {
j_day_no += j_days_in_month[i];
}
j_day_no += jd;
int g_day_no = j_day_no + 79;
int gy = 1600 + 400 * _div(g_day_no, 146097);
g_day_no = g_day_no % 146097;
int leap = true;
if (g_day_no >= 36525) {
g_day_no--;
gy += 100 * _div(g_day_no, 36524);
g_day_no = g_day_no % 36524;
if (g_day_no >= 365) {
g_day_no++;
} else {
leap = false;
}
}
gy += 4 * _div(g_day_no, 1461);
g_day_no %= 1461;
if (g_day_no >= 366) {
leap = false;
g_day_no--;
gy += _div(g_day_no, 365);
g_day_no = g_day_no % 365;
}
for (i = 0; g_day_no >= g_days_in_month[i] + (i == 1 && leap); i++) {
g_day_no -= g_days_in_month[i] + (i == 1 && leap);
}
int gm = i + 1;
int gd = g_day_no + 1;
jDate jdate;
jdate.year = _inttostr(gy);
jdate.month = _inttostr(gm);
jdate.day = _inttostr(gd);
return jdate;
}
jDate gregorian_to_jalali (int g_y, int g_m, int g_d)
{
int i;
int gy = g_y - 1600;
int gm = g_m - 1;
int gd = g_d - 1;
int g_day_no = 365 * gy + _div(gy + 3, 4) - _div(gy + 99, 100) + _div(gy + 399, 400);
for (i=0; i < gm; ++i) {
g_day_no += g_days_in_month[i];
}
if (gm > 1 && ((gy % 4 == 0 && gy % 100 != 0) || (gy % 400 == 0))) {
g_day_no++;
}
g_day_no += gd;
int j_day_no = g_day_no - 79;
int j_np = _div(j_day_no, 12053);
j_day_no = j_day_no % 12053;
int jy = 979 + 33 * j_np + 4 * _div(j_day_no, 1461);
j_day_no %= 1461;
if (j_day_no >= 366) {
jy += _div(j_day_no - 1, 365);
j_day_no = (j_day_no-1) % 365;
}
for (i = 0; i < 11 && j_day_no >= j_days_in_month[i]; ++i) {
j_day_no -= j_days_in_month[i];
}
int jm = i+1;
int jd = j_day_no + 1;
jDate jdate;
jdate.year = _inttostr(jy);
jdate.month = _inttostr(jm);
jdate.day = _inttostr(jd);
return jdate;
}
#endif //_JDATE_H