1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * either of the following licenses
6 * - GNU Lesser General Public License Version 2.1
7 * - Sun Industry Standards Source License Version 1.1
9 * Sun Microsystems Inc., October, 2000
11 * GNU Lesser General Public License Version 2.1
12 * =============================================
13 * Copyright 2000 by Sun Microsystems, Inc.
14 * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License version 2.1, as published by the Free Software Foundation.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
31 * Sun Industry Standards Source License Version 1.1
32 * =================================================
33 * The contents of this file are subject to the Sun Industry Standards
34 * Source License Version 1.1 (the "License"); You may not use this file
35 * except in compliance with the License. You may obtain a copy of the
36 * License at http://www.openoffice.org/license.html.
38 * Software provided under this License is provided on an "AS IS" basis,
39 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
40 * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
41 * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
42 * See the License for the specific provisions governing your rights and
43 * obligations concerning the Software.
45 * The Initial Developer of the Original Code is: IBM Corporation
47 * Copyright: 2008 by IBM Corporation
49 * All Rights Reserved.
51 * Contributor(s): _______________________________________
54 ************************************************************************/
55 #include "localtime.hxx"
57 #include <unicode/timezone.h>
59 const long DAY_SEC
=24 * 60 * 60;
60 const long YEAR_SEC
= 365 * DAY_SEC
;
61 const long FOURYEAR_SEC
= 4 * YEAR_SEC
+ DAY_SEC
;
63 const long LONG_MAX
=2147483647;
65 const long TIMEZONE
= -28800;
66 //01-01-70 was a Thursday
67 const long BASE_DOW
= 4;
69 long _lpdays
[] = {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
71 long _days
[] = {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364};
73 bool LtgGmTime(long rtime
,LtTm
& rtm
)
79 //is-current-year-a-leap-year flag
84 tmptim
= (long)(caltim
/ FOURYEAR_SEC
);
85 caltim
-= ((long)tmptim
* FOURYEAR_SEC
);
88 //Determine which year of the interval
90 // 1970, 1974, 1978,...,etc.
91 tmptim
= (tmptim
* 4) + 70;
93 if (caltim
>= YEAR_SEC
)
95 //1971, 1975, 1979,...,etc.
99 if ( caltim
>= YEAR_SEC
)
101 // 1972, 1976, 1980,...,etc.
105 //Note, it takes 366 days-worth of seconds to get past a leap year.
106 if (caltim
>= (YEAR_SEC
+ DAY_SEC
))
108 //1973, 1977, 1981,...,etc.
110 caltim
-= (YEAR_SEC
+ DAY_SEC
);
114 //In a leap year after all, set the flag.
121 //tmptim now holds the value for tm_year. caltim now holds the
122 //number of elapsed seconds since the beginning of that year.
124 rtm
.tm_year
= tmptim
;
126 //Determine days since January 1 (0 - 365). This is the tm_yday value.
127 //Leave caltim with number of elapsed seconds in that day.
129 rtm
.tm_yday
= (long)(caltim
/ DAY_SEC
);
130 caltim
-= (long)(rtm
.tm_yday
) * DAY_SEC
;
132 //Determine months since January (0 - 11) and day of month (1 - 31)
144 for ( tmptim
= 1 ; mdays
[tmptim
] < rtm
.tm_yday
; tmptim
++ ) ;
146 rtm
.tm_mon
= --tmptim
;
148 rtm
.tm_mday
= rtm
.tm_yday
- mdays
[tmptim
];
151 //Determine days since Sunday (0 - 6)
153 rtm
.tm_wday
= ((long)(rtime
/ DAY_SEC
) + BASE_DOW
) % 7;
155 //Determine hours since midnight (0 - 23), minutes after the hour
156 //(0 - 59), and seconds after the minute (0 - 59).
158 rtm
.tm_hour
= (long)(caltim
/ 3600);
159 caltim
-= (long)rtm
.tm_hour
* 3600;
161 rtm
.tm_min
= (long)(caltim
/ 60);
162 rtm
.tm_sec
= (long)(caltim
- (rtm
.tm_min
) * 60);
169 //adjust year & month
176 bool LtgLocalTime(long rtime
,LtTm
& rtm
)
184 if ((rtime
> 3 * DAY_SEC
)&&(rtime
< LONG_MAX
- 3 * DAY_SEC
))
186 TimeZone
* pLocalZone
= TimeZone::createDefault();
187 long offset
= (pLocalZone
->getRawOffset())/1000;
188 ltime
= rtime
+ offset
;
189 return LtgGmTime(ltime
,rtm
);