-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.c
1357 lines (1171 loc) · 33.1 KB
/
time.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
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2013 Marco Lizza ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2006-2011 OpenNETCF Consulting
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Version 0.01 - March 22, 2007
// Initial Release
//
// Version 0.02 - July 5, 2007
// Bug fixes. UTC offset not properly accounted for unless SetTz had been previously called.
// UTC offset used for functions like localtime_ce using old, rather than current data
//
// For the latest source, visit http://time.codeplex.com
///////////////////////////////////////////////////////////////////////////////
// Many but not all of the functions were created from the following source:
//
// Copyright (C) 2002 Michael Ringgaard. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the project nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
///////////////////////////////////////////////////////////////////////////////
#include <windows.h>
//#include <time.h>
#include "sys/timeb.h"
#include <stdio.h>
#include <winsock2.h>
#include <errno.h>
#include "internal.h"
//#include <stdlib.h> // time_t defined there
///////////////////////////////////////////////////////////////////////////////
// Macros
///////////////////////////////////////////////////////////////////////////////
#define TIME_FAILURE 0xFFFFFFFF
#define ASC_BUFF_SIZE 26 // Ascii buffer size is 26 bytes, (24 chars and CR+LF)
#define SEC_IN_HOUR 3600L
#define SECS_IN_MIN 60L
#define DAYSPERWEEK 7
#define YEAR0 1900
#define EPOCH_YR 1970
#define SECS_DAY (24L * 60L * 60L)
#define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
#define TIME_MAX 2147483647L
static const FILETIME ftJan1970 = {3577643008,27111902};
static struct tm tmbuf;
///////////////////////////////////////////////////////////////////////////////
// Local Variables
///////////////////////////////////////////////////////////////////////////////
// Number of seconds between local time and UTC time, includes DST bias
//
LONG _localtime;
// Is the local time in daylight savings time
//
DWORD _isdst;
// Bias for daylight savings time
//
int _dstBias;
// Contains the time zone string
//
char tz_name[2][32];
// Contains the 1/1/1970 reference date/time
//
const SYSTEMTIME st1970 = {1970, 1, 4, 1, 0, 0, 0, 0};
// Contains the number of days per month for
// non leap and leap years
//
const int _ytab[2][12] =
{
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
// Contains the days of the week abreviation
//
static char *aday[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
// Contains the days of the week full name
//
static char *day[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
// Contains the months of the year abreviation
//
static char *amonth[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
// Contains the months of the year full name
//
static char *month[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
///////////////////////////////////////////////////////////////////////////////
// Forward declaration of internal functions
///////////////////////////////////////////////////////////////////////////////
// Convert system time into seconds since 1970
//
LONGLONG SystemTimeToSecondsSince1970(SYSTEMTIME * st);
// Convert seconds since 1970 into a system time
//
void SecondsSince1970ToSystemTime(const time_t * timer, SYSTEMTIME * st,
BOOLEAN local);
// Initialize the time zone information needed for the time_ce methods
//
void SetTz(SYSTEMTIME *_st);
// Copy system time structure to tm structure
//
void SystemTimeToTm(SYSTEMTIME *st, struct tm * tmbuffer);
void TmToSystemTime(struct tm * tmbuffer, SYSTEMTIME *st);
// Get Julian Days from the begining of the year
//
DWORD JulianDays(SYSTEMTIME * st);
// Method parses and formats strings
//
static void strfmt(char *str, const char *fmt, ...);
// Reentrant version of gmttime
//
struct tm *gmtime_r_ce(const time_t *timer, struct tm *tmbuf, BOOLEAN local);
// Reentrant version of localtime
//
struct tm *localtime_r_ce(const time_t *timer, struct tm *tmbuf);
struct tm *localtime(const time_t *timer);
size_t strftime(char *s, size_t maxs, const char *f, const struct tm *t);
static void shl64(ULARGE_INTEGER* value, int shift)
{
ULARGE_INTEGER result;
if (value == NULL || shift <= 0)
return;
result.HighPart = (value->HighPart << shift) | (value->LowPart >> (32-shift));
result.LowPart = (value->LowPart << shift);
value->HighPart = result.HighPart;
value->LowPart = result.LowPart;
}
static void shr64(ULARGE_INTEGER* value, int shift)
{
ULARGE_INTEGER result;
if (value == NULL || shift <= 0)
return;
result.HighPart = (value->HighPart >> shift);
result.LowPart = (value->LowPart >> shift) | (value->HighPart << (32-shift));
value->HighPart = result.HighPart;
value->LowPart = result.LowPart;
}
// Subtract valueToSubtract from value (doesn't handle underflow)
static void sub64(ULARGE_INTEGER* value, ULARGE_INTEGER* valueToSubtract)
{
if (value == NULL || valueToSubtract == NULL)
return;
if (value->LowPart < valueToSubtract->LowPart)
value->HighPart--; // borrow from HighPart
value->HighPart -= valueToSubtract->HighPart;
value->LowPart -= valueToSubtract->LowPart;
}
// Returns position of top non-zero bit,
// eg. -1 = none set, 0 = first bit, 31 = top bit
static int topBit(DWORD value)
{
if (value == 0)
return -1;
else if (value & 0xffff0000)
{ // bit in 0xffff0000 is set
if (value & 0xff000000)
{ // bit in 0xff000000 is set
if (value & 0xf0000000)
{ // bit in 0xf0000000 is set
if (value & 0xc0000000)
{ // bit in 0xc0000000 is set
if (value & 0x80000000)
return 31;
else
return 30;
}
else
{ // bit in 0x30000000 is set
if (value & 0x20000000)
return 29;
else
return 28;
}
}
else
{ // bit in 0x0f000000 is set
if (value & 0x0c000000)
{ // bit in 0x0c000000 is set
if (value & 0x08000000)
return 27;
else
return 26;
}
else
{ // bit in 0x03000000 is set
if (value & 0x02000000)
return 25;
else
return 24;
}
}
}
else
{ // bit in 0x00ff0000 is set
if (value & 0x00f00000)
{ // bit in 0x00f00000 is set
if (value & 0x00c00000)
{ // bit in 0x00c00000 is set
if (value & 0x00800000)
return 23;
else
return 22;
}
else
{ // bit in 0x00300000 is set
if (value & 0x00200000)
return 21;
else
return 20;
}
}
else
{ // bit in 0x000f0000 is set
if (value & 0x000c0000)
{ // bit in 0x000c0000 is set
if (value & 0x00080000)
return 19;
else
return 18;
}
else
{ // bit in 0x00030000 is set
if (value & 0x00020000)
return 17;
else
return 16;
}
}
}
}
else
{ // bit in 0x0000ffff is set
if (value & 0x0000ff00)
{ // bit in 0x0000ff00 is set
if (value & 0x0000f000)
{ // bit in 0x0000f000 is set
if (value & 0x0000c000)
{ // bit in 0x0000c000 is set
if (value & 0x00008000)
return 15;
else
return 14;
}
else
{ // bit in 0x00003000 is set
if (value & 0x00002000)
return 13;
else
return 12;
}
}
else
{ // bit in 0x00000f00 is set
if (value & 0x00000c00)
{ // bit in 0x00000c00 is set
if (value & 0x00000800)
return 11;
else
return 10;
}
else
{ // bit in 0x00000300 is set
if (value & 0x00000200)
return 9;
else
return 8;
}
}
}
else
{ // bit in 0x000000ff is set
if (value & 0x000000f0)
{ // bit in 0x000000f0 is set
if (value & 0x000000c0)
{ // bit in 0x000000c0 is set
if (value & 0x00000080)
return 7;
else
return 6;
}
else
{ // bit in 0x00000030 is set
if (value & 0x00000020)
return 5;
else
return 4;
}
}
else
{ // bit in 0x0000000f is set
if (value & 0x0000000c)
{ // bit in 0x0000000c is set
if (value & 0x00000008)
return 3;
else
return 2;
}
else
{ // bit in 0x00000003 is set
if (value & 0x00000002)
return 1;
else
return 0;
}
}
}
}
}
// Returns position of top non-zero bit,
// eg. -1 = none set, 0 = first bit, 63 = top bit
static int topBit64(ULARGE_INTEGER* value)
{
int result;
if (value == NULL)
return 0;
result = topBit(value->HighPart);
if (result != -1)
return result+32;
else
return topBit(value->LowPart);
}
// Compare two 64-bit values.
// Returns:
// <0 if a is less than b
// 0 if a is equal to b
// >0 if a is greater than b
static int cmp64(const ULARGE_INTEGER* a, const ULARGE_INTEGER* b)
{
if (a == NULL || b == NULL)
return -1; // error but no error return value
if (a->HighPart < b->HighPart)
return -1;
else if (a->HighPart == b->HighPart)
{
if (a->LowPart < b->LowPart)
return -1;
else if (a->LowPart == b->LowPart)
return 0;
else // if (a->LowPart > b->LowPart)
return 1;
}
else // if (a->HighPart < b->HighPart)
return 1;
}
// Add valueToAdd to value (doesn't handle overflow)
static void add64(ULARGE_INTEGER* value, ULARGE_INTEGER* valueToAdd)
{
if (value == NULL || valueToAdd == NULL)
return;
value->LowPart += valueToAdd->LowPart;
if (value->LowPart < valueToAdd->LowPart)
value->HighPart++; // carry to HighPart
value->HighPart += valueToAdd->HighPart;
}
static void div64(ULARGE_INTEGER* value, DWORD divisor)
{
ULARGE_INTEGER result = { 0, 0 };
ULARGE_INTEGER shiftedDivisor; // divisor shifted to left
ULARGE_INTEGER shiftedOne; // '1' shifted to left by same number of bits as divisor
int shift;
if (value == NULL)
return;
if (divisor == 0)
{
value->LowPart = 0;
value->HighPart = 0;
return;
}
if (value->HighPart == 0)
{
if (value->LowPart != 0)
value->LowPart /= divisor;
return;
}
// shift divisor up (into shifted) as far as it can go before it is greater than value
shift = topBit64(value) - topBit(divisor);
shiftedDivisor.LowPart = divisor;
shiftedDivisor.HighPart = 0;
shiftedOne.LowPart = 1;
shiftedOne.HighPart = 0;
shl64(&shiftedDivisor, shift);
shl64(&shiftedOne, shift);
while (shift >= 0)
{
if (cmp64(&shiftedDivisor, value) <= 0)
{
add64(&result, &shiftedOne);
sub64(value, &shiftedDivisor);
}
shr64(&shiftedDivisor, 1);
shr64(&shiftedOne, 1);
shift--;
}
value->HighPart = result.HighPart;
value->LowPart = result.LowPart;
}
///////////////////////////////////////////////////////////////////////////////
// Methods - The meat
///////////////////////////////////////////////////////////////////////////////
// Convert tm to a string in the format "Www Mmm dd hh:mm:ss yyyy",
// where Www is the weekday, Mmm the month in letters, dd the day
// of the month, hh:mm:ss the time, and yyyy the year. The string
// is followed by a newline and a terminating null character,
// conforming a total of 26 characters.
//
char *asctime(const struct tm* tmptr)
{
static char ascbuf[ASC_BUFF_SIZE];
strftime(ascbuf, ASC_BUFF_SIZE, "%c\n", tmptr);
return ascbuf;
}
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
// Return number of clock ticks since process start.
// NOTE: This differs from standard clock since GetTickCount is the
// number of milliseconds since system startup not process start.
// This will also rollover after 49.7 days of continuous system
// runtime.
//
clock_t clock(void)
{
return GetTickCount();
}
// Convert time_t value to string in the same format as asctime.
//
char* ctime(const time_t* timer)
{
return asctime(localtime(timer));
}
// Returns the difference in seconds between the two times.
//
double difftime(time_t timer2, time_t timer1)
{
time_t timediff;
if (timer2 < timer1)
{
timediff = (timer1 - timer2);
}
else
{
timediff = (timer2 - timer1);
}
return (double)timediff;
}
// Reentrant version of gmttime_ce
//
struct tm *gmtime_r(const time_t *timer, struct tm *tmbuf, BOOLEAN local)
{
SYSTEMTIME st;
SecondsSince1970ToSystemTime(timer, &st, local);
SetTz(&st);
if(_isdst) {
SecondsSince1970ToSystemTime(timer, &st, local);
}
// copy SYSTEMTIME data to tm structure
//
SystemTimeToTm(&st, tmbuf);
return tmbuf;
}
// Reentrant version of localtime_ce
//
struct tm *localtime_r(const time_t *timer, struct tm *tmbuf)
{
return gmtime_r(timer, tmbuf, TRUE);
}
// Convert a time_t value to a tm structure as UTC time.
//
struct tm *gmtime(const time_t *timer)
{
return gmtime_r(timer, &tmbuf, FALSE);
}
// Convert a time_t value to a tm structure as local time.
//
struct tm *localtime(const time_t *timer)
{
return localtime_r(timer, &tmbuf);
}
// time_t represents seconds since midnight January 1, 1970 UTC
// (coordinated universal time) in 32-bits Win32 FILETIME structure is 64-bit,
// which represents the number of 100-nanosecond (hns) intervals since
// January 1, 1601 UTC (coordinate universal time) the time difference
// between midnight January 1, 1970 and midnight January 1, 1601 is 11644473600 seconds
//
time_t mktime(struct tm *tptr)
{
SYSTEMTIME st;
int day = 0;
int year = 0;
int seconds = 0;
int overflow;
int tm_year;
int yday, month;
TmToSystemTime(tptr, &st);
SetTz(&st);
// see if seconds are < 0
while(tptr->tm_sec < 0)
{
// steal 60 seconds from the minutes
tptr->tm_sec += 60;
tptr->tm_min--;
}
// roll any seconds > 60 into the minutes
tptr->tm_min += tptr->tm_sec / 60;
// then crop them off
tptr->tm_sec %= 60;
// see if minutes are < 0
while(tptr->tm_min < 0)
{
// steal 60 minutes from the hours
tptr->tm_min += 60;
tptr->tm_hour--;
}
// roll any minutes > 60 into the hours
tptr->tm_hour += tptr->tm_min / 60;
// then crop them off
tptr->tm_min %= 60;
// see if hours are < 0
while(tptr->tm_hour < 0)
{
// steal 24 hours from the days
tptr->tm_hour += 24;
day--;
}
// keep any "excess" days (tm doesn't have a convenient place for this)
day += tptr->tm_hour / 24;
// crop
tptr->tm_hour %= 24;
// roll any months > 12 into the years
tptr->tm_year += tptr->tm_mon / 12;
// then crop the off
tptr->tm_mon %= 12;
// see if months are < 0
if (tptr->tm_mon < 0)
{
// steal 12 months from the years
tptr->tm_mon += 12;
tptr->tm_year--;
}
// add number of days into the month to total day
day += (tptr->tm_mday - 1);
// if days are < 0 then calculate the number of days
// checking to see if the month is a leap year month
while (day < 0)
{
// If months are < 0 then steal 12 months from number of years
// for the day calculation
if(--tptr->tm_mon < 0)
{
tptr->tm_year--;
tptr->tm_mon = 11;
}
day += _ytab[LEAPYEAR(YEAR0 + tptr->tm_year)][tptr->tm_mon];
}
// if day is greater then the number of days in the month
// subtract the number of days in the month and adjust the
// month
while (day >= _ytab[LEAPYEAR(YEAR0 + tptr->tm_year)][tptr->tm_mon])
{
day -= _ytab[LEAPYEAR(YEAR0 + tptr->tm_year)][tptr->tm_mon];
if (++(tptr->tm_mon) == 12)
{
tptr->tm_mon = 0;
tptr->tm_year++;
}
}
tptr->tm_mday = day + 1;
year = EPOCH_YR;
// if year is less then 1970 then return error
if (tptr->tm_year < year - YEAR0) return (time_t) -1;
seconds = 0;
day = 0; // Means days since day 0 now
overflow = 0;
// Assume that when day becomes negative, there will certainly
// be overflow on seconds.
// The check for overflow needs not to be done for leapyears
// divisible by 400.
// The code only works when year (1970) is not a leapyear.
tm_year = tptr->tm_year + YEAR0;
// make sure we are not past the max year for 32-bit number
if (TIME_MAX / 365 < tm_year - year) overflow++;
// calculate number of days since EPOCH
day = (tm_year - year) * 365;
if (TIME_MAX - day < (tm_year - year) / 4 + 1) overflow++;
day += (tm_year - year) / 4 + ((tm_year % 4) && tm_year % 4 < year % 4);
day -= (tm_year - year) / 100 + ((tm_year % 100) && tm_year % 100 < year % 100);
day += (tm_year - year) / 400 + ((tm_year % 400) && tm_year % 400 < year % 400);
// setup for calculation of the yday or Julian day since Jan 1
yday = month = 0;
// add up the number of days for the preceding months
while (month < tptr->tm_mon)
{
yday += _ytab[LEAPYEAR(tm_year)][month];
month++;
}
// add the number of days in the current month
yday += (tptr->tm_mday - 1);
// make sure the didn't overflow
if (day + yday < 0) overflow++;
day += yday;
// set the year day in the structure
tptr->tm_yday = yday;
// calculate the weekday
tptr->tm_wday = (day + 4) % 7; // Day 0 was thursday (4)
// start the seconds calculation by totaling the hours, min, seconds
seconds = ((tptr->tm_hour * 60L) + tptr->tm_min) * 60L + tptr->tm_sec;
// make sure we are not going to overflow
if ((TIME_MAX - seconds) / SECS_DAY < day) overflow++;
// calculate the number of seconds for the number of days
seconds += day * SECS_DAY;
// Now adjust according to timezone and daylight saving time
if (((_localtime > 0) && (TIME_MAX - _localtime < seconds))
|| ((_localtime < 0) && (seconds < -_localtime)))
overflow++;
// Adjust for local time zone
seconds += _localtime;
// return error if we are going to blow the max values
if (overflow) return (time_t) -1;
if ((time_t) seconds != seconds) return (time_t) -1;
// return the number of seconds since EPOCH
return (time_t) seconds;
}
// Convert Win32 FILETIME into time_t
time_t w32_filetime_to_time_t(FILETIME* ft)
{
// make sure ft is at least ftJan1970
if (cmp64((ULARGE_INTEGER*)ft, (ULARGE_INTEGER*)&ftJan1970) < 0)
{
// errno = -1;
return -1;
}
// subtract ftJan1970 from ft
sub64((ULARGE_INTEGER*)ft, (ULARGE_INTEGER*)&ftJan1970);
// divide ft by 10,000,000 to convert from 100-nanosecond units to seconds
div64((ULARGE_INTEGER*)ft, 10000000);
// bound check result
if (ft->dwHighDateTime != 0 || ft->dwLowDateTime >= 2147483648)
{
// errno = -1;
return -1; // value is too big to return in time_t
}
return (time_t)ft->dwLowDateTime;
}
// Get the current system time and convert to seconds since
// 1/1/1970. Store the seconds value in tloc if not a NULL pointer then
// return the seconds value.
//
time_t time(time_t *tloc)
{
SYSTEMTIME st;
time_t secs = 0;
// Get current system time
GetSystemTime(&st);
// Set time zone information
//
SetTz(&st);
// convert system time to number of seconds since 1970
//
/* leg: note: time_t is sometimes unsigned long, and other
* times long long, so we need to cast */
secs = (time_t)SystemTimeToSecondsSince1970(&st);
// check for failure
//
if(secs == TIME_FAILURE)
{
return TIME_FAILURE;
}
// If tloc is not NULL, the return value is also stored in the location to which tloc points
//
if(tloc != NULL)
{
#if 0
if(IsBadWritePtr(tloc, sizeof(time_t)))
{
return TIME_FAILURE;
}
#endif
memcpy(tloc, &secs, sizeof(time_t));
}
return secs;
}
// The strftime function is a modified version created by the following:
// written 6 september 1989 by jim nutt
// released into the public domain by jim nutt
//
// modified 21-Oct-89 by Rob Duff
//
//
// size_t strftime(char *str,
// size_t maxs,
// const char *fmt,
// const struct tm *t)
//
// this functions acts much like a sprintf for time/date output.
// given a pointer to an output buffer, a format string and a
// time, it copies the time to the output buffer formatted in
// accordance with the format string. the parameters are used
// as follows:
//
// str is a pointer to the output buffer, there should
// be at least maxs characters available at the address
// pointed to by str.
//
// maxs is the maximum number of characters to be copied
// into the output buffer, included the '\0' terminator
//
// fmt is the format string. a percent sign (%) is used
// to indicate that the following character is a special
// format character. the following are valid format
// characters:
//
// %A full weekday name (Monday)
// %a abbreviated weekday name (Mon)
// %B full month name (January)
// %b abbreviated month name (Jan)
// %c standard date and time representation
// %d day-of-month (01-31)
// %H hour (24 hour clock) (00-23)
// %I hour (12 hour clock) (01-12)
// %j day-of-year (001-366)
// %M minute (00-59)
// %m month (01-12)
// %p local equivalent of AM or PM
// %S second (00-59)
// %U week-of-year, first day sunday (00-53)
// %W week-of-year, first day monday (00-53)
// %w weekday (0-6, sunday is 0)
// %X standard time representation
// %x standard date representation
// %Y year with century
// %y year without century (00-99)
// %Z timezone name
// %% percent sign
//
// the standard date string is equivalent to:
//
// %a %b %d %Y
//
// the standard time string is equivalent to:
//
// %H:%M:%S
//
// the standard date and time string is equivalent to:
//
// %a %b %d %H:%M:%S %Y
//
// strftime returns the number of characters placed in the
// buffer, not including the terminating \0, or zero if more
// than maxs characters were produced.
//
size_t strftime(char *s, size_t maxs, const char *f, const struct tm *t)
{
int w;
char *p, *q, *r;
static char buf[26];
p = s;
q = s + maxs - 1;
while ((*f != '\0'))
{
if (*f++ == '%')
{
r = buf;
switch (*f++)
{
case '%' :
r = "%";
break;
case 'a' :
r = aday[t->tm_wday];
break;
case 'A' :
r = day[t->tm_wday];
break;
case 'b' :
r = amonth[t->tm_mon];
break;
case 'B' :
r = month[t->tm_mon];
break;
case 'c' :
strfmt(r, "%0 %0 %2 %2:%2:%2 %4",
aday[t->tm_wday], amonth[t->tm_mon],
t->tm_mday,t->tm_hour, t->tm_min,
t->tm_sec, t->tm_year+1900);
break;
case 'd' :
strfmt(r,"%2",t->tm_mday);
break;
case 'H' :
strfmt(r,"%2",t->tm_hour);
break;
case 'I' :
strfmt(r,"%2",(t->tm_hour%12)?t->tm_hour%12:12);
break;