update dev300-m57
[ooovba.git] / lotuswordpro / source / filter / localtime.cxx
blob82aeaa98ce8cd49eaf513635287de1f7d4f32c96
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,
28 * MA 02111-1307 USA
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"
56 #include <limits.h>
57 #include <unicode/timezone.h>
58 //End by
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;
62 #ifndef LONG_MAX
63 const long LONG_MAX=2147483647;
64 #endif
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)
75 if (rtime < 0)
77 return false;
79 //is-current-year-a-leap-year flag
80 int islpyr = 0;
82 long tmptim;
83 long caltim = rtime;
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.
96 tmptim++;
97 caltim -= YEAR_SEC;
99 if ( caltim >= YEAR_SEC )
101 // 1972, 1976, 1980,...,etc.
102 tmptim++;
103 caltim -= YEAR_SEC;
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.
109 tmptim++;
110 caltim -= (YEAR_SEC + DAY_SEC);
112 else
114 //In a leap year after all, set the flag.
115 islpyr++;
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)
134 long* mdays;
135 if ( islpyr )
137 mdays = _lpdays;
139 else
141 mdays = _days;
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);
164 rtm.tm_isdst = 0;
169 //adjust year & month
170 rtm.tm_year += 1900;
171 ++(rtm.tm_mon);
173 return true;
176 bool LtgLocalTime(long rtime,LtTm& rtm)
178 long ltime;
179 if (rtime < 0)
181 return false;
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);
191 return false;