Debian Codegraph @

src/plugins/schedulers/tj/taskjuggler/Utility.cpp

raw
1 /*
2 * Utility.cpp - TaskJuggler
3 *
4 * SPDX-FileCopyrightText: 2001, 2002, 2003, 2004 Chris Schlaeger <[email protected]>
5 * SPDX-FileCopyrightText: 2011 Dag Andersen <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * $Id$
12 */
13
14 // clazy:excludeall=qstring-arg
15 #include "Utility.h"
16
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20
21 #include <string>
22 #include <stdlib.h>
23
24 #include <QLocale>
25 #include <QMap>
26
27 #include "TjMessageHandler.h"
28 #include "tjlib-internal.h"
29
30 #if defined(__SVR4) && defined(__sun)
31 int setenv(const char* var, const char* val, int ignore);
32 int unsetenv (const char *var);
33 #endif
34
35 #ifdef _MSC_VER
36 #if _MSC_VER >= 1900
37 #define tzname _tzname
38 #define tzset _tzset
39 #endif
40 #endif
41
42 namespace TJ
43 {
44
45 static QMap<QString, const char*> TZDict; // clazy:exclude=non-pod-global-static
46 static bool TZDictReady = false;
47
48 static QString UtilityError; // clazy:exclude=non-pod-global-static
49
50 /* localtime() calls are fairly expensive, so we implement a hashtable based
51 * cache to avoid redundant calls for the same value. Changing the timezone
52 * invalidates the cache though. */
53 class LtHashTabEntry
54 {
55 public:
56 LtHashTabEntry() : tms(nullptr), next(nullptr) {}
57 ~LtHashTabEntry() { /*qDebug()<<"~LtHashTabEntry";*/ delete tms; }
58 time_t t;
59 struct tm* tms;
60 LtHashTabEntry* next;
61 };
62
63 static long LTHASHTABSIZE;
64 static LtHashTabEntry** LtHashTab = nullptr;
65
66 bool
67 isRichText(const QString& str)
68 {
69 /* This function tries to guess whether a string is a rich-text string or
70 * not. It looks for xml tags marks and does a simple validation on them.
71 */
72
73 bool hasTags = false;
74 bool inTag = false;
75 for (int i = 0; i < str.length(); ++i)
76 {
77 if (str[i] == '<')
78 {
79 if (inTag)
80 return false;
81 inTag = hasTags = true;
82 }
83 else if (str[i] == '>')
84 {
85 if (!inTag)
86 return false;
87 inTag = false;
88 }
89 }
90
91 return hasTags && !inTag;
92 }
93
94 const char*
95 timezone2tz(const char* tzone)
96 {
97 if (!TZDictReady)
98 {
99 // TZDict.setAutoDelete(false);
100
101 // Let's start with generic timezones
102 TZDict.insert("+1300", "GMT-13:00");
103 TZDict.insert("+1200", "GMT-12:00");
104 TZDict.insert("+1100", "GMT-11:00");
105 TZDict.insert("+1000", "GMT-10:00");
106 TZDict.insert("+0900", "GMT-9:00");
107 TZDict.insert("+0800", "GMT-8:00");
108 TZDict.insert("+0700", "GMT-7:00");
109 TZDict.insert("+0600", "GMT-6:00");
110 TZDict.insert("+0500", "GMT-5:00");
111 TZDict.insert("+0400", "GMT-4:00");
112 TZDict.insert("+0300", "GMT-3:00");
113 TZDict.insert("+0200", "GMT-2:00");
114 TZDict.insert("+0100", "GMT-1:00");
115 TZDict.insert("+0000", "GMT-0:00");
116 TZDict.insert("-0100", "GMT+1:00");
117 TZDict.insert("-0200", "GMT+2:00");
118 TZDict.insert("-0300", "GMT+3:00");
119 TZDict.insert("-0400", "GMT+4:00");
120 TZDict.insert("-0500", "GMT+5:00");
121 TZDict.insert("-0600", "GMT+6:00");
122 TZDict.insert("-0700", "GMT+7:00");
123 TZDict.insert("-0800", "GMT+8:00");
124 TZDict.insert("-0900", "GMT+9:00");
125 TZDict.insert("-1000", "GMT+10:00");
126 TZDict.insert("-1100", "GMT+11:00");
127 TZDict.insert("-1200", "GMT+12:00");
128 // Now some convenience timezones. There will be more in the future.
129 TZDict.insert("PST", "GMT+8:00");
130 TZDict.insert("PDT", "GMT+7:00");
131 TZDict.insert("MST", "GMT+7:00");
132 TZDict.insert("MDT", "GMT+6:00");
133 TZDict.insert("CST", "GMT+6:00");
134 TZDict.insert("CDT", "GMT+5:00");
135 TZDict.insert("EST", "GMT+5:00");
136 TZDict.insert("EDT", "GMT+4:00");
137 TZDict.insert("GMT", "GMT");
138 TZDict.insert("UTC", "GMT");
139 TZDict.insert("CET", "GMT-1:00");
140 TZDict.insert("CEDT", "GMT-2:00");
141
142 TZDictReady = true;
143 }
144
145 return TZDict[tzone];
146 }
147
148 void initUtility(long dictSize)
149 {
150 if (LtHashTab)
151 exitUtility();
152
153 /* Find a prime number that is equal or bigger than dictSize. */
154 for (long i = 2; i < (dictSize / 2); i++)
155 if (dictSize % i == 0)
156 {
157 dictSize++;
158 i = 1;
159 }
160
161 LtHashTab = new LtHashTabEntry*[LTHASHTABSIZE = dictSize];
162 for (long i = 0; i < LTHASHTABSIZE; ++i)
163 LtHashTab[i] = nullptr;
164 }
165
166 void exitUtility()
167 {
168 //qDebug()<<"exitUtility:"<<LtHashTab;
169 if (!LtHashTab)
170 return;
171
172 //qDebug()<<"exitUtility:"<<LTHASHTABSIZE;
173 for (long i = 0; i < LTHASHTABSIZE; ++i)
174 for (LtHashTabEntry* htep = LtHashTab[i]; htep;)
175 {
176 LtHashTabEntry* tmp = htep->next;
177 delete htep;
178 htep = tmp;
179 }
180
181 delete [] LtHashTab;
182 LtHashTab = nullptr;
183 }
184
185 bool
186 setTimezone(const char* tZone)
187 {
188 UtilityError.clear();
189
190 if (!qputenv("TZ", tZone))
191 qFatal("Ran out of space in environment section while "
192 "setting timezone.");
193
194 /* To validate the tZone value we call tzset(). It will convert the zone
195 * into a three-letter acronym in case the tZone value is good. If not, it
196 * will just copy the wrong value to tzname[0] (glibc < 2.5) or fall back
197 * to UTC. */
198 tzset();
199 if (timezone2tz(tZone) == nullptr &&
200 (strcmp(tzname[0], tZone) == 0 ||
201 (strcmp(tZone, "UTC") != 0 && strcmp(tzname[0], "UTC") == 0)))
202 {
203 qDebug("1: %s, 2: %s", tzname[0], tzname[1]);
204 UtilityError = QString(QString("Illegal timezone '%1'")).arg(tZone);
205 return false;
206 }
207
208 if (!LtHashTab)
209 return true;
210 for (long i = 0; i < LTHASHTABSIZE; ++i)
211 {
212 for (LtHashTabEntry* htep = LtHashTab[i]; htep;)
213 {
214 LtHashTabEntry* tmp = htep->next;
215 delete htep->tms;
216 htep = tmp;
217 }
218 if (LtHashTab[i])
219 LtHashTab[i] = nullptr;
220 }
221 return true;
222 }
223
224 const struct tm *
225 clocaltime(const time_t* t)
226 {
227 /* In some cases we haven't initialized the module yet. So we do not use
228 * the cache. */
229 time_t tt = *t < 0 ? 0 : *t;
230 if (!LtHashTab)
231 return localtime(&tt);
232
233 long index = tt % LTHASHTABSIZE;
234 if (LtHashTab[index])
235 for (LtHashTabEntry* htep = LtHashTab[index]; htep;
236 htep = htep->next)
237 if (htep->t == tt)
238 return htep->tms;
239
240 LtHashTabEntry* htep = new LtHashTabEntry;
241 htep->next = LtHashTab[index];
242 htep->t = tt;
243 htep->tms = new struct tm;
244 memcpy(htep->tms, localtime(&tt), sizeof(struct tm));
245 LtHashTab[index] = htep;
246 return htep->tms;
247 }
248
249 const QString&
250 getUtilityError()
251 {
252 return UtilityError;
253 }
254
255 QString
256 monthAndYear(time_t t)
257 {
258 const struct tm* tms = clocaltime(&t);
259 static char s[32];
260 strftime(s, sizeof(s), "%b %Y", tms);
261 return QString::fromLocal8Bit(s);
262 }
263
264 QString
265 shortMonthName(int mon)
266 {
267 struct tm tms;
268 tms.tm_mday = 1;
269 tms.tm_mon = mon;
270 tms.tm_year = 2000;
271 static char s[32];
272 strftime(s, sizeof(s), "%b", &tms);
273 return QString::fromLocal8Bit(s);
274 }
275
276 bool
277 isWeekend(time_t t)
278 {
279 const struct tm* tms = clocaltime(&t);
280 return (tms->tm_wday < 1 || tms->tm_wday > 5);
281 }
282
283 int
284 daysLeftInMonth(time_t t)
285 {
286 int left = 0;
287 const struct tm* tms = clocaltime(&t);
288 for (int m = tms->tm_mon; tms->tm_mon == m;)
289 {
290 left++;
291 t = sameTimeNextDay(t);
292 tms = clocaltime(&t);
293 }
294 return left;
295 }
296
297 int
298 weeksLeftInMonth(time_t t)
299 {
300 int left = 0;
301 const struct tm* tms = clocaltime(&t);
302 for (int m = tms->tm_mon; tms->tm_mon == m;)
303 {
304 left++;
305 t = sameTimeNextWeek(t);
306 tms = clocaltime(&t);
307 }
308 return left;
309 }
310
311 int
312 monthLeftInYear(time_t t)
313 {
314 int left = 0;
315 const struct tm* tms = clocaltime(&t);
316 for (int m = tms->tm_year; tms->tm_year == m;)
317 {
318 left++;
319 t = sameTimeNextMonth(t);
320 tms = clocaltime(&t);
321 }
322 return left;
323 }
324
325 int
326 quartersLeftInYear(time_t t)
327 {
328 int left = 0;
329 const struct tm* tms = clocaltime(&t);
330 for (int m = tms->tm_year; tms->tm_year == m;)
331 {
332 left++;
333 t = sameTimeNextQuarter(t);
334 tms = clocaltime(&t);
335 }
336 return left;
337 }
338
339 int
340 daysBetween(time_t t1, time_t t2)
341 {
342 int days = 0;
343 // TODO: Very slow!!!
344 for (time_t t = t1; t < t2; t = sameTimeNextDay(t))
345 days++;
346 return days;
347 }
348
349 int
350 weeksBetween(time_t t1, time_t t2)
351 {
352 int days = 0;
353 // TODO: Very slow!!!
354 for (time_t t = t1; t < t2; t = sameTimeNextWeek(t))
355 days++;
356 return days;
357 }
358
359 int
360 monthsBetween(time_t t1, time_t t2)
361 {
362 int months = 0;
363 // TODO: Very slow!!!
364 for (time_t t = t1; t < t2; t = sameTimeNextMonth(t))
365 months++;
366 return months;
367 }
368
369 int
370 quartersBetween(time_t t1, time_t t2)
371 {
372 int quarters = 0;
373 // TODO: Very slow!!!
374 for (time_t t = t1; t < t2; t = sameTimeNextQuarter(t))
375 quarters++;
376 return quarters;
377 }
378
379 int
380 secondsOfDay(time_t t)
381 {
382 const struct tm* tms = clocaltime(&t);
383 return tms->tm_sec + tms->tm_min * 60 + tms->tm_hour * 3600;
384 }
385
386 int
387 hourOfDay(time_t t)
388 {
389 const struct tm* tms = clocaltime(&t);
390 return tms->tm_hour;
391 }
392
393 int
394 dayOfMonth(time_t t)
395 {
396 const struct tm* tms = clocaltime(&t);
397 return tms->tm_mday;
398 }
399
400 int
401 weekOfYear(time_t t, bool beginOnMonday)
402 {
403 /* The ISO 8601:1988 week number of the current year as a decimal
404 * number, range 1 to 53, where week 1 is the first week that has at
405 * least 4 days in the current year, and with Monday as the first day
406 * of the week. This is also compliant with DIN 1355. */
407 uint week = 0;
408 uint weekday1Jan = dayOfWeek(beginOfYear(t), beginOnMonday);
409 const struct tm* tms = clocaltime(&t);
410 int days = tms->tm_yday;
411
412 if (weekday1Jan > 3)
413 days = days - (7 - weekday1Jan);
414 else
415 days = days + weekday1Jan;
416
417 if (days < 0)
418 if ((weekday1Jan == 4) ||
419 (dayOfWeek(beginOfYear(beginOfYear(t) - 1), beginOnMonday) == 3))
420 week = 53;
421 else
422 week = 52;
423 else
424 week = days / 7 + 1;
425
426 if ((days > 360) && (week > 52))
427 {
428 if (weekday1Jan == 3)
429 week = 53;
430 else if (dayOfWeek(sameTimeNextYear(beginOfYear(t)),
431 beginOnMonday) == 4)
432 week = 53;
433 else
434 week = 1;
435 }
436
437 return week;
438 }
439
440 int
441 monthOfWeek(time_t t, bool beginOnMonday)
442 {
443 const struct tm* tms = clocaltime(&t);
444 int tm_mon = tms->tm_mon;
445 int tm_mday = tms->tm_mday;
446 int lastDayOfMonth = dayOfMonth(beginOfMonth(sameTimeNextMonth(t)) - 1);
447 if (tm_mday < 4)
448 {
449 if (dayOfWeek(t, beginOnMonday) - tm_mday >= 3)
450 {
451 if (tm_mon == 0)
452 {
453 return 12;
454 }
455 else
456 {
457 return tm_mon;
458 }
459 }
460 }
461 else if (tm_mday > lastDayOfMonth - 4)
462 {
463 if (tm_mday - dayOfWeek(t, beginOnMonday) > lastDayOfMonth - 4)
464 {
465 if (tm_mon == 11)
466 {
467 return 1;
468 }
469 else
470 {
471 return tm_mon + 2;
472 }
473 }
474 }
475 return tm_mon + 1;
476 }
477
478 int
479 monthOfYear(time_t t)
480 {
481 const struct tm* tms = clocaltime(&t);
482 return tms->tm_mon + 1;
483 }
484
485 int
486 quarterOfYear(time_t t)
487 {
488 const struct tm* tms = clocaltime(&t);
489 return tms->tm_mon / 3 + 1;
490 }
491
492 int
493 dayOfWeek(time_t t, bool beginOnMonday)
494 {
495 const struct tm* tms = clocaltime(&t);
496 if (beginOnMonday)
497 return tms->tm_wday ? tms->tm_wday - 1 : 6;
498 else
499 return tms->tm_wday;
500 }
501
502 QString
503 dayOfWeekName(time_t t)
504 {
505 const struct tm* tms = clocaltime(&t);
506 static char buf[64];
507
508 strftime(buf, 63, "%A", tms);
509 return QString::fromLocal8Bit(buf);
510 }
511
512 int
513 dayOfYear(time_t t)
514 {
515 const struct tm* tms = clocaltime(&t);
516 return tms->tm_yday + 1;
517 }
518
519
520 int
521 year(time_t t)
522 {
523 const struct tm* tms = clocaltime(&t);
524 return tms->tm_year + 1900;
525 }
526
527 int
528 yearOfWeek(time_t t, bool beginOnMonday)
529 {
530 const struct tm* tms = clocaltime(&t);
531 int tm_year = tms->tm_year;
532
533 int lastDayOfYear = dayOfYear(beginOfYear(sameTimeNextYear(t)) - 1);
534 if (dayOfYear(t) < 4)
535 {
536 if (dayOfWeek(t, beginOnMonday) - dayOfYear(t) >= 3)
537 return 1900 + tm_year - 1;
538 }
539 else if (dayOfYear(t) > lastDayOfYear - 4)
540 {
541 if (dayOfYear(t) - dayOfWeek(t, beginOnMonday) > lastDayOfYear - 4)
542 return 1900 + tm_year + 1;
543 }
544 return 1900 + tm_year;
545 }
546
547 time_t
548 beginOfHour(time_t t)
549 {
550 const struct tm* tms = clocaltime(&t);
551 struct tm tmc;
552 memcpy(&tmc, tms, sizeof(struct tm));
553 tmc.tm_sec = tmc.tm_min = 0;
554 tmc.tm_isdst = -1;
555 return mktime(&tmc);
556 }
557
558 time_t
559 midnight(time_t t)
560 {
561 const struct tm* tms = clocaltime(&t);
562 struct tm tmc;
563 memcpy(&tmc, tms, sizeof(struct tm));
564 tmc.tm_sec = tmc.tm_min = tmc.tm_hour = 0;
565 tmc.tm_isdst = -1;
566 return mktime(&tmc);
567 }
568
569 time_t
570 beginOfWeek(time_t t, bool beginOnMonday)
571 {
572 const struct tm* tms;
573 for (tms = clocaltime(&t) ; tms->tm_wday != (beginOnMonday ? 1 : 0);)
574 {
575 t = sameTimeYesterday(t);
576 tms = clocaltime(&t);
577 }
578 struct tm tmc;
579 memcpy(&tmc, tms, sizeof(struct tm));
580 tmc.tm_sec = tmc.tm_min = tmc.tm_hour = 0;
581 tmc.tm_isdst = -1;
582 return mktime(&tmc);
583 }
584
585 time_t
586 beginOfMonth(time_t t)
587 {
588 const struct tm* tms = clocaltime(&t);
589 struct tm tmc;
590 memcpy(&tmc, tms, sizeof(struct tm));
591 tmc.tm_mday = 1;
592 tmc.tm_sec = tmc.tm_min = tmc.tm_hour = 0;
593 tmc.tm_isdst = -1;
594 return mktime(&tmc);
595 }
596
597 time_t
598 beginOfQuarter(time_t t)
599 {
600 const struct tm* tms = clocaltime(&t);
601 struct tm tmc;
602 memcpy(&tmc, tms, sizeof(struct tm));
603 tmc.tm_mon = (tmc.tm_mon / 3) * 3;
604 tmc.tm_mday = 1;
605 tmc.tm_sec = tmc.tm_min = tmc.tm_hour = 0;
606 tmc.tm_isdst = -1;
607 return mktime(&tmc);
608 }
609
610 time_t
611 beginOfYear(time_t t)
612 {
613 const struct tm* tms = clocaltime(&t);
614 struct tm tmc;
615 memcpy(&tmc, tms, sizeof(struct tm));
616 tmc.tm_mon = 0;
617 tmc.tm_mday = 1;
618 tmc.tm_sec = tmc.tm_min = tmc.tm_hour = 0;
619 tmc.tm_isdst = -1;
620 return mktime(&tmc);
621 }
622
623 time_t
624 hoursLater(int h, time_t t)
625 {
626 // I hope this is correct under all circumstances.
627 return t + h * ONEHOUR;
628 }
629
630 time_t
631 sameTimeNextDay(time_t t)
632 {
633 const struct tm* tms = clocaltime(&t);
634 struct tm tmc;
635 memcpy(&tmc, tms, sizeof(struct tm));
636 tmc.tm_mday++;
637 tmc.tm_isdst = -1;
638 if (mktime(&tmc) == -1)
639 qFatal("Error at %s", time2ISO(t).toLatin1().constData());
640 return mktime(&tmc);
641 }
642
643 time_t
644 sameTimeYesterday(time_t t)
645 {
646 const struct tm* tms = clocaltime(&t);
647 struct tm tmc;
648 memcpy(&tmc, tms, sizeof(struct tm));
649 tmc.tm_mday--;
650 tmc.tm_isdst = -1;
651 return mktime(&tmc);
652 }
653
654 time_t
655 sameTimeNextWeek(time_t t)
656 {
657 const struct tm* tms = clocaltime(&t);
658 int weekday = tms->tm_wday;
659 do
660 {
661 t = sameTimeNextDay(t);
662 tms = clocaltime(&t);
663 } while (tms->tm_wday != weekday);
664 return t;
665 }
666
667 time_t
668 sameTimeLastWeek(time_t t)
669 {
670 const struct tm* tms = clocaltime(&t);
671 int weekday = tms->tm_wday;
672 do
673 {
674 t = sameTimeYesterday(t);
675 tms = clocaltime(&t);
676 } while (tms->tm_wday != weekday);
677 return t;
678 }
679
680 time_t
681 sameTimeNextMonth(time_t t)
682 {
683 const struct tm* tms = clocaltime(&t);
684 struct tm tmc;
685 memcpy(&tmc, tms, sizeof(struct tm));
686 tmc.tm_mon++;
687 tmc.tm_isdst = -1;
688 return mktime(&tmc);
689 }
690
691 time_t
692 sameTimeNextQuarter(time_t t)
693 {
694 const struct tm* tms = clocaltime(&t);
695 struct tm tmc;
696 memcpy(&tmc, tms, sizeof(struct tm));
697 tmc.tm_mon += 3;
698 tmc.tm_isdst = -1;
699 return mktime(&tmc);
700 }
701
702 time_t
703 sameTimeNextYear(time_t t)
704 {
705 const struct tm* tms = clocaltime(&t);
706 struct tm tmc;
707 memcpy(&tmc, tms, sizeof(struct tm));
708 tmc.tm_year++;
709 tmc.tm_isdst = -1;
710 return mktime(&tmc);
711 }
712
713 time_t
714 sameTimeLastYear(time_t t)
715 {
716 const struct tm* tms = clocaltime(&t);
717 struct tm tmc;
718 memcpy(&tmc, tms, sizeof(struct tm));
719 tmc.tm_year--;
720 tmc.tm_isdst = -1;
721 return mktime(&tmc);
722 }
723
724 QString
725 time2ISO(time_t t)
726 {
727 const struct tm* tms = clocaltime(&t);
728 static char buf[128];
729
730 strftime(buf, 127, "%Y-%m-%d %H:%M:%S %Z", tms);
731 return QString::fromLocal8Bit(buf);
732 }
733
734 QString
735 time2tjp(time_t t)
736 {
737 const struct tm* tms = clocaltime(&t);
738 static char buf[128];
739
740 strftime(buf, 127, "%Y-%m-%d-%H:%M:%S-%z", tms);
741 return QString::fromLocal8Bit(buf);
742 }
743
744 QString
745 time2user(time_t t, const QString& timeFormat, bool localtime)
746 {
747 if (t == 0)
748 return QString("undefined");
749
750 const struct tm* tms;
751 if (localtime)
752 tms = clocaltime(&t);
753 else
754 tms = gmtime(&t);
755
756 static char buf[128];
757
758 strftime(buf, 127, timeFormat.toLocal8Bit().constData(), tms);
759 return QString::fromLocal8Bit(buf);
760 }
761
762 QString
763 time2time(time_t t)
764 {
765 const struct tm* tms = clocaltime(&t);
766 static char buf[128];
767
768 strftime(buf, 127, "%H:%M %Z", tms);
769 return QString::fromLocal8Bit(buf);
770 }
771
772 QString
773 time2date(time_t t)
774 {
775 const struct tm* tms = clocaltime(&t);
776 static char buf[128];
777
778 strftime(buf, 127, "%Y-%m-%d", tms);
779 return QString::fromLocal8Bit(buf);
780 }
781
782 QString
783 time2weekday(time_t t)
784 {
785 const struct tm* tms = clocaltime(&t);
786 static char buf[128];
787
788 strftime(buf, 127, "%A", tms);
789 return QString::fromLocal8Bit(buf);
790 }
791
792 time_t
793 addTimeToDate(time_t day, time_t hour)
794 {
795 day = midnight(day);
796 const struct tm* tms = clocaltime(&day);
797
798 struct tm tmc;
799 memcpy(&tmc, tms, sizeof(struct tm));
800
801 tmc.tm_hour = hour / (60 * 60);
802 tmc.tm_min = (hour / 60) % 60;
803 tmc.tm_sec = hour % 60;
804
805 tmc.tm_isdst = -1;
806 return mktime(&tmc);
807 }
808
809 time_t
810 date2time(const QString& date)
811 {
812 UtilityError.clear();
813
814 int y, m, d, hour, min, sec;
815 char tZone[64] = "";
816 std::string savedTZ;
817 bool restoreTZ = false;
818 if (sscanf(date.toLocal8Bit().constData(), "%d-%d-%d-%d:%d:%d-%s",
819 &y, &m, &d, &hour, &min, &sec, tZone) == 7 ||
820 (sec = 0) || // set sec to 0
821 sscanf(date.toLocal8Bit().constData(), "%d-%d-%d-%d:%d-%s",
822 &y, &m, &d, &hour, &min, tZone) == 6)
823 {
824 const char* tz;
825 if ((tz = getenv("TZ")) != nullptr)
826 {
827 savedTZ = tz;
828 }
829 if ((tz = timezone2tz(tZone)) == nullptr)
830 {
831 UtilityError = QString("Illegal timezone %1").arg(tZone);
832 return 0;
833 }
834 else
835 {
836 if (!qputenv("TZ", tz))
837 qFatal("date2time: Ran out of space in environment section.");
838 restoreTZ = true;
839 }
840 }
841 else if (sscanf(date.toLocal8Bit().constData(), "%d-%d-%d-%d:%d:%d",
842 &y, &m, &d, &hour, &min, &sec) == 6)
843 tZone[0] = '\0';
844 else if (sscanf(date.toLocal8Bit().constData(), "%d-%d-%d-%d:%d", &y, &m, &d, &hour, &min) == 5)
845 {
846 sec = 0;
847 tZone[0] = '\0';
848 }
849 else if (sscanf(date.toLocal8Bit().constData(), "%d-%d-%d", &y, &m, &d) == 3)
850 {
851 tZone[0] = '\0';
852 hour = min = sec = 0;
853 }
854 else
855 {
856 qFatal("%s", QString("Illegal date: %1").arg(date).toLocal8Bit().constData());
857 return 0;
858 }
859
860 if (y < 1970)
861 {
862 UtilityError = QString("Year must be larger than 1969");
863 return 0;
864 }
865 if (m < 1 || m > 12)
866 {
867 UtilityError = QString("Month must be between 1 and 12");
868 return 0;
869 }
870 if (d < 1 || d > 31)
871 {
872 UtilityError = QString("Day must be between 1 and 31");
873 return 0;
874 }
875 if (hour < 0 || hour > 23)
876 {
877 UtilityError = QString("Hour must be between 0 and 23");
878 return 0;
879 }
880 if (min < 0 || min > 59)
881 {
882 UtilityError = QString("Minutes must be between 0 and 59");
883 return 0;
884 }
885 if (sec < 0 || sec > 59)
886 {
887 UtilityError = QString("Seconds must be between 0 and 59");
888 return 0;
889 }
890
891 #if defined(Q_OS_WIN) || defined(__CYGWIN__) || (defined(__SVR4) && defined(__sun))
892 struct tm t = { sec, min, hour, d, m - 1, y - 1900, 0, 0, -1 };
893 #else
894 struct tm t = { sec, min, hour, d, m - 1, y - 1900, 0, 0, -1, 0, nullptr };
895 #endif
896 time_t localTime = mktime(&t);
897
898 if (restoreTZ)
899 {
900 if (!savedTZ.empty())
901 {
902 if (!qputenv("TZ", savedTZ.c_str()))
903 qFatal("date2time: Ran out of space in environment section.");
904 }
905 else
906 qunsetenv("TZ");
907 }
908
909 return localTime;
910 }
911
912 QString
913 formatTime(time_t t)
914 {
915 return QLocale().toString(QDateTime::fromSecsSinceEpoch(t), QLocale::ShortFormat);
916 }
917
918 QDate
919 time2qdate(time_t t)
920 {
921 return QDate(year(t), monthOfYear(t), dayOfMonth(t));
922 }
923
924 time_t
925 qdate2time(const QDate& d)
926 {
927 #if defined(Q_OS_WIN) || defined(__CYGWIN__) || (defined(__SVR4) && defined(__sun))
928 struct tm t = { 0, 0, 0, d.day(), d.month() - 1, d.year() - 1900,
929 0, 0, -1 };
930 #else
931 struct tm t = { 0, 0, 0, d.day(), d.month() - 1, d.year() - 1900,
932 0, 0, -1, 0, nullptr };
933 #endif
934 return mktime(&t);
935 }
936
937 #if defined(__SVR4) && defined(__sun)
938 /*
939 * Note: a proper implementation of a "setenv" function for Solaris
940 * would take a map where the "variable-name" is linked to a
941 * pointer. This would allow freeing the memory allocated here
942 * (a kind of garbage collection).
943 */
944
945 int setenv(const char* var, const char* val, int ignore)
946 {
947 int varLen = strlen(var);
948 int valLen = strlen(val);
949 char *buffer = NULL;
950
951 if ((buffer = static_cast<char*>(malloc(varLen + valLen + 2))) == NULL)
952 return -1;
953
954 sprintf (buffer, "%s=%s", var, val);
955
956 return putenv(buffer) ? -1 : 0;
957 }
958
959 int unsetenv (const char *var)
960 {
961 return 0; /* SKIP */
962 }
963 #endif
964
965 } // namespace TJ