Bump for 3.6-28
[LibreOffice.git] / ucb / source / ucp / webdav / DateTimeHelper.cxx
blob7044aaee94221f1e27779eef46932e5bafa20dab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <stdio.h>
31 #include <osl/time.h>
32 #include <com/sun/star/util/DateTime.hpp>
33 #include "DateTimeHelper.hxx"
35 using namespace com::sun::star::util;
37 using namespace webdav_ucp;
39 using ::rtl::OUString;
40 using ::rtl::OString;
42 bool DateTimeHelper::ISO8601_To_DateTime (const OUString& s,
43 DateTime& dateTime)
45 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
47 int year, month, day, hours, minutes, off_hours, off_minutes, fix;
48 double seconds;
50 // 2001-01-01T12:30:00Z
51 int n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
52 &year, &month, &day, &hours, &minutes, &seconds );
53 if ( n == 6 )
55 fix = 0;
57 else
59 // 2001-01-01T12:30:00+03:30
60 n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
61 &year, &month, &day, &hours, &minutes, &seconds,
62 &off_hours, &off_minutes );
63 if ( n == 8 )
65 fix = - off_hours * 3600 - off_minutes * 60;
67 else
69 // 2001-01-01T12:30:00-03:30
70 n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
71 &year, &month, &day, &hours, &minutes, &seconds,
72 &off_hours, &off_minutes );
73 if ( n == 8 )
75 fix = off_hours * 3600 + off_minutes * 60;
77 else
79 return false;
84 // Convert to local time...
86 oslDateTime aDateTime;
87 aDateTime.NanoSeconds = 0;
88 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds); // 0-59
89 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes); // 0-59
90 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours); // 0-23
91 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day); // 1-31
92 aDateTime.DayOfWeek = 0; // 0-6, 0 = Sunday
93 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month); // 1-12
94 aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
96 TimeValue aTimeValue;
97 if ( osl_getTimeValueFromDateTime( &aDateTime, &aTimeValue ) )
99 aTimeValue.Seconds += fix;
101 if ( osl_getLocalTimeFromSystemTime( &aTimeValue, &aTimeValue ) )
103 if ( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) )
105 dateTime.Year = aDateTime.Year;
106 dateTime.Month = aDateTime.Month;
107 dateTime.Day = aDateTime.Day;
108 dateTime.Hours = aDateTime.Hours;
109 dateTime.Minutes = aDateTime.Minutes;
110 dateTime.Seconds = aDateTime.Seconds;
112 return true;
117 return false;
121 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
123 if (day.compareToAscii ("Sun") == 0)
124 return 0;
125 else if (day.compareToAscii ("Mon") == 0)
126 return 1;
127 else if (day.compareToAscii ("Tue") == 0)
128 return 2;
129 else if (day.compareToAscii ("Wed") == 0)
130 return 3;
131 else if (day.compareToAscii ("Thu") == 0)
132 return 4;
133 else if (day.compareToAscii ("Fri") == 0)
134 return 5;
135 else if (day.compareToAscii ("Sat") == 0)
136 return 6;
137 else
138 return -1;
142 sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
144 if (month.compareToAscii ("Jan") == 0)
145 return 1;
146 else if (month.compareToAscii ("Feb") == 0)
147 return 2;
148 else if (month.compareToAscii ("Mar") == 0)
149 return 3;
150 else if (month.compareToAscii ("Apr") == 0)
151 return 4;
152 else if (month.compareToAscii ("May") == 0)
153 return 5;
154 else if (month.compareToAscii ("Jun") == 0)
155 return 6;
156 else if (month.compareToAscii ("Jul") == 0)
157 return 7;
158 else if (month.compareToAscii ("Aug") == 0)
159 return 8;
160 else if (month.compareToAscii ("Sep") == 0)
161 return 9;
162 else if (month.compareToAscii ("Oct") == 0)
163 return 10;
164 else if (month.compareToAscii ("Nov") == 0)
165 return 11;
166 else if (month.compareToAscii ("Dec") == 0)
167 return 12;
168 else
169 return 0;
172 bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
173 DateTime& dateTime)
175 int year;
176 int day;
177 int hours;
178 int minutes;
179 int seconds;
180 sal_Char string_month[3 + 1];
181 sal_Char string_day[3 + 1];
183 sal_Int32 found = s.indexOf (',');
184 if (found != -1)
186 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
188 // RFC 1123
189 found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
190 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
191 if (found != 7)
193 // RFC 1036
194 found = sscanf (aDT.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
195 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
197 found = (found == 7) ? 1 : 0;
199 else
201 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
203 // ANSI C's asctime () format
204 found = sscanf (aDT.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
205 string_day, string_month,
206 &day, &hours, &minutes, &seconds, &year);
207 found = (found == 7) ? 1 : 0;
210 if (found)
212 found = 0;
214 int month = DateTimeHelper::convertMonthToInt (
215 OUString::createFromAscii (string_month));
216 if (month)
218 // Convert to local time...
220 oslDateTime aDateTime;
221 aDateTime.NanoSeconds = 0;
222 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
223 // 0-59
224 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
225 // 0-59
226 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
227 // 0-23
228 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
229 // 1-31
230 aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
231 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
232 // 1-12
233 aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
235 TimeValue aTimeValue;
236 if ( osl_getTimeValueFromDateTime( &aDateTime,
237 &aTimeValue ) )
239 if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
240 &aTimeValue ) )
242 if ( osl_getDateTimeFromTimeValue( &aTimeValue,
243 &aDateTime ) )
245 dateTime.Year = aDateTime.Year;
246 dateTime.Month = aDateTime.Month;
247 dateTime.Day = aDateTime.Day;
248 dateTime.Hours = aDateTime.Hours;
249 dateTime.Minutes = aDateTime.Minutes;
250 dateTime.Seconds = aDateTime.Seconds;
252 found = 1;
259 return (found) ? true : false;
262 bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
264 if (ISO8601_To_DateTime (s, dateTime))
265 return true;
266 else if (RFC2068_To_DateTime (s, dateTime))
267 return true;
268 else
269 return false;
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */