merge the formfield patch from ooo-build
[ooovba.git] / ucb / source / ucp / webdav / DateTimeHelper.cxx
blob4ce2e08c29e5e5ac4813d9d5544a20ad8c80d3b5
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DateTimeHelper.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_ucb.hxx"
34 #include <stdio.h>
35 #include <osl/time.h>
36 #include <com/sun/star/util/DateTime.hpp>
37 #include "DateTimeHelper.hxx"
39 using namespace com::sun::star::util;
40 using namespace rtl;
42 using namespace webdav_ucp;
44 bool DateTimeHelper::ISO8601_To_DateTime (const OUString& s,
45 DateTime& dateTime)
47 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
49 int year, month, day, hours, minutes, off_hours, off_minutes, fix;
50 double seconds;
52 // 2001-01-01T12:30:00Z
53 int n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
54 &year, &month, &day, &hours, &minutes, &seconds );
55 if ( n == 6 )
57 fix = 0;
59 else
61 // 2001-01-01T12:30:00+03:30
62 n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
63 &year, &month, &day, &hours, &minutes, &seconds,
64 &off_hours, &off_minutes );
65 if ( n == 8 )
67 fix = - off_hours * 3600 - off_minutes * 60;
69 else
71 // 2001-01-01T12:30:00-03:30
72 n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
73 &year, &month, &day, &hours, &minutes, &seconds,
74 &off_hours, &off_minutes );
75 if ( n == 8 )
77 fix = off_hours * 3600 + off_minutes * 60;
79 else
81 return false;
86 // Convert to local time...
88 oslDateTime aDateTime;
89 aDateTime.NanoSeconds = 0;
90 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds); // 0-59
91 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes); // 0-59
92 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours); // 0-23
93 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day); // 1-31
94 aDateTime.DayOfWeek = 0; // 0-6, 0 = Sunday
95 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month); // 1-12
96 aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
98 TimeValue aTimeValue;
99 if ( osl_getTimeValueFromDateTime( &aDateTime, &aTimeValue ) )
101 aTimeValue.Seconds += fix;
103 if ( osl_getLocalTimeFromSystemTime( &aTimeValue, &aTimeValue ) )
105 if ( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) )
107 dateTime.Year = aDateTime.Year;
108 dateTime.Month = aDateTime.Month;
109 dateTime.Day = aDateTime.Day;
110 dateTime.Hours = aDateTime.Hours;
111 dateTime.Minutes = aDateTime.Minutes;
112 dateTime.Seconds = aDateTime.Seconds;
114 return true;
119 return false;
123 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
125 if (day.compareToAscii ("Sun") == 0)
126 return 0;
127 else if (day.compareToAscii ("Mon") == 0)
128 return 1;
129 else if (day.compareToAscii ("Tue") == 0)
130 return 2;
131 else if (day.compareToAscii ("Wed") == 0)
132 return 3;
133 else if (day.compareToAscii ("Thu") == 0)
134 return 4;
135 else if (day.compareToAscii ("Fri") == 0)
136 return 5;
137 else if (day.compareToAscii ("Sat") == 0)
138 return 6;
139 else
140 return -1;
144 sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
146 if (month.compareToAscii ("Jan") == 0)
147 return 1;
148 else if (month.compareToAscii ("Feb") == 0)
149 return 2;
150 else if (month.compareToAscii ("Mar") == 0)
151 return 3;
152 else if (month.compareToAscii ("Apr") == 0)
153 return 4;
154 else if (month.compareToAscii ("May") == 0)
155 return 5;
156 else if (month.compareToAscii ("Jun") == 0)
157 return 6;
158 else if (month.compareToAscii ("Jul") == 0)
159 return 7;
160 else if (month.compareToAscii ("Aug") == 0)
161 return 8;
162 else if (month.compareToAscii ("Sep") == 0)
163 return 9;
164 else if (month.compareToAscii ("Oct") == 0)
165 return 10;
166 else if (month.compareToAscii ("Nov") == 0)
167 return 11;
168 else if (month.compareToAscii ("Dec") == 0)
169 return 12;
170 else
171 return 0;
174 bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
175 DateTime& dateTime)
177 int year;
178 int day;
179 int hours;
180 int minutes;
181 int seconds;
182 sal_Char string_month[3 + 1];
183 sal_Char string_day[3 + 1];
185 sal_Int32 found = s.indexOf (',');
186 if (found != -1)
188 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
190 // RFC 1123
191 found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
192 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
193 if (found != 7)
195 // RFC 1036
196 found = sscanf (aDT.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
197 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
199 found = (found == 7) ? 1 : 0;
201 else
203 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
205 // ANSI C's asctime () format
206 found = sscanf (aDT.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
207 string_day, string_month,
208 &day, &hours, &minutes, &seconds, &year);
209 found = (found == 7) ? 1 : 0;
212 if (found)
214 found = 0;
216 int month = DateTimeHelper::convertMonthToInt (
217 OUString::createFromAscii (string_month));
218 if (month)
220 // Convert to local time...
222 oslDateTime aDateTime;
223 aDateTime.NanoSeconds = 0;
224 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
225 // 0-59
226 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
227 // 0-59
228 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
229 // 0-23
230 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
231 // 1-31
232 aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
233 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
234 // 1-12
235 aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
237 TimeValue aTimeValue;
238 if ( osl_getTimeValueFromDateTime( &aDateTime,
239 &aTimeValue ) )
241 if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
242 &aTimeValue ) )
244 if ( osl_getDateTimeFromTimeValue( &aTimeValue,
245 &aDateTime ) )
247 dateTime.Year = aDateTime.Year;
248 dateTime.Month = aDateTime.Month;
249 dateTime.Day = aDateTime.Day;
250 dateTime.Hours = aDateTime.Hours;
251 dateTime.Minutes = aDateTime.Minutes;
252 dateTime.Seconds = aDateTime.Seconds;
254 found = 1;
261 return (found) ? true : false;
264 bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
266 if (ISO8601_To_DateTime (s, dateTime))
267 return true;
268 else if (RFC2068_To_DateTime (s, dateTime))
269 return true;
270 else
271 return false;