-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdatetime.c
885 lines (778 loc) · 23.5 KB
/
datetime.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
/*=======================================================================
solunar
datetime.c
Definition of DateTime object
Stores date and time in a locate-neutral way. The date/time can
optionally be associated with a name, e.g., 'vernal equinox'
(c)2005-2012 Kevin Boone
=======================================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdint.h>
#include "timeutil.h"
#include "datetime.h"
extern char *strptime (const char *s, const char *fmt, struct tm *tm);
typedef struct _DateTimePriv
{
time_t utime;
char *name; // e.g., Christmas day
} DateTimePriv;
/*=======================================================================
my_setenv
A variant of setenv() that allows a NULL value to call unsetenv;
setenv() itself does not accept NULL as a value
=======================================================================*/
void my_setenv (const char *name, const char *value, BOOL dummy)
{
//printf ("name=%s value=%s\n", name, value);
if (value)
setenv (name, value, 1);
else
unsetenv (name);
}
/*=======================================================================
getenv_dup
Gets a copy of an environment string. Caller must free it. Note that the
return value might be null (which cannot be freed)
=======================================================================*/
static char *getenv_dup (const char *env)
{
char *s = getenv (env);
if (s) return strdup (s);
return NULL;
}
/*=======================================================================
DateTime_new_utime
=======================================================================*/
DateTime *DateTime_new_utime (time_t utime)
{
DateTime *self = (DateTime *) malloc (sizeof (DateTime));
self->priv = (DateTimePriv *) malloc (sizeof (DateTimePriv));
self->priv->utime = utime;
self->priv->name = NULL;
return self;
}
/*=======================================================================
DateTime_new_julian
=======================================================================*/
DateTime *DateTime_new_julian (double jd)
{
DateTime *self = (DateTime *) malloc (sizeof (DateTime));
self->priv = (DateTimePriv *) malloc (sizeof (DateTimePriv));
self->priv->utime = (jd - 2440587.5) * 86400;
self->priv->name = NULL;
return self;
}
/*=======================================================================
DateTime_new_centre
Creates a new datetime half-way between the two specified values.
The result should always be between d1 and d2, but we fix a bug
in this method where sometimes the calculted sunrise comes out exactly
24 hours early, which would make the value of high noon 12 hours
early. It would be better to fix the bug rather than work around it
here, but I can't find it :/
=======================================================================*/
DateTime *DateTime_new_centre (const DateTime *d1, const DateTime *d2)
{
DateTime *self = (DateTime *) malloc (sizeof (DateTime));
self->priv = (DateTimePriv *) malloc (sizeof (DateTimePriv));
if (d2->priv->utime < d1->priv->utime)
{
// Watch out here -- time_t is no big enough to hold the sum of two
// times
self->priv->utime = (time_t)
(((uint64_t)d2->priv->utime + (uint64_t)(d1->priv->utime + 24 * 3600)) / 2);
}
else
{
self->priv->utime = (time_t)
(((uint64_t)d2->priv->utime + (uint64_t)d1->priv->utime) / 2);
}
self->priv->name = NULL;
return self;
}
/*=======================================================================
DateTime_set_name
=======================================================================*/
void DateTime_set_name (const DateTime *self, const char *name)
{
if (self->priv->name) free (self->priv->name);
if (name)
self->priv->name = strdup (name);
else
self->priv->name = NULL;
}
/*=======================================================================
DateTime_get_name
=======================================================================*/
const char* DateTime_get_name (const DateTime *self)
{
return self->priv->name;
}
/*=======================================================================
DateTime_clone
=======================================================================*/
DateTime *DateTime_clone (const DateTime *other)
{
DateTime *r = DateTime_new_utime (other->priv->utime);
DateTime_set_name (r, other->priv->name);
return r;
}
/*=======================================================================
DateTime_parse
Parse a string against a format, write results to tm
=======================================================================*/
BOOL DateTime_parse (const char *str, const char *fmt, struct tm *tm)
{
time_t now = time (NULL);
BOOL ret;
memcpy (tm, localtime (&now), sizeof (struct tm));
tm->tm_hour = 2;
tm->tm_min = 0;
tm->tm_sec = 0;
tm->tm_isdst = -1;
char *last = strptime (str, fmt, tm);
if (tm->tm_year < 50) tm->tm_year += 2000;
if (last != str + strlen (str))
ret = FALSE;
else
ret = TRUE;
return ret;
}
/*=======================================================================
DateTime_new_parse
=======================================================================*/
DateTime *DateTime_new_parse (const char *str, Error **error, const char *tz,
BOOL utc)
{
struct tm tm;
time_t utime = 0;
char *oldtz = NULL;
if (DateTime_parse (str, "%e/%m/%Y %H:%M", &tm)) goto success;
if (DateTime_parse (str, "%e/%m/%Y", &tm)) goto success;
if (DateTime_parse (str, "%e/%m %M:M", &tm)) goto success;
if (DateTime_parse (str, "%e/%m", &tm)) goto success;
if (DateTime_parse (str, "%b %e %Y %H:%M", &tm)) goto success;
if (DateTime_parse (str, "%b %e %H:%M", &tm)) goto success;
if (DateTime_parse (str, "%b %e %Y", &tm)) goto success;
if (DateTime_parse (str, "%b %e", &tm)) goto success;
if (DateTime_parse (str, "%H:%M", &tm)) goto success;
if (DateTime_parse (str, "%j#%Y", &tm)) goto success;
// Failed
*error = Error_new ("Can't parse date");
return NULL;
success:
if (utc) tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
utime = mktime (&tm);
if (tz)
{
////if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
DateTime *dt = DateTime_new_utime (utime);
return dt;
}
/*=======================================================================
DateTime_new_dmy_name
=======================================================================*/
DateTime *DateTime_new_dmy (int day, int month, int year,
const char *tz, BOOL utc)
{
return DateTime_new_dmy_name (day, month, year, NULL, tz, utc);
}
/*=======================================================================
DateTime_new_dmy_name
Creates a new datetime at midnight on the specified date. Note that
we need to pass timezone info, because midnight in one zone is not
the same universal time as midnight in another
=======================================================================*/
DateTime *DateTime_new_dmy_name (int day, int month, int year,
const char *name, const char *tz, BOOL utc)
{
struct tm tm;
time_t utime = 0;
char *oldtz = NULL;
if (utc) tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
tm.tm_mday = day;
tm.tm_mon = month - 1;
tm.tm_year = year - 1900;
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
utime = mktime (&tm);
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
DateTime *r = DateTime_new_utime (utime);
DateTime_set_name (r, name);
return r;
}
/*=======================================================================
DateTime_new_today
=======================================================================*/
DateTime *DateTime_new_today (void)
{
return DateTime_new_utime (time (NULL));
}
/*=======================================================================
DateTime_date_to_string_syslocal
Caller must free string
=======================================================================*/
char *DateTime_date_to_string_syslocal (const DateTime *self)
{
struct tm *tm = localtime(&(self->priv->utime));
char s[100];
strftime (s, sizeof (s), "%A %e %B %Y", tm);
return strdup (s);
}
/*=======================================================================
DateTime_date_to_string_syslocal
Caller must free string
=======================================================================*/
char *DateTime_date_to_string_local (const DateTime *self, const char *tz)
{
char *oldtz = NULL;
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
struct tm *tm = localtime(&(self->priv->utime));
char s[100];
strftime (s, sizeof (s), "%A %e %B %Y", tm);
if (s[strlen(s) - 1] == 10) s[strlen(s) - 1] = 0;
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return strdup (s);
}
/*=======================================================================
DateTime_date_to_string_utc
Caller must free string
=======================================================================*/
char *DateTime_date_to_string_UTC (const DateTime *self)
{
struct tm *tm = gmtime(&(self->priv->utime));
char s[100];
strftime (s, sizeof (s), "%A %e %B %Y", tm);
return strdup (s);
}
/*=======================================================================
DateTime_to_string_local
Caller must free string
If tz is null, gives the local time at the home locale
=======================================================================*/
char *DateTime_to_string_local (const DateTime *self, const char *tz)
{
char *oldtz = NULL;
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
char *s = ctime (&(self->priv->utime));
if (s[strlen(s) - 1] == 10) s[strlen(s) - 1] = 0;
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return strdup (s);
}
/*=======================================================================
DateTime_to_string_syslocal
Caller must free string
=======================================================================*/
char *DateTime_to_string_syslocal (const DateTime *self)
{
char *s = ctime (&(self->priv->utime));
if (s[strlen(s) - 1] == 10) s[strlen(s) - 1] = 0;
return strdup (s);
}
/*=======================================================================
DateTime_to_string_utc
Caller must free string
=======================================================================*/
char *DateTime_to_string_UTC (const DateTime *self)
{
char *oldtz = NULL;
char *tz = "GMT0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
char *s = ctime (&(self->priv->utime));
if (s[strlen(s) - 1] == 10) s[strlen(s) - 1] = 0;
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return strdup (s);
}
/*=======================================================================
DateTime_time_to_string_utc
Caller must free string
=======================================================================*/
char *DateTime_time_to_string_UTC (const DateTime *self, BOOL twelve_hour)
{
char *oldtz = NULL;
char *tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
struct tm *tm = localtime(&(self->priv->utime));
//struct tm *tm = gmtime(&(self->priv->utime));
char s[20];
if (twelve_hour)
{
snprintf (s, sizeof (s), "%02d:%02d %s",
tm->tm_hour <= 12 ? tm->tm_hour : tm->tm_hour - 12 , tm->tm_min,
tm->tm_hour >= 12 ? "pm": "am");
}
else
snprintf (s, sizeof (s), "%02d:%02d", tm->tm_hour, tm->tm_min);
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return strdup (s);
}
/*=======================================================================
DateTime_time_to_string_local
Caller must free string
=======================================================================*/
char *DateTime_time_to_string_local (const DateTime *self, const char *tz,
BOOL twelve_hour)
{
char *oldtz = NULL;
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
struct tm *tm = localtime(&(self->priv->utime));
char s[20];
if (twelve_hour)
{
snprintf (s, sizeof (s), "%2d:%02d %s",
tm->tm_hour <= 12 ? tm->tm_hour : tm->tm_hour - 12 , tm->tm_min,
tm->tm_hour >= 12 ? "pm": "am");
}
else
//snprintf (s, sizeof (s), "%02d:%02d", tm->tm_hour, tm->tm_min);
//snprintf (s, sizeof (s), "%02d:%02d %d/%d", tm->tm_hour, tm->tm_min, tm->tm_mday, tm->tm_mon + 1);
snprintf (s, sizeof (s), "%02d:%02d", tm->tm_hour, tm->tm_min);
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return strdup (s);
}
/*=======================================================================
DateTime_time_to_string_syslocal
Caller must free string
=======================================================================*/
char *DateTime_time_to_string_syslocal (const DateTime *self, BOOL twelve_hour)
{
struct tm *tm = localtime(&(self->priv->utime));
char s[20];
if (twelve_hour)
{
snprintf (s, sizeof (s), "%2d:%02d %s",
tm->tm_hour <= 12 ? tm->tm_hour : tm->tm_hour - 12 , tm->tm_min,
tm->tm_hour >= 12 ? "pm": "am");
}
else
snprintf (s, sizeof (s), "%02d:%02d", tm->tm_hour, tm->tm_min);
return strdup (s);
}
/*=======================================================================
DateTime_free
=======================================================================*/
void DateTime_free (DateTime *self)
{
if (!self) return;
if (self->priv)
{
if (self->priv->name) free (self->priv->name);
free (self->priv);
}
free (self);
}
/*=======================================================================
DateTime_get_day_of_year
=======================================================================*/
int DateTime_get_day_of_year (const DateTime *self, const char *tz)
{
struct tm tm;
char *oldtz = NULL;
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
memcpy (&tm, gmtime (&self->priv->utime), sizeof (struct tm));
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return tm.tm_yday + 1;
}
/*=======================================================================
DateTime_set_time_hours_fracion
=======================================================================*/
void DateTime_set_time_hours_fraction (DateTime *self, double hours)
{
struct tm tm;
double h, m, s;
memcpy (&tm, gmtime (&self->priv->utime), sizeof (struct tm));
h = floor (hours);
m = floor ((hours - h) * 60);
s = (hours - h - m / 60) * 3600;
tm.tm_hour = h;
tm.tm_min = m;
tm.tm_sec = s;
char *oldtz = NULL;
char *tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
self->priv->utime = mktime (&tm);
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
}
}
/*=======================================================================
DateTime_get_juian_date
=======================================================================*/
double DateTime_get_julian_date (const DateTime *self)
{
double jd = timeutil_unix_to_JD (self->priv->utime);
return jd;
}
/*=======================================================================
DateTime_get_modified_juian_date
=======================================================================*/
double DateTime_get_modified_julian_date (const DateTime *self)
{
double jd = timeutil_unix_to_MJD (self->priv->utime);
return jd;
}
/*=======================================================================
DateTime_get_day_start
Caller must free result
Get midnight on the day in which this datetime falls
Umm... I'm not sure if we should take DST into account here
=======================================================================*/
DateTime *DateTime_get_day_start (const DateTime *self, const char *tz)
{
char *oldtz = NULL;
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
struct tm tm;
memcpy (&tm, localtime (&self->priv->utime), sizeof (struct tm));
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
time_t utime = mktime (&tm);
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
DateTime *dt = DateTime_new_utime (utime);
return dt;
}
/*=======================================================================
DateTime_get_day_end
Caller must free result
Get 23:59:59 on the day in which this datetime falls
=======================================================================*/
DateTime *DateTime_get_day_end (const DateTime *self, const char *tz)
{
char *oldtz = NULL;
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
struct tm tm;
memcpy (&tm, localtime (&self->priv->utime), sizeof (struct tm));
tm.tm_hour = 23;
tm.tm_min = 59;
tm.tm_sec = 59;
time_t utime = mktime (&tm);
if (tz)
{
if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
DateTime *dt = DateTime_new_utime (utime);
return dt;
}
/*=======================================================================
DateTime_seconds_difference
=======================================================================*/
long DateTime_seconds_difference (const DateTime *start, const DateTime *end)
{
return end->priv->utime - start->priv->utime;
}
/*=======================================================================
DateTime_add_seconds
=======================================================================*/
void DateTime_add_seconds (DateTime *self, long seconds)
{
self->priv->utime += seconds;
}
/*=======================================================================
DateTime_add_days
Nightmare! We can't just add 24*3600*days seconds, because we might be
crossing at DST boundary. We want the same time on the days a certan
distance from the current day
=======================================================================*/
void DateTime_add_days (DateTime *self, int days, const char *tz, BOOL utc)
{
struct tm tm;
char *oldtz = NULL;
if (utc) tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
memcpy (&tm, localtime (&(self->priv->utime)), sizeof (struct tm));
// mktime seems to cope with mday values > 31 and < 0, by adjusting
// the other fields to match. This even deals with DST. I am unsure
// whether this behaviour can be relied up on all systems
tm.tm_mday += days;
tm.tm_isdst = -1;
self->priv->utime = mktime (&tm);
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
}
/*=======================================================================
DateTime_is_same_day
Returns true if the two datetimes fall on the same calendar day
Note -- don't need tz info despite use of gmtime, because if two
events are on the same day in one zone, they are on the same day in
another, even if they both are on a different day
=======================================================================*/
BOOL DateTime_is_same_day (const DateTime *self, const DateTime *other)
{
struct tm self_tm, other_tm;
struct tm *tm = gmtime(&(self->priv->utime));
memcpy (&self_tm, tm, sizeof (struct tm));
tm = gmtime(&(other->priv->utime));
memcpy (&other_tm, tm, sizeof (struct tm));
if (self_tm.tm_mday == other_tm.tm_mday
&& self_tm.tm_mon == other_tm.tm_mon
&& self_tm.tm_year == other_tm.tm_year) return TRUE;
return FALSE;
}
/*=======================================================================
DateTime_is_same_day_of_year
As is_same_day, but ignores the year. This is important for comparing
anniversaries: Christmas day is the 25th December in any year
=======================================================================*/
BOOL DateTime_is_same_day_of_year (const DateTime *self, const DateTime *other)
{
struct tm self_tm, other_tm;
struct tm *tm = gmtime(&(self->priv->utime));
memcpy (&self_tm, tm, sizeof (struct tm));
tm = gmtime(&(other->priv->utime));
memcpy (&other_tm, tm, sizeof (struct tm));
if (self_tm.tm_mday == other_tm.tm_mday
&& self_tm.tm_mon == other_tm.tm_mon) return TRUE;
return FALSE;
}
/*=======================================================================
DateTime_get_ymdhms
=======================================================================*/
void DateTime_get_ymdhms (const DateTime *self, int *year, int *month, int *day,
int *hours, int *minutes, int *seconds, const char *tz, BOOL utc)
{
struct tm tm;
char *oldtz = NULL;
if (utc) tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
memcpy (&tm, localtime (&self->priv->utime), sizeof (struct tm));
*year = tm.tm_year + 1900;
*month = tm.tm_mon + 1;
*day = tm.tm_mday;
*hours = tm.tm_hour;
*minutes = tm.tm_min;
*seconds = tm.tm_sec;
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
}
/*=======================================================================
DateTime_get_jan_first
Get start of year -- midnight on january first. Note the we need tz
information, as midnight occurs at different universal times in
different zones.
=======================================================================*/
DateTime *DateTime_get_jan_first (const DateTime *self,
const char *tz, BOOL utc)
{
struct tm tm;
char *oldtz = NULL;
if (utc) tz = "UTC0";
if (tz)
{
oldtz = getenv_dup ("TZ");
my_setenv ("TZ", tz, 1);
tzset ();
}
memcpy (&tm, localtime (&(self->priv->utime)), sizeof (struct tm));
tm.tm_mday = 0;
tm.tm_mon = 0;
/* tm.tm_year unchanged */
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
DateTime *r = DateTime_new_utime (mktime (&tm));
if (tz)
{
//if (oldtz)
{
my_setenv ("TZ", oldtz, 1);
if (oldtz) free (oldtz);
tzset ();
}
//unmy_setenv ("TZ");
}
return r;
}
/*=======================================================================
clone_offset_days
Create a new datetime object the specified days offset from the one
supplied
=======================================================================*/
DateTime *DateTime_clone_offset_days (const DateTime *dt, int days,
const char *name, const char *tz, BOOL utc)
{
DateTime *r = DateTime_clone (dt);
DateTime_set_name (r, name);
DateTime_add_days (r, days, tz, utc);
return r;
}