1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DateTimeHelper.cxx,v $
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"
36 #include <com/sun/star/util/DateTime.hpp>
37 #include "DateTimeHelper.hxx"
39 using namespace com::sun::star::util
;
42 using namespace webdav_ucp
;
44 bool DateTimeHelper::ISO8601_To_DateTime (const OUString
& s
,
47 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
49 int year
, month
, day
, hours
, minutes
, off_hours
, off_minutes
, fix
;
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
);
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
);
67 fix
= - off_hours
* 3600 - off_minutes
* 60;
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
);
77 fix
= off_hours
* 3600 + off_minutes
* 60;
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
);
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
;
123 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
125 if (day.compareToAscii ("Sun") == 0)
127 else if (day.compareToAscii ("Mon") == 0)
129 else if (day.compareToAscii ("Tue") == 0)
131 else if (day.compareToAscii ("Wed") == 0)
133 else if (day.compareToAscii ("Thu") == 0)
135 else if (day.compareToAscii ("Fri") == 0)
137 else if (day.compareToAscii ("Sat") == 0)
144 sal_Int32
DateTimeHelper::convertMonthToInt (const OUString
& month
)
146 if (month
.compareToAscii ("Jan") == 0)
148 else if (month
.compareToAscii ("Feb") == 0)
150 else if (month
.compareToAscii ("Mar") == 0)
152 else if (month
.compareToAscii ("Apr") == 0)
154 else if (month
.compareToAscii ("May") == 0)
156 else if (month
.compareToAscii ("Jun") == 0)
158 else if (month
.compareToAscii ("Jul") == 0)
160 else if (month
.compareToAscii ("Aug") == 0)
162 else if (month
.compareToAscii ("Sep") == 0)
164 else if (month
.compareToAscii ("Oct") == 0)
166 else if (month
.compareToAscii ("Nov") == 0)
168 else if (month
.compareToAscii ("Dec") == 0)
174 bool DateTimeHelper::RFC2068_To_DateTime (const OUString
& s
,
182 sal_Char string_month
[3 + 1];
183 sal_Char string_day
[3 + 1];
185 sal_Int32 found
= s
.indexOf (',');
188 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
191 found
= sscanf (aDT
.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
192 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
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;
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;
216 int month
= DateTimeHelper::convertMonthToInt (
217 OUString::createFromAscii (string_month
));
220 // Convert to local time...
222 oslDateTime aDateTime
;
223 aDateTime
.NanoSeconds
= 0;
224 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
);
226 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
);
228 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
);
230 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
);
232 aDateTime
.DayOfWeek
= 0; //dayofweek; // 0-6, 0 = Sunday
233 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
);
235 aDateTime
.Year
= sal::static_int_cast
< sal_uInt16
>(year
);
237 TimeValue aTimeValue
;
238 if ( osl_getTimeValueFromDateTime( &aDateTime
,
241 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
,
244 if ( osl_getDateTimeFromTimeValue( &aTimeValue
,
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
;
261 return (found
) ? true : false;
264 bool DateTimeHelper::convert (const OUString
& s
, DateTime
& dateTime
)
266 if (ISO8601_To_DateTime (s
, dateTime
))
268 else if (RFC2068_To_DateTime (s
, dateTime
))