Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * The Contents of this file are made available subject to the terms of
5 : : * either of the following licenses
6 : : *
7 : : * - GNU Lesser General Public License Version 2.1
8 : : * - Sun Industry Standards Source License Version 1.1
9 : : *
10 : : * Sun Microsystems Inc., October, 2000
11 : : *
12 : : * GNU Lesser General Public License Version 2.1
13 : : * =============================================
14 : : * Copyright 2000 by Sun Microsystems, Inc.
15 : : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : : *
17 : : * This library is free software; you can redistribute it and/or
18 : : * modify it under the terms of the GNU Lesser General Public
19 : : * License version 2.1, as published by the Free Software Foundation.
20 : : *
21 : : * This library is distributed in the hope that it will be useful,
22 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : : * Lesser General Public License for more details.
25 : : *
26 : : * You should have received a copy of the GNU Lesser General Public
27 : : * License along with this library; if not, write to the Free Software
28 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : : * MA 02111-1307 USA
30 : : *
31 : : *
32 : : * Sun Industry Standards Source License Version 1.1
33 : : * =================================================
34 : : * The contents of this file are subject to the Sun Industry Standards
35 : : * Source License Version 1.1 (the "License"); You may not use this file
36 : : * except in compliance with the License. You may obtain a copy of the
37 : : * License at http://www.openoffice.org/license.html.
38 : : *
39 : : * Software provided under this License is provided on an "AS IS" basis,
40 : : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : : * See the License for the specific provisions governing your rights and
44 : : * obligations concerning the Software.
45 : : *
46 : : * The Initial Developer of the Original Code is: IBM Corporation
47 : : *
48 : : * Copyright: 2008 by IBM Corporation
49 : : *
50 : : * All Rights Reserved.
51 : : *
52 : : * Contributor(s): _______________________________________
53 : : *
54 : : *
55 : : ************************************************************************/
56 : : #include "localtime.hxx"
57 : : #include <limits.h>
58 : : #include <unicode/timezone.h>
59 : : //End by
60 : : const long DAY_SEC =24 * 60 * 60;
61 : : const long YEAR_SEC = 365 * DAY_SEC;
62 : : const long FOURYEAR_SEC = 4 * YEAR_SEC + DAY_SEC;
63 : : #ifndef LONG_MAX
64 : : const long LONG_MAX=2147483647;
65 : : #endif
66 : : const long TIMEZONE = -28800;
67 : : //01-01-70 was a Thursday
68 : : const long BASE_DOW = 4;
69 : :
70 : : long _lpdays[] = {-1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
71 : :
72 : : long _days[] = {-1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364};
73 : :
74 : 40 : bool LtgGmTime(long rtime,LtTm& rtm)
75 : : {
76 [ - + ]: 40 : if (rtime < 0)
77 : : {
78 : 0 : return false;
79 : : }
80 : : //is-current-year-a-leap-year flag
81 : 40 : int islpyr = 0;
82 : :
83 : : long tmptim;
84 : 40 : long caltim = rtime;
85 : 40 : tmptim = (long)(caltim / FOURYEAR_SEC);
86 : 40 : caltim -= ((long)tmptim * FOURYEAR_SEC);
87 : :
88 : : //
89 : : //Determine which year of the interval
90 : : //
91 : : // 1970, 1974, 1978,...,etc.
92 : 40 : tmptim = (tmptim * 4) + 70;
93 : :
94 [ + - ]: 40 : if (caltim >= YEAR_SEC)
95 : : {
96 : : //1971, 1975, 1979,...,etc.
97 : 40 : tmptim++;
98 : 40 : caltim -= YEAR_SEC;
99 : :
100 [ + - ]: 40 : if ( caltim >= YEAR_SEC )
101 : : {
102 : : // 1972, 1976, 1980,...,etc.
103 : 40 : tmptim++;
104 : 40 : caltim -= YEAR_SEC;
105 : :
106 : : //Note, it takes 366 days-worth of seconds to get past a leap year.
107 [ - + ]: 40 : if (caltim >= (YEAR_SEC + DAY_SEC))
108 : : {
109 : : //1973, 1977, 1981,...,etc.
110 : 0 : tmptim++;
111 : 0 : caltim -= (YEAR_SEC + DAY_SEC);
112 : : }
113 : : else
114 : : {
115 : : //In a leap year after all, set the flag.
116 : 40 : islpyr++;
117 : : }
118 : : }
119 : : }
120 : :
121 : : //
122 : : //tmptim now holds the value for tm_year. caltim now holds the
123 : : //number of elapsed seconds since the beginning of that year.
124 : : //
125 : 40 : rtm.tm_year = tmptim;
126 : : //
127 : : //Determine days since January 1 (0 - 365). This is the tm_yday value.
128 : : //Leave caltim with number of elapsed seconds in that day.
129 : : //
130 : 40 : rtm.tm_yday = (long)(caltim / DAY_SEC);
131 : 40 : caltim -= (long)(rtm.tm_yday) * DAY_SEC;
132 : : //
133 : : //Determine months since January (0 - 11) and day of month (1 - 31)
134 : : //
135 : : long* mdays;
136 [ + - ]: 40 : if ( islpyr )
137 : : {
138 : 40 : mdays = _lpdays;
139 : : }
140 : : else
141 : : {
142 : 0 : mdays = _days;
143 : : }
144 : :
145 [ + + ]: 280 : for ( tmptim = 1 ; mdays[tmptim] < rtm.tm_yday ; tmptim++ ) ;
146 : :
147 : 40 : rtm.tm_mon = --tmptim;
148 : :
149 : 40 : rtm.tm_mday = rtm.tm_yday - mdays[tmptim];
150 : :
151 : : //
152 : : //Determine days since Sunday (0 - 6)
153 : : //
154 : 40 : rtm.tm_wday = ((long)(rtime / DAY_SEC) + BASE_DOW) % 7;
155 : : //
156 : : //Determine hours since midnight (0 - 23), minutes after the hour
157 : : //(0 - 59), and seconds after the minute (0 - 59).
158 : : //
159 : 40 : rtm.tm_hour = (long)(caltim / 3600);
160 : 40 : caltim -= (long)rtm.tm_hour * 3600;
161 : :
162 : 40 : rtm.tm_min = (long)(caltim / 60);
163 : 40 : rtm.tm_sec = (long)(caltim - (rtm.tm_min) * 60);
164 : :
165 : 40 : rtm.tm_isdst = 0;
166 : :
167 : : //
168 : : //
169 : : //
170 : : //adjust year & month
171 : 40 : rtm.tm_year += 1900;
172 : 40 : ++(rtm.tm_mon);
173 : :
174 : 40 : return true;
175 : :
176 : : };
177 : 40 : bool LtgLocalTime(long rtime,LtTm& rtm)
178 : : {
179 [ - + ]: 40 : if (rtime < 0)
180 : : {
181 : 0 : return false;
182 : : }
183 : :
184 [ + - ][ + - ]: 40 : if ((rtime > 3 * DAY_SEC)&&(rtime < LONG_MAX - 3 * DAY_SEC))
185 : : {
186 : 40 : TimeZone* pLocalZone = TimeZone::createDefault();
187 : 40 : long offset = (pLocalZone->getRawOffset())/1000;
188 [ + - ]: 40 : delete pLocalZone;
189 : 40 : long ltime = rtime + offset;
190 : 40 : return LtgGmTime(ltime,rtm);
191 : : }
192 : 40 : return false;
193 : : };
194 : :
195 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|