Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / ucb / source / ucp / webdav / DateTimeHelper.cxx
blobc4ccbcaa27128bf66dcff6d23a7de99cb487c93f
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 <stdio.h>
21 #include <osl/time.h>
22 #include <com/sun/star/util/DateTime.hpp>
23 #include "DateTimeHelper.hxx"
25 using namespace com::sun::star::util;
26 using namespace rtl;
28 using namespace http_dav_ucp;
30 bool DateTimeHelper::ISO8601_To_DateTime (const OUString& s,
31 DateTime& dateTime)
33 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
35 int year, month, day, hours, minutes, off_hours, off_minutes, fix;
36 double seconds;
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 );
41 if ( n == 6 )
43 fix = 0;
45 else
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 );
51 if ( n == 8 )
53 fix = - off_hours * 3600 - off_minutes * 60;
55 else
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 );
61 if ( n == 8 )
63 fix = off_hours * 3600 + off_minutes * 60;
65 else
67 return false;
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);
84 TimeValue aTimeValue;
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;
100 return true;
105 return false;
109 sal_Int32 DateTimeHelper::convertDayToInt (const OUString& day)
111 if (day.compareToAscii ("Sun") == 0)
112 return 0;
113 else if (day.compareToAscii ("Mon") == 0)
114 return 1;
115 else if (day.compareToAscii ("Tue") == 0)
116 return 2;
117 else if (day.compareToAscii ("Wed") == 0)
118 return 3;
119 else if (day.compareToAscii ("Thu") == 0)
120 return 4;
121 else if (day.compareToAscii ("Fri") == 0)
122 return 5;
123 else if (day.compareToAscii ("Sat") == 0)
124 return 6;
125 else
126 return -1;
130 sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
132 if (month.compareToAscii ("Jan") == 0)
133 return 1;
134 else if (month.compareToAscii ("Feb") == 0)
135 return 2;
136 else if (month.compareToAscii ("Mar") == 0)
137 return 3;
138 else if (month.compareToAscii ("Apr") == 0)
139 return 4;
140 else if (month.compareToAscii ("May") == 0)
141 return 5;
142 else if (month.compareToAscii ("Jun") == 0)
143 return 6;
144 else if (month.compareToAscii ("Jul") == 0)
145 return 7;
146 else if (month.compareToAscii ("Aug") == 0)
147 return 8;
148 else if (month.compareToAscii ("Sep") == 0)
149 return 9;
150 else if (month.compareToAscii ("Oct") == 0)
151 return 10;
152 else if (month.compareToAscii ("Nov") == 0)
153 return 11;
154 else if (month.compareToAscii ("Dec") == 0)
155 return 12;
156 else
157 return 0;
160 bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
161 DateTime& dateTime)
163 int year;
164 int day;
165 int hours;
166 int minutes;
167 int seconds;
168 sal_Char string_month[3 + 1];
169 sal_Char string_day[3 + 1];
171 sal_Int32 found = s.indexOf (',');
172 if (found != -1)
174 OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
176 // RFC 1123
177 found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
178 string_day, &day, string_month, &year, &hours, &minutes, &seconds);
179 if (found != 7)
181 // RFC 1036
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;
187 else
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;
198 if (found)
200 found = 0;
202 int month = DateTimeHelper::convertMonthToInt (
203 OUString::createFromAscii (string_month));
204 if (month)
206 // Convert to local time...
208 oslDateTime aDateTime;
209 aDateTime.NanoSeconds = 0;
210 aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
211 // 0-59
212 aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
213 // 0-59
214 aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
215 // 0-23
216 aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
217 // 1-31
218 aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
219 aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
220 // 1-12
221 aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
223 TimeValue aTimeValue;
224 if ( osl_getTimeValueFromDateTime( &aDateTime,
225 &aTimeValue ) )
227 if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
228 &aTimeValue ) )
230 if ( osl_getDateTimeFromTimeValue( &aTimeValue,
231 &aDateTime ) )
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;
240 found = 1;
247 return (found) ? true : false;
250 bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
252 if (ISO8601_To_DateTime (s, dateTime))
253 return true;
254 else if (RFC2068_To_DateTime (s, dateTime))
255 return true;
256 else
257 return false;
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */