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 ************************************************************************/
31 #include <com/sun/star/util/DateTime.hpp>
32 #include "DateTimeHelper.hxx"
34 using namespace com::sun::star::util
;
36 using namespace webdav_ucp
;
39 bool DateTimeHelper::ISO8601_To_DateTime (const OUString
& s
,
42 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
44 int year
, month
, day
, hours
, minutes
, off_hours
, off_minutes
, fix
;
47 // 2001-01-01T12:30:00Z
48 int n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
49 &year
, &month
, &day
, &hours
, &minutes
, &seconds
);
56 // 2001-01-01T12:30:00+03:30
57 n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
58 &year
, &month
, &day
, &hours
, &minutes
, &seconds
,
59 &off_hours
, &off_minutes
);
62 fix
= - off_hours
* 3600 - off_minutes
* 60;
66 // 2001-01-01T12:30:00-03:30
67 n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
68 &year
, &month
, &day
, &hours
, &minutes
, &seconds
,
69 &off_hours
, &off_minutes
);
72 fix
= off_hours
* 3600 + off_minutes
* 60;
81 // Convert to local time...
83 oslDateTime aDateTime
;
84 aDateTime
.NanoSeconds
= 0;
85 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
); // 0-59
86 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
); // 0-59
87 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
); // 0-23
88 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
); // 1-31
89 aDateTime
.DayOfWeek
= 0; // 0-6, 0 = Sunday
90 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
); // 1-12
91 aDateTime
.Year
= sal::static_int_cast
< sal_Int16
>(year
);
94 if ( osl_getTimeValueFromDateTime( &aDateTime
, &aTimeValue
) )
96 aTimeValue
.Seconds
+= fix
;
98 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
, &aTimeValue
) )
100 if ( osl_getDateTimeFromTimeValue( &aTimeValue
, &aDateTime
) )
102 dateTime
.Year
= aDateTime
.Year
;
103 dateTime
.Month
= aDateTime
.Month
;
104 dateTime
.Day
= aDateTime
.Day
;
105 dateTime
.Hours
= aDateTime
.Hours
;
106 dateTime
.Minutes
= aDateTime
.Minutes
;
107 dateTime
.Seconds
= aDateTime
.Seconds
;
117 sal_Int32
DateTimeHelper::convertMonthToInt (const OUString
& month
)
121 else if (month
== "Feb")
123 else if (month
== "Mar")
125 else if (month
== "Apr")
127 else if (month
== "May")
129 else if (month
== "Jun")
131 else if (month
== "Jul")
133 else if (month
== "Aug")
135 else if (month
== "Sep")
137 else if (month
== "Oct")
139 else if (month
== "Nov")
141 else if (month
== "Dec")
147 bool DateTimeHelper::RFC2068_To_DateTime (const OUString
& s
,
155 sal_Char string_month
[3 + 1];
156 sal_Char string_day
[3 + 1];
158 sal_Int32 found
= s
.indexOf (',');
161 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
164 found
= sscanf (aDT
.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
165 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
169 found
= sscanf (aDT
.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
170 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
172 found
= (found
== 7) ? 1 : 0;
176 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
178 // ANSI C's asctime () format
179 found
= sscanf (aDT
.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
180 string_day
, string_month
,
181 &day
, &hours
, &minutes
, &seconds
, &year
);
182 found
= (found
== 7) ? 1 : 0;
189 int month
= DateTimeHelper::convertMonthToInt (
190 OUString::createFromAscii (string_month
));
193 // Convert to local time...
195 oslDateTime aDateTime
;
196 aDateTime
.NanoSeconds
= 0;
197 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
);
199 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
);
201 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
);
203 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
);
205 aDateTime
.DayOfWeek
= 0; //dayofweek; // 0-6, 0 = Sunday
206 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
);
208 aDateTime
.Year
= sal::static_int_cast
< sal_Int16
>(year
);
210 TimeValue aTimeValue
;
211 if ( osl_getTimeValueFromDateTime( &aDateTime
,
214 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
,
217 if ( osl_getDateTimeFromTimeValue( &aTimeValue
,
220 dateTime
.Year
= aDateTime
.Year
;
221 dateTime
.Month
= aDateTime
.Month
;
222 dateTime
.Day
= aDateTime
.Day
;
223 dateTime
.Hours
= aDateTime
.Hours
;
224 dateTime
.Minutes
= aDateTime
.Minutes
;
225 dateTime
.Seconds
= aDateTime
.Seconds
;
237 bool DateTimeHelper::convert (const OUString
& s
, DateTime
& dateTime
)
239 if (ISO8601_To_DateTime (s
, dateTime
))
241 else if (RFC2068_To_DateTime (s
, dateTime
))
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */