LibreOffice Module lotuswordpro (master) 1
localtime.cxx
Go to the documentation of this file.
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#include <memory>
60
61const tools::Long DAY_SEC = 24 * 60 * 60;
64#ifndef LONG_MAX
65const long LONG_MAX = 2147483647;
66#endif
67//01-01-70 was a Thursday
69
70bool LtgGmTime(tools::Long rtime, LtTm& rtm)
71{
72 if (rtime < 0)
73 {
74 return false;
75 }
76 //is-current-year-a-leap-year flag
77 int islpyr = 0;
78
79 tools::Long tmptim;
80 tools::Long caltim = rtime;
81 tmptim = static_cast<tools::Long>(caltim / FOURYEAR_SEC);
82 caltim -= tmptim * FOURYEAR_SEC;
83
84 //Determine which year of the interval
85
86 // 1970, 1974, 1978,...,etc.
87 tmptim = (tmptim * 4) + 70;
88
89 if (caltim >= YEAR_SEC)
90 {
91 //1971, 1975, 1979,...,etc.
92 tmptim++;
93 caltim -= YEAR_SEC;
94
95 if (caltim >= YEAR_SEC)
96 {
97 // 1972, 1976, 1980,...,etc.
98 tmptim++;
99 caltim -= YEAR_SEC;
100
101 //Note, it takes 366 days-worth of seconds to get past a leap year.
102 if (caltim >= (YEAR_SEC + DAY_SEC))
103 {
104 //1973, 1977, 1981,...,etc.
105 tmptim++;
106 caltim -= (YEAR_SEC + DAY_SEC);
107 }
108 else
109 {
110 //In a leap year after all, set the flag.
111 islpyr++;
112 }
113 }
114 }
115
116 //tmptim now holds the value for tm_year. caltim now holds the
117 //number of elapsed seconds since the beginning of that year.
118
119 rtm.tm_year = tmptim;
120
121 //Determine days since January 1 (0 - 365). This is the tm_yday value.
122 //Leave caltim with number of elapsed seconds in that day.
123
124 rtm.tm_yday = static_cast<tools::Long>(caltim / DAY_SEC);
125 caltim -= rtm.tm_yday * DAY_SEC;
126
127 //Determine months since January (0 - 11) and day of month (1 - 31)
128
129 tools::Long const* mdays;
130 if (islpyr)
131 {
132 static tools::Long const lpdays[]
133 = { -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
134 mdays = lpdays;
135 }
136 else
137 {
138 static tools::Long const days[]
139 = { -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364 };
140 mdays = days;
141 }
142
143 for (tmptim = 1; mdays[tmptim] < rtm.tm_yday; tmptim++)
144 ;
145
146 rtm.tm_mon = --tmptim;
147
148 rtm.tm_mday = rtm.tm_yday - mdays[tmptim];
149
150 //Determine days since Sunday (0 - 6)
151
152 rtm.tm_wday = (static_cast<tools::Long>(rtime / DAY_SEC) + BASE_DOW) % 7;
153
154 //Determine hours since midnight (0 - 23), minutes after the hour
155 //(0 - 59), and seconds after the minute (0 - 59).
156
157 rtm.tm_hour = static_cast<tools::Long>(caltim / 3600);
158 caltim -= rtm.tm_hour * 3600;
159
160 rtm.tm_min = static_cast<tools::Long>(caltim / 60);
161 rtm.tm_sec = static_cast<tools::Long>(caltim - (rtm.tm_min) * 60);
162
163 //adjust year & month
164 rtm.tm_year += 1900;
165 ++(rtm.tm_mon);
166
167 return true;
168};
170{
171 if (rtime < 0)
172 {
173 return false;
174 }
175
176 if ((rtime > 3 * DAY_SEC) && (rtime < LONG_MAX - 3 * DAY_SEC))
177 {
178 std::unique_ptr<icu::TimeZone> pLocalZone(icu::TimeZone::createDefault());
179 tools::Long offset = (pLocalZone->getRawOffset()) / 1000;
180 pLocalZone.reset();
181 tools::Long ltime = rtime + offset;
182 return LtgGmTime(ltime, rtm);
183 }
184 return false;
185};
186
187/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const tools::Long FOURYEAR_SEC
Definition: localtime.cxx:63
const long LONG_MAX
Definition: localtime.cxx:65
const tools::Long DAY_SEC
Definition: localtime.cxx:61
const tools::Long BASE_DOW
Definition: localtime.cxx:68
bool LtgLocalTime(tools::Long rtime, LtTm &rtm)
Definition: localtime.cxx:169
bool LtgGmTime(tools::Long rtime, LtTm &rtm)
Definition: localtime.cxx:70
const tools::Long YEAR_SEC
Definition: localtime.cxx:62
long Long
tools::Long tm_wday
Definition: localtime.hxx:68
tools::Long tm_mday
Definition: localtime.hxx:65
tools::Long tm_min
Definition: localtime.hxx:63
tools::Long tm_sec
Definition: localtime.hxx:62
tools::Long tm_hour
Definition: localtime.hxx:64
tools::Long tm_yday
Definition: localtime.hxx:69
tools::Long tm_mon
Definition: localtime.hxx:66
tools::Long tm_year
Definition: localtime.hxx:67