1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <com/sun/star/util/DateTime.hpp>
22 #include "DateTimeHelper.hxx"
24 using namespace com::sun::star::util
;
26 using namespace http_dav_ucp
;
28 bool DateTimeHelper::ISO8601_To_DateTime (const OUString
& s
,
31 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
33 int year
, month
, day
, hours
, minutes
, off_hours
, off_minutes
, fix
;
36 // 2001-01-01T12:30:00Z
37 int n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
38 &year
, &month
, &day
, &hours
, &minutes
, &seconds
);
45 // 2001-01-01T12:30:00+03:30
46 n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
47 &year
, &month
, &day
, &hours
, &minutes
, &seconds
,
48 &off_hours
, &off_minutes
);
51 fix
= - off_hours
* 3600 - off_minutes
* 60;
55 // 2001-01-01T12:30:00-03:30
56 n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
57 &year
, &month
, &day
, &hours
, &minutes
, &seconds
,
58 &off_hours
, &off_minutes
);
61 fix
= off_hours
* 3600 + off_minutes
* 60;
70 // Convert to local time...
72 oslDateTime aDateTime
;
73 aDateTime
.NanoSeconds
= 0;
74 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
); // 0-59
75 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
); // 0-59
76 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
); // 0-23
77 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
); // 1-31
78 aDateTime
.DayOfWeek
= 0; // 0-6, 0 = Sunday
79 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
); // 1-12
80 aDateTime
.Year
= sal::static_int_cast
< sal_Int16
>(year
);
83 if ( osl_getTimeValueFromDateTime( &aDateTime
, &aTimeValue
) )
85 aTimeValue
.Seconds
+= fix
;
87 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
, &aTimeValue
) )
89 if ( osl_getDateTimeFromTimeValue( &aTimeValue
, &aDateTime
) )
91 dateTime
.Year
= aDateTime
.Year
;
92 dateTime
.Month
= aDateTime
.Month
;
93 dateTime
.Day
= aDateTime
.Day
;
94 dateTime
.Hours
= aDateTime
.Hours
;
95 dateTime
.Minutes
= aDateTime
.Minutes
;
96 dateTime
.Seconds
= aDateTime
.Seconds
;
107 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
109 if (day.equalsAscii("Sun"))
111 else if (day.equalsAscii("Mon"))
113 else if (day.equalsAscii("Tue"))
115 else if (day.equalsAscii("Wed"))
117 else if (day.equalsAscii("Thu"))
119 else if (day.equalsAscii("Fri"))
121 else if (day.equalsAscii("Sat"))
128 sal_Int32
DateTimeHelper::convertMonthToInt (const OUString
& month
)
132 else if (month
== "Feb")
134 else if (month
== "Mar")
136 else if (month
== "Apr")
138 else if (month
== "May")
140 else if (month
== "Jun")
142 else if (month
== "Jul")
144 else if (month
== "Aug")
146 else if (month
== "Sep")
148 else if (month
== "Oct")
150 else if (month
== "Nov")
152 else if (month
== "Dec")
158 bool DateTimeHelper::RFC2068_To_DateTime (const OUString
& s
,
166 sal_Char string_month
[3 + 1];
167 sal_Char string_day
[3 + 1];
169 sal_Int32 found
= s
.indexOf (',');
172 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
175 found
= sscanf (aDT
.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
176 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
180 found
= sscanf (aDT
.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
181 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
183 found
= (found
== 7) ? 1 : 0;
187 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
189 // ANSI C's asctime () format
190 found
= sscanf (aDT
.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
191 string_day
, string_month
,
192 &day
, &hours
, &minutes
, &seconds
, &year
);
193 found
= (found
== 7) ? 1 : 0;
200 int month
= DateTimeHelper::convertMonthToInt (
201 OUString::createFromAscii (string_month
));
204 // Convert to local time...
206 oslDateTime aDateTime
;
207 aDateTime
.NanoSeconds
= 0;
208 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
);
210 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
);
212 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
);
214 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
);
216 aDateTime
.DayOfWeek
= 0; //dayofweek; // 0-6, 0 = Sunday
217 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
);
219 aDateTime
.Year
= sal::static_int_cast
< sal_Int16
>(year
);
221 TimeValue aTimeValue
;
222 if ( osl_getTimeValueFromDateTime( &aDateTime
,
225 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
,
228 if ( osl_getDateTimeFromTimeValue( &aTimeValue
,
231 dateTime
.Year
= aDateTime
.Year
;
232 dateTime
.Month
= aDateTime
.Month
;
233 dateTime
.Day
= aDateTime
.Day
;
234 dateTime
.Hours
= aDateTime
.Hours
;
235 dateTime
.Minutes
= aDateTime
.Minutes
;
236 dateTime
.Seconds
= aDateTime
.Seconds
;
248 bool DateTimeHelper::convert (const OUString
& s
, DateTime
& dateTime
)
250 if (ISO8601_To_DateTime (s
, dateTime
))
252 else if (RFC2068_To_DateTime (s
, dateTime
))
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */