fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav-neon / DateTimeHelper.cxx
blob5765d225a5637793bd988a72922a55fa14fec8dc
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 ************************************************************************/
30 #include <osl/time.h>
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,
40 DateTime& dateTime)
42 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
44 int year, month, day, hours, minutes, off_hours, off_minutes, fix;
45 double seconds;
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 );
50 if ( n == 6 )
52 fix = 0;
54 else
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 );
60 if ( n == 8 )
62 fix = - off_hours * 3600 - off_minutes * 60;
64 else
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 );
70 if ( n == 8 )
72 fix = off_hours * 3600 + off_minutes * 60;
74 else
76 return false;
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);
93 TimeValue aTimeValue;
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;
109 return true;
114 return false;
117 sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
119 if (month == "Jan")
120 return 1;
121 else if (month == "Feb")
122 return 2;
123 else if (month == "Mar")
124 return 3;
125 else if (month == "Apr")
126 return 4;
127 else if (month == "May")
128 return 5;
129 else if (month == "Jun")
130 return 6;
131 else if (month == "Jul")
132 return 7;
133 else if (month == "Aug")
134 return 8;
135 else if (month == "Sep")
136 return 9;
137 else if (month == "Oct")
138 return 10;
139 else if (month == "Nov")
140 return 11;
141 else if (month == "Dec")
142 return 12;
143 else
144 return 0;
147 bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
148 DateTime& dateTime)
150 int year;
151 int day;
152 int hours;
153 int minutes;
154 int seconds;
155 sal_Char string_month[3 + 1];
156 sal_Char string_day[3 + 1];
158 sal_Int32 found = s.indexOf (',');
159 if (found != -1)
161 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
163 // RFC 1123
164 found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
165 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
166 if (found != 7)
168 // RFC 1036
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;
174 else
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;
185 if (found)
187 found = 0;
189 int month = DateTimeHelper::convertMonthToInt (
190 OUString::createFromAscii (string_month));
191 if (month)
193 // Convert to local time...
195 oslDateTime aDateTime;
196 aDateTime.NanoSeconds = 0;
197 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
198 // 0-59
199 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
200 // 0-59
201 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
202 // 0-23
203 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
204 // 1-31
205 aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
206 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
207 // 1-12
208 aDateTime.Year = sal::static_int_cast< sal_Int16 >(year);
210 TimeValue aTimeValue;
211 if ( osl_getTimeValueFromDateTime( &aDateTime,
212 &aTimeValue ) )
214 if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
215 &aTimeValue ) )
217 if ( osl_getDateTimeFromTimeValue( &aTimeValue,
218 &aDateTime ) )
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;
227 found = 1;
234 return found != 0;
237 bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
239 if (ISO8601_To_DateTime (s, dateTime))
240 return true;
241 else if (RFC2068_To_DateTime (s, dateTime))
242 return true;
243 else
244 return false;
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */