fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav / DateTimeHelper.cxx
blob961c2332e9777d633d10f49f55e19b5e7b845680
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <osl/time.h>
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,
29 DateTime& dateTime)
31 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
33 int year, month, day, hours, minutes, off_hours, off_minutes, fix;
34 double seconds;
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 );
39 if ( n == 6 )
41 fix = 0;
43 else
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 );
49 if ( n == 8 )
51 fix = - off_hours * 3600 - off_minutes * 60;
53 else
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 );
59 if ( n == 8 )
61 fix = off_hours * 3600 + off_minutes * 60;
63 else
65 return false;
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);
82 TimeValue aTimeValue;
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;
98 return true;
103 return false;
107 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
109 if (day.equalsAscii("Sun"))
110 return 0;
111 else if (day.equalsAscii("Mon"))
112 return 1;
113 else if (day.equalsAscii("Tue"))
114 return 2;
115 else if (day.equalsAscii("Wed"))
116 return 3;
117 else if (day.equalsAscii("Thu"))
118 return 4;
119 else if (day.equalsAscii("Fri"))
120 return 5;
121 else if (day.equalsAscii("Sat"))
122 return 6;
123 else
124 return -1;
128 sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
130 if (month.equalsAscii("Jan"))
131 return 1;
132 else if (month.equalsAscii("Feb"))
133 return 2;
134 else if (month.equalsAscii("Mar"))
135 return 3;
136 else if (month.equalsAscii("Apr"))
137 return 4;
138 else if (month.equalsAscii("May"))
139 return 5;
140 else if (month.equalsAscii("Jun"))
141 return 6;
142 else if (month.equalsAscii("Jul"))
143 return 7;
144 else if (month.equalsAscii("Aug"))
145 return 8;
146 else if (month.equalsAscii("Sep"))
147 return 9;
148 else if (month.equalsAscii("Oct"))
149 return 10;
150 else if (month.equalsAscii("Nov"))
151 return 11;
152 else if (month.equalsAscii("Dec"))
153 return 12;
154 else
155 return 0;
158 bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
159 DateTime& dateTime)
161 int year;
162 int day;
163 int hours;
164 int minutes;
165 int seconds;
166 sal_Char string_month[3 + 1];
167 sal_Char string_day[3 + 1];
169 sal_Int32 found = s.indexOf (',');
170 if (found != -1)
172 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
174 // RFC 1123
175 found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
176 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
177 if (found != 7)
179 // RFC 1036
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;
185 else
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;
196 if (found)
198 found = 0;
200 int month = DateTimeHelper::convertMonthToInt (
201 OUString::createFromAscii (string_month));
202 if (month)
204 // Convert to local time...
206 oslDateTime aDateTime;
207 aDateTime.NanoSeconds = 0;
208 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
209 // 0-59
210 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
211 // 0-59
212 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
213 // 0-23
214 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
215 // 1-31
216 aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
217 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
218 // 1-12
219 aDateTime.Year = sal::static_int_cast< sal_Int16 >(year);
221 TimeValue aTimeValue;
222 if ( osl_getTimeValueFromDateTime( &aDateTime,
223 &aTimeValue ) )
225 if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
226 &aTimeValue ) )
228 if ( osl_getDateTimeFromTimeValue( &aTimeValue,
229 &aDateTime ) )
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;
238 found = 1;
245 return (found) ? true : false;
248 bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
250 if (ISO8601_To_DateTime (s, dateTime))
251 return true;
252 else if (RFC2068_To_DateTime (s, dateTime))
253 return true;
254 else
255 return false;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */