16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
*
* Copyright (c) 1992-1995 Karl Lehenbauer and Mark Diekhans.
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclDate.c,v 1.10 2000/01/11 02:59:47 ericm Exp $
*/
#include "tclInt.h"
#include "tclPort.h"
#ifdef MAC_TCL
# define EPOCH 1904
|
|
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
*
* Copyright (c) 1992-1995 Karl Lehenbauer and Mark Diekhans.
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclDate.c,v 1.11 2000/01/12 01:16:08 ericm Exp $
*/
#include "tclInt.h"
#include "tclPort.h"
#ifdef MAC_TCL
# define EPOCH 1904
|
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
|
if (Hours < 1 || Hours > 12)
return -1;
return (((Hours % 12) + 12) * 60L + Minutes) * 60L + Seconds;
}
return -1; /* Should never be reached */
}
static int
Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode, TimePtr)
time_t Month;
time_t Day;
time_t Year;
time_t Hours;
time_t Minutes;
time_t Seconds;
MERIDIAN Meridian;
DSTMODE DSTmode;
time_t *TimePtr;
{
static int DaysInMonth[12] = {
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
time_t tod;
time_t Julian;
int i;
DaysInMonth[1] = (Year % 4 == 0) && (Year % 100 != 0 || Year % 400 == 0)
? 29 : 28;
if (Month < 1 || Month > 12
|| Year < START_OF_TIME || Year > END_OF_TIME
|| Day < 1 || Day > DaysInMonth[(int)--Month])
return -1;
for (Julian = Day - 1, i = 0; i < Month; i++)
Julian += DaysInMonth[i];
if (Year >= EPOCH) {
for (i = EPOCH; i < Year; i++)
Julian += 365 + (((i % 4) == 0) &&
(((i % 100) != 0) || ((i % 400) == 0)));
} else {
for (i = Year; i < EPOCH; i++)
Julian -= 365 + (((i % 4) == 0) &&
(((i % 100) != 0) || ((i % 400) == 0)));
}
Julian *= SECSPERDAY;
Julian += TclDateTimezone * 60L;
if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
return -1;
Julian += tod;
if (DSTmode == DSTon
|| (DSTmode == DSTmaybe && TclpGetDate((TclpTime_t)&Julian, 0)->tm_isdst))
Julian -= 60 * 60;
*TimePtr = Julian;
return 0;
}
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
if (Hours < 1 || Hours > 12)
return -1;
return (((Hours % 12) + 12) * 60L + Minutes) * 60L + Seconds;
}
return -1; /* Should never be reached */
}
/*
*-----------------------------------------------------------------------------
*
* Convert --
*
* Convert a {month, day, year, hours, minutes, seconds, meridian, dst}
* tuple into a clock seconds value.
*
* Results:
* 0 or -1 indicating success or failure.
*
* Side effects:
* Fills TimePtr with the computed value.
*
*-----------------------------------------------------------------------------
*/
static int
Convert(Month, Day, Year, Hours, Minutes, Seconds, Meridian, DSTmode, TimePtr)
time_t Month;
time_t Day;
time_t Year;
time_t Hours;
time_t Minutes;
time_t Seconds;
MERIDIAN Meridian;
DSTMODE DSTmode;
time_t *TimePtr;
{
static int DaysInMonth[12] = {
31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
time_t tod;
time_t Julian;
int i;
/* Figure out how many days are in February for the given year.
* Every year divisible by 4 is a leap year.
* But, every year divisible by 100 is not a leap year.
* But, every year divisible by 400 is a leap year after all.
*/
DaysInMonth[1] = (Year % 4 == 0) && (Year % 100 != 0 || Year % 400 == 0)
? 29 : 28;
/* Check the inputs for validity */
if (Month < 1 || Month > 12
|| Year < START_OF_TIME || Year > END_OF_TIME
|| Day < 1 || Day > DaysInMonth[(int)--Month])
return -1;
/* Start computing the value. First determine the number of days
* represented by the date, then multiply by the number of seconds/day.
*/
for (Julian = Day - 1, i = 0; i < Month; i++)
Julian += DaysInMonth[i];
if (Year >= EPOCH) {
for (i = EPOCH; i < Year; i++)
Julian += 365 + (((i % 4) == 0) &&
(((i % 100) != 0) || ((i % 400) == 0)));
} else {
for (i = Year; i < EPOCH; i++)
Julian -= 365 + (((i % 4) == 0) &&
(((i % 100) != 0) || ((i % 400) == 0)));
}
Julian *= SECSPERDAY;
/* Add the timezone offset ?? */
Julian += TclDateTimezone * 60L;
/* Add the number of seconds represented by the time component */
if ((tod = ToSeconds(Hours, Minutes, Seconds, Meridian)) < 0)
return -1;
Julian += tod;
/* Perform a preliminary DST compensation ?? */
if (DSTmode == DSTon
|| (DSTmode == DSTmaybe && TclpGetDate((TclpTime_t)&Julian, 0)->tm_isdst))
Julian -= 60 * 60;
*TimePtr = Julian;
return 0;
}
|