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 .
22 #include <com/sun/star/util/DateTime.hpp>
23 #include "DateTimeHelper.hxx"
25 using namespace com::sun::star::util
;
28 using namespace http_dav_ucp
;
30 bool DateTimeHelper::ISO8601_To_DateTime (const OUString
& s
,
33 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
35 int year
, month
, day
, hours
, minutes
, off_hours
, off_minutes
, fix
;
38 // 2001-01-01T12:30:00Z
39 int n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
40 &year
, &month
, &day
, &hours
, &minutes
, &seconds
);
47 // 2001-01-01T12:30:00+03:30
48 n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
49 &year
, &month
, &day
, &hours
, &minutes
, &seconds
,
50 &off_hours
, &off_minutes
);
53 fix
= - off_hours
* 3600 - off_minutes
* 60;
57 // 2001-01-01T12:30:00-03:30
58 n
= sscanf( aDT
.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
59 &year
, &month
, &day
, &hours
, &minutes
, &seconds
,
60 &off_hours
, &off_minutes
);
63 fix
= off_hours
* 3600 + off_minutes
* 60;
72 // Convert to local time...
74 oslDateTime aDateTime
;
75 aDateTime
.NanoSeconds
= 0;
76 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
); // 0-59
77 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
); // 0-59
78 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
); // 0-23
79 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
); // 1-31
80 aDateTime
.DayOfWeek
= 0; // 0-6, 0 = Sunday
81 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
); // 1-12
82 aDateTime
.Year
= sal::static_int_cast
< sal_uInt16
>(year
);
85 if ( osl_getTimeValueFromDateTime( &aDateTime
, &aTimeValue
) )
87 aTimeValue
.Seconds
+= fix
;
89 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
, &aTimeValue
) )
91 if ( osl_getDateTimeFromTimeValue( &aTimeValue
, &aDateTime
) )
93 dateTime
.Year
= aDateTime
.Year
;
94 dateTime
.Month
= aDateTime
.Month
;
95 dateTime
.Day
= aDateTime
.Day
;
96 dateTime
.Hours
= aDateTime
.Hours
;
97 dateTime
.Minutes
= aDateTime
.Minutes
;
98 dateTime
.Seconds
= aDateTime
.Seconds
;
109 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
111 if (day.compareToAscii ("Sun") == 0)
113 else if (day.compareToAscii ("Mon") == 0)
115 else if (day.compareToAscii ("Tue") == 0)
117 else if (day.compareToAscii ("Wed") == 0)
119 else if (day.compareToAscii ("Thu") == 0)
121 else if (day.compareToAscii ("Fri") == 0)
123 else if (day.compareToAscii ("Sat") == 0)
130 sal_Int32
DateTimeHelper::convertMonthToInt (const OUString
& month
)
132 if (month
.compareToAscii ("Jan") == 0)
134 else if (month
.compareToAscii ("Feb") == 0)
136 else if (month
.compareToAscii ("Mar") == 0)
138 else if (month
.compareToAscii ("Apr") == 0)
140 else if (month
.compareToAscii ("May") == 0)
142 else if (month
.compareToAscii ("Jun") == 0)
144 else if (month
.compareToAscii ("Jul") == 0)
146 else if (month
.compareToAscii ("Aug") == 0)
148 else if (month
.compareToAscii ("Sep") == 0)
150 else if (month
.compareToAscii ("Oct") == 0)
152 else if (month
.compareToAscii ("Nov") == 0)
154 else if (month
.compareToAscii ("Dec") == 0)
160 bool DateTimeHelper::RFC2068_To_DateTime (const OUString
& s
,
168 sal_Char string_month
[3 + 1];
169 sal_Char string_day
[3 + 1];
171 sal_Int32 found
= s
.indexOf (',');
174 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
177 found
= sscanf (aDT
.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
178 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
182 found
= sscanf (aDT
.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
183 string_day
, &day
, string_month
, &year
, &hours
, &minutes
, &seconds
);
185 found
= (found
== 7) ? 1 : 0;
189 OString
aDT (s
.getStr(), s
.getLength(), RTL_TEXTENCODING_ASCII_US
);
191 // ANSI C's asctime () format
192 found
= sscanf (aDT
.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
193 string_day
, string_month
,
194 &day
, &hours
, &minutes
, &seconds
, &year
);
195 found
= (found
== 7) ? 1 : 0;
202 int month
= DateTimeHelper::convertMonthToInt (
203 OUString::createFromAscii (string_month
));
206 // Convert to local time...
208 oslDateTime aDateTime
;
209 aDateTime
.NanoSeconds
= 0;
210 aDateTime
.Seconds
= sal::static_int_cast
< sal_uInt16
>(seconds
);
212 aDateTime
.Minutes
= sal::static_int_cast
< sal_uInt16
>(minutes
);
214 aDateTime
.Hours
= sal::static_int_cast
< sal_uInt16
>(hours
);
216 aDateTime
.Day
= sal::static_int_cast
< sal_uInt16
>(day
);
218 aDateTime
.DayOfWeek
= 0; //dayofweek; // 0-6, 0 = Sunday
219 aDateTime
.Month
= sal::static_int_cast
< sal_uInt16
>(month
);
221 aDateTime
.Year
= sal::static_int_cast
< sal_uInt16
>(year
);
223 TimeValue aTimeValue
;
224 if ( osl_getTimeValueFromDateTime( &aDateTime
,
227 if ( osl_getLocalTimeFromSystemTime( &aTimeValue
,
230 if ( osl_getDateTimeFromTimeValue( &aTimeValue
,
233 dateTime
.Year
= aDateTime
.Year
;
234 dateTime
.Month
= aDateTime
.Month
;
235 dateTime
.Day
= aDateTime
.Day
;
236 dateTime
.Hours
= aDateTime
.Hours
;
237 dateTime
.Minutes
= aDateTime
.Minutes
;
238 dateTime
.Seconds
= aDateTime
.Seconds
;
247 return (found
) ? true : false;
250 bool DateTimeHelper::convert (const OUString
& s
, DateTime
& dateTime
)
252 if (ISO8601_To_DateTime (s
, dateTime
))
254 else if (RFC2068_To_DateTime (s
, dateTime
))
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */