Skip to content

Commit 4a4ee3a

Browse files
committed
Fix jdate conversion error
Make sure the daylight savings time flag is NOT set and the time zone is set to “UTC”, otherwise the conversion of a Gregorian date to Jalali form and vice versa in some cases could be one day off.
1 parent 162c703 commit 4a4ee3a

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

sources/src/jdate.c

+20-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ int main(int argc, char** argv)
7171

7272
struct jtm j;
7373
struct tm g;
74+
char *tmp_tm_zone;
7475

7576
struct jdate_action action = {0};
7677

@@ -189,24 +190,40 @@ int main(int argc, char** argv)
189190
exit(EXIT_FAILURE);
190191
}
191192

192-
g.tm_hour = 0;
193+
g.tm_hour = 12;
193194
g.tm_min = 0;
194195
g.tm_sec = 0;
196+
g.tm_isdst = 0;
197+
g.tm_gmtoff = 0;
198+
tmp_tm_zone = getenv("TZ");
199+
setenv("TZ", "UTC", 1);
195200

196201
t = mktime(&g);
202+
if (tmp_tm_zone)
203+
setenv("TZ", tmp_tm_zone, 1);
204+
else
205+
unsetenv("TZ");
197206
} else if (action.gregorian) {
198207
if (!jstrptime(action.gregorian_ptr, "%Y/%m/%d", &j)) {
199208
fprintf(stderr, "Specify jalali date in the following format\n");
200209
fprintf(stderr, "%%Y/%%m/%%d e.g. 1390/03/25\n");
201210
exit(EXIT_FAILURE);
202211
}
203212

204-
jalali_update(&j);
205-
j.tm_hour = 0;
213+
j.tm_hour = 12;
206214
j.tm_min = 0;
207215
j.tm_sec = 0;
216+
j.tm_isdst = 0;
217+
j.tm_gmtoff = 0;
218+
tmp_tm_zone = getenv("TZ");
219+
setenv("TZ", "UTC", 1);
220+
jalali_update(&j);
208221

209222
t = jmktime(&j);
223+
if (tmp_tm_zone)
224+
setenv("TZ", tmp_tm_zone, 1);
225+
else
226+
unsetenv("TZ");
210227
}
211228

212229
if (action.date) {

0 commit comments

Comments
 (0)