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 ************************************************************************/
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
;
42 bool DateTimeHelper::ISO8601_To_DateTime (const OUString
& s
,
45 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
47 int year
, month
, day
, hours
, minutes
, off_hours
, off_minutes
, fix
;
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
);
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
);
65 fix
= - off_hours
* 3600 - off_minutes
* 60;
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
);
75 fix
= off_hours
* 3600 + off_minutes
* 60;
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
);
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
;
121 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
123 if (day.compareToAscii ("Sun") == 0)
125 else if (day.compareToAscii ("Mon") == 0)
127 else if (day.compareToAscii ("Tue") == 0)
129 else if (day.compareToAscii ("Wed") == 0)
131 else if (day.compareToAscii ("Thu") == 0)
133 else if (day.compareToAscii ("Fri") == 0)
135 else if (day.compareToAscii ("Sat") == 0)
142 sal_Int32
DateTimeHelper::convertMonthToInt (const OUString
& month
)
144 if (month
.compareToAscii ("Jan") == 0)
146 else if (month
.compareToAscii ("Feb") == 0)
148 else if (month
.compareToAscii ("Mar") == 0)
150 else if (month
.compareToAscii ("Apr") == 0)
152 else if (month
.compareToAscii ("May") == 0)
154 else if (month
.compareToAscii ("Jun") == 0)
156 else if (month
.compareToAscii ("Jul") == 0)
158 else if (month
.compareToAscii ("Aug") == 0)
160 else if (month
.compareToAscii ("Sep") == 0)
162 else if (month
.compareToAscii ("Oct") == 0)
164 else if (month
.compareToAscii ("Nov") == 0)
166 else if (month
.compareToAscii ("Dec") == 0)
172 bool DateTimeHelper::RFC2068_To_DateTime (const OUString
& s
,
180 sal_Char string_month
[3 + 1];
181 sal_Char string_day
[3 + 1];
183 sal_Int32 found
= s
.indexOf (',');
186 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
189 found
= sscanf (aDT
.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
190 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
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;
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;
214 int month
= DateTimeHelper::convertMonthToInt (
215 OUString::createFromAscii (string_month
));
218 // Convert to local time...
220 oslDateTime aDateTime
;
221 aDateTime
.NanoSeconds
= 0;
222 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
);
224 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
);
226 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
);
228 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
);
230 aDateTime
.DayOfWeek
= 0; //dayofweek; // 0-6, 0 = Sunday
231 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
);
233 aDateTime
.Year
= sal::static_int_cast
< sal_uInt16
>(year
);
235 TimeValue aTimeValue
;
236 if ( osl_getTimeValueFromDateTime( &aDateTime
,
239 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
,
242 if ( osl_getDateTimeFromTimeValue( &aTimeValue
,
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
;
259 return (found
) ? true : false;
262 bool DateTimeHelper::convert (const OUString
& s
, DateTime
& dateTime
)
264 if (ISO8601_To_DateTime (s
, dateTime
))
266 else if (RFC2068_To_DateTime (s
, dateTime
))
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */