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