12f86921cSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
32f86921cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
42f86921cSAndrew Rist * or more contributor license agreements. See the NOTICE file
52f86921cSAndrew Rist * distributed with this work for additional information
62f86921cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
72f86921cSAndrew Rist * to you under the Apache License, Version 2.0 (the
82f86921cSAndrew Rist * "License"); you may not use this file except in compliance
92f86921cSAndrew Rist * with the License. You may obtain a copy of the License at
102f86921cSAndrew Rist *
112f86921cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
122f86921cSAndrew Rist *
132f86921cSAndrew Rist * Unless required by applicable law or agreed to in writing,
142f86921cSAndrew Rist * software distributed under the License is distributed on an
152f86921cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162f86921cSAndrew Rist * KIND, either express or implied. See the License for the
172f86921cSAndrew Rist * specific language governing permissions and limitations
182f86921cSAndrew Rist * under the License.
192f86921cSAndrew Rist *
202f86921cSAndrew Rist *************************************************************/
212f86921cSAndrew Rist
222f86921cSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*421ed02eSdamjan #include "precompiled_webdav.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <stdio.h>
28cdf0e10cSrcweir #include <osl/time.h>
29cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp>
30cdf0e10cSrcweir #include "DateTimeHelper.hxx"
31cdf0e10cSrcweir
32cdf0e10cSrcweir using namespace com::sun::star::util;
33cdf0e10cSrcweir using namespace rtl;
34cdf0e10cSrcweir
3559ddfc10SAndre Fischer using namespace http_dav_ucp;
36cdf0e10cSrcweir
ISO8601_To_DateTime(const OUString & s,DateTime & dateTime)37cdf0e10cSrcweir bool DateTimeHelper::ISO8601_To_DateTime (const OUString& s,
38cdf0e10cSrcweir DateTime& dateTime)
39cdf0e10cSrcweir {
40cdf0e10cSrcweir OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
41cdf0e10cSrcweir
42cdf0e10cSrcweir int year, month, day, hours, minutes, off_hours, off_minutes, fix;
43cdf0e10cSrcweir double seconds;
44cdf0e10cSrcweir
45cdf0e10cSrcweir // 2001-01-01T12:30:00Z
46cdf0e10cSrcweir int n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
47cdf0e10cSrcweir &year, &month, &day, &hours, &minutes, &seconds );
48cdf0e10cSrcweir if ( n == 6 )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir fix = 0;
51cdf0e10cSrcweir }
52cdf0e10cSrcweir else
53cdf0e10cSrcweir {
54cdf0e10cSrcweir // 2001-01-01T12:30:00+03:30
55cdf0e10cSrcweir n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
56cdf0e10cSrcweir &year, &month, &day, &hours, &minutes, &seconds,
57cdf0e10cSrcweir &off_hours, &off_minutes );
58cdf0e10cSrcweir if ( n == 8 )
59cdf0e10cSrcweir {
60cdf0e10cSrcweir fix = - off_hours * 3600 - off_minutes * 60;
61cdf0e10cSrcweir }
62cdf0e10cSrcweir else
63cdf0e10cSrcweir {
64cdf0e10cSrcweir // 2001-01-01T12:30:00-03:30
65cdf0e10cSrcweir n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
66cdf0e10cSrcweir &year, &month, &day, &hours, &minutes, &seconds,
67cdf0e10cSrcweir &off_hours, &off_minutes );
68cdf0e10cSrcweir if ( n == 8 )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir fix = off_hours * 3600 + off_minutes * 60;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir else
73cdf0e10cSrcweir {
74cdf0e10cSrcweir return false;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir }
77cdf0e10cSrcweir }
78cdf0e10cSrcweir
79cdf0e10cSrcweir // Convert to local time...
80cdf0e10cSrcweir
81cdf0e10cSrcweir oslDateTime aDateTime;
82cdf0e10cSrcweir aDateTime.NanoSeconds = 0;
83cdf0e10cSrcweir aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds); // 0-59
84cdf0e10cSrcweir aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes); // 0-59
85cdf0e10cSrcweir aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours); // 0-23
86cdf0e10cSrcweir aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day); // 1-31
87cdf0e10cSrcweir aDateTime.DayOfWeek = 0; // 0-6, 0 = Sunday
88cdf0e10cSrcweir aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month); // 1-12
89cdf0e10cSrcweir aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
90cdf0e10cSrcweir
91cdf0e10cSrcweir TimeValue aTimeValue;
92cdf0e10cSrcweir if ( osl_getTimeValueFromDateTime( &aDateTime, &aTimeValue ) )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir aTimeValue.Seconds += fix;
95cdf0e10cSrcweir
96cdf0e10cSrcweir if ( osl_getLocalTimeFromSystemTime( &aTimeValue, &aTimeValue ) )
97cdf0e10cSrcweir {
98cdf0e10cSrcweir if ( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir dateTime.Year = aDateTime.Year;
101cdf0e10cSrcweir dateTime.Month = aDateTime.Month;
102cdf0e10cSrcweir dateTime.Day = aDateTime.Day;
103cdf0e10cSrcweir dateTime.Hours = aDateTime.Hours;
104cdf0e10cSrcweir dateTime.Minutes = aDateTime.Minutes;
105cdf0e10cSrcweir dateTime.Seconds = aDateTime.Seconds;
106cdf0e10cSrcweir
107cdf0e10cSrcweir return true;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir }
110cdf0e10cSrcweir }
111cdf0e10cSrcweir
112cdf0e10cSrcweir return false;
113cdf0e10cSrcweir }
114cdf0e10cSrcweir
115cdf0e10cSrcweir /*
116cdf0e10cSrcweir sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
117cdf0e10cSrcweir {
118cdf0e10cSrcweir if (day.compareToAscii ("Sun") == 0)
119cdf0e10cSrcweir return 0;
120cdf0e10cSrcweir else if (day.compareToAscii ("Mon") == 0)
121cdf0e10cSrcweir return 1;
122cdf0e10cSrcweir else if (day.compareToAscii ("Tue") == 0)
123cdf0e10cSrcweir return 2;
124cdf0e10cSrcweir else if (day.compareToAscii ("Wed") == 0)
125cdf0e10cSrcweir return 3;
126cdf0e10cSrcweir else if (day.compareToAscii ("Thu") == 0)
127cdf0e10cSrcweir return 4;
128cdf0e10cSrcweir else if (day.compareToAscii ("Fri") == 0)
129cdf0e10cSrcweir return 5;
130cdf0e10cSrcweir else if (day.compareToAscii ("Sat") == 0)
131cdf0e10cSrcweir return 6;
132cdf0e10cSrcweir else
133cdf0e10cSrcweir return -1;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir */
136cdf0e10cSrcweir
convertMonthToInt(const OUString & month)137cdf0e10cSrcweir sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir if (month.compareToAscii ("Jan") == 0)
140cdf0e10cSrcweir return 1;
141cdf0e10cSrcweir else if (month.compareToAscii ("Feb") == 0)
142cdf0e10cSrcweir return 2;
143cdf0e10cSrcweir else if (month.compareToAscii ("Mar") == 0)
144cdf0e10cSrcweir return 3;
145cdf0e10cSrcweir else if (month.compareToAscii ("Apr") == 0)
146cdf0e10cSrcweir return 4;
147cdf0e10cSrcweir else if (month.compareToAscii ("May") == 0)
148cdf0e10cSrcweir return 5;
149cdf0e10cSrcweir else if (month.compareToAscii ("Jun") == 0)
150cdf0e10cSrcweir return 6;
151cdf0e10cSrcweir else if (month.compareToAscii ("Jul") == 0)
152cdf0e10cSrcweir return 7;
153cdf0e10cSrcweir else if (month.compareToAscii ("Aug") == 0)
154cdf0e10cSrcweir return 8;
155cdf0e10cSrcweir else if (month.compareToAscii ("Sep") == 0)
156cdf0e10cSrcweir return 9;
157cdf0e10cSrcweir else if (month.compareToAscii ("Oct") == 0)
158cdf0e10cSrcweir return 10;
159cdf0e10cSrcweir else if (month.compareToAscii ("Nov") == 0)
160cdf0e10cSrcweir return 11;
161cdf0e10cSrcweir else if (month.compareToAscii ("Dec") == 0)
162cdf0e10cSrcweir return 12;
163cdf0e10cSrcweir else
164cdf0e10cSrcweir return 0;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir
RFC2068_To_DateTime(const OUString & s,DateTime & dateTime)167cdf0e10cSrcweir bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
168cdf0e10cSrcweir DateTime& dateTime)
169cdf0e10cSrcweir {
170cdf0e10cSrcweir int year;
171cdf0e10cSrcweir int day;
172cdf0e10cSrcweir int hours;
173cdf0e10cSrcweir int minutes;
174cdf0e10cSrcweir int seconds;
175cdf0e10cSrcweir sal_Char string_month[3 + 1];
176cdf0e10cSrcweir sal_Char string_day[3 + 1];
177cdf0e10cSrcweir
178cdf0e10cSrcweir sal_Int32 found = s.indexOf (',');
179cdf0e10cSrcweir if (found != -1)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
182cdf0e10cSrcweir
183cdf0e10cSrcweir // RFC 1123
184cdf0e10cSrcweir found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
185cdf0e10cSrcweir string_day, &day, string_month, &year, &hours, &minutes, &seconds);
186cdf0e10cSrcweir if (found != 7)
187cdf0e10cSrcweir {
188cdf0e10cSrcweir // RFC 1036
189cdf0e10cSrcweir found = sscanf (aDT.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
190cdf0e10cSrcweir string_day, &day, string_month, &year, &hours, &minutes, &seconds);
191cdf0e10cSrcweir }
192cdf0e10cSrcweir found = (found == 7) ? 1 : 0;
193cdf0e10cSrcweir }
194cdf0e10cSrcweir else
195cdf0e10cSrcweir {
196cdf0e10cSrcweir OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
197cdf0e10cSrcweir
198cdf0e10cSrcweir // ANSI C's asctime () format
199cdf0e10cSrcweir found = sscanf (aDT.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
200cdf0e10cSrcweir string_day, string_month,
201cdf0e10cSrcweir &day, &hours, &minutes, &seconds, &year);
202cdf0e10cSrcweir found = (found == 7) ? 1 : 0;
203cdf0e10cSrcweir }
204cdf0e10cSrcweir
205cdf0e10cSrcweir if (found)
206cdf0e10cSrcweir {
207cdf0e10cSrcweir found = 0;
208cdf0e10cSrcweir
209cdf0e10cSrcweir int month = DateTimeHelper::convertMonthToInt (
210cdf0e10cSrcweir OUString::createFromAscii (string_month));
211cdf0e10cSrcweir if (month)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir // Convert to local time...
214cdf0e10cSrcweir
215cdf0e10cSrcweir oslDateTime aDateTime;
216cdf0e10cSrcweir aDateTime.NanoSeconds = 0;
217cdf0e10cSrcweir aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
218cdf0e10cSrcweir // 0-59
219cdf0e10cSrcweir aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
220cdf0e10cSrcweir // 0-59
221cdf0e10cSrcweir aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
222cdf0e10cSrcweir // 0-23
223cdf0e10cSrcweir aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
224cdf0e10cSrcweir // 1-31
225cdf0e10cSrcweir aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
226cdf0e10cSrcweir aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
227cdf0e10cSrcweir // 1-12
228cdf0e10cSrcweir aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
229cdf0e10cSrcweir
230cdf0e10cSrcweir TimeValue aTimeValue;
231cdf0e10cSrcweir if ( osl_getTimeValueFromDateTime( &aDateTime,
232cdf0e10cSrcweir &aTimeValue ) )
233cdf0e10cSrcweir {
234cdf0e10cSrcweir if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
235cdf0e10cSrcweir &aTimeValue ) )
236cdf0e10cSrcweir {
237cdf0e10cSrcweir if ( osl_getDateTimeFromTimeValue( &aTimeValue,
238cdf0e10cSrcweir &aDateTime ) )
239cdf0e10cSrcweir {
240cdf0e10cSrcweir dateTime.Year = aDateTime.Year;
241cdf0e10cSrcweir dateTime.Month = aDateTime.Month;
242cdf0e10cSrcweir dateTime.Day = aDateTime.Day;
243cdf0e10cSrcweir dateTime.Hours = aDateTime.Hours;
244cdf0e10cSrcweir dateTime.Minutes = aDateTime.Minutes;
245cdf0e10cSrcweir dateTime.Seconds = aDateTime.Seconds;
246cdf0e10cSrcweir
247cdf0e10cSrcweir found = 1;
248cdf0e10cSrcweir }
249cdf0e10cSrcweir }
250cdf0e10cSrcweir }
251cdf0e10cSrcweir }
252cdf0e10cSrcweir }
253cdf0e10cSrcweir
254cdf0e10cSrcweir return (found) ? true : false;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir
convert(const OUString & s,DateTime & dateTime)257cdf0e10cSrcweir bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
258cdf0e10cSrcweir {
259cdf0e10cSrcweir if (ISO8601_To_DateTime (s, dateTime))
260cdf0e10cSrcweir return true;
261cdf0e10cSrcweir else if (RFC2068_To_DateTime (s, dateTime))
262cdf0e10cSrcweir return true;
263cdf0e10cSrcweir else
264cdf0e10cSrcweir return false;
265cdf0e10cSrcweir }
266cdf0e10cSrcweir
267