Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
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.
15 : : *
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).
21 : : *
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.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include <stdio.h>
31 : : #include <osl/time.h>
32 : : #include <com/sun/star/util/DateTime.hpp>
33 : : #include "DateTimeHelper.hxx"
34 : :
35 : : using namespace com::sun::star::util;
36 : :
37 : : using namespace webdav_ucp;
38 : :
39 : : using ::rtl::OUString;
40 : : using ::rtl::OString;
41 : :
42 : 0 : bool DateTimeHelper::ISO8601_To_DateTime (const OUString& s,
43 : : DateTime& dateTime)
44 : : {
45 [ # # ]: 0 : OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
46 : :
47 : : int year, month, day, hours, minutes, off_hours, off_minutes, fix;
48 : : double seconds;
49 : :
50 : : // 2001-01-01T12:30:00Z
51 : : int n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lfZ",
52 : 0 : &year, &month, &day, &hours, &minutes, &seconds );
53 [ # # ]: 0 : if ( n == 6 )
54 : : {
55 : 0 : fix = 0;
56 : : }
57 : : else
58 : : {
59 : : // 2001-01-01T12:30:00+03:30
60 : : n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf+%02d:%02d",
61 : : &year, &month, &day, &hours, &minutes, &seconds,
62 : 0 : &off_hours, &off_minutes );
63 [ # # ]: 0 : if ( n == 8 )
64 : : {
65 : 0 : fix = - off_hours * 3600 - off_minutes * 60;
66 : : }
67 : : else
68 : : {
69 : : // 2001-01-01T12:30:00-03:30
70 : : n = sscanf( aDT.getStr(), "%04d-%02d-%02dT%02d:%02d:%lf-%02d:%02d",
71 : : &year, &month, &day, &hours, &minutes, &seconds,
72 : 0 : &off_hours, &off_minutes );
73 [ # # ]: 0 : if ( n == 8 )
74 : : {
75 : 0 : fix = off_hours * 3600 + off_minutes * 60;
76 : : }
77 : : else
78 : : {
79 : 0 : return false;
80 : : }
81 : : }
82 : : }
83 : :
84 : : // Convert to local time...
85 : :
86 : : oslDateTime aDateTime;
87 : 0 : aDateTime.NanoSeconds = 0;
88 : 0 : aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds); // 0-59
89 : 0 : aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes); // 0-59
90 : 0 : aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours); // 0-23
91 : 0 : aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day); // 1-31
92 : 0 : aDateTime.DayOfWeek = 0; // 0-6, 0 = Sunday
93 : 0 : aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month); // 1-12
94 : 0 : aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
95 : :
96 : : TimeValue aTimeValue;
97 [ # # ][ # # ]: 0 : if ( osl_getTimeValueFromDateTime( &aDateTime, &aTimeValue ) )
98 : : {
99 : 0 : aTimeValue.Seconds += fix;
100 : :
101 [ # # ][ # # ]: 0 : if ( osl_getLocalTimeFromSystemTime( &aTimeValue, &aTimeValue ) )
102 : : {
103 [ # # ][ # # ]: 0 : if ( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) )
104 : : {
105 : 0 : dateTime.Year = aDateTime.Year;
106 : 0 : dateTime.Month = aDateTime.Month;
107 : 0 : dateTime.Day = aDateTime.Day;
108 : 0 : dateTime.Hours = aDateTime.Hours;
109 : 0 : dateTime.Minutes = aDateTime.Minutes;
110 : 0 : dateTime.Seconds = aDateTime.Seconds;
111 : :
112 : 0 : return true;
113 : : }
114 : : }
115 : : }
116 : :
117 : 0 : return false;
118 : : }
119 : :
120 : 0 : sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
121 : : {
122 [ # # ]: 0 : if (month.compareToAscii ("Jan") == 0)
123 : 0 : return 1;
124 [ # # ]: 0 : else if (month.compareToAscii ("Feb") == 0)
125 : 0 : return 2;
126 [ # # ]: 0 : else if (month.compareToAscii ("Mar") == 0)
127 : 0 : return 3;
128 [ # # ]: 0 : else if (month.compareToAscii ("Apr") == 0)
129 : 0 : return 4;
130 [ # # ]: 0 : else if (month.compareToAscii ("May") == 0)
131 : 0 : return 5;
132 [ # # ]: 0 : else if (month.compareToAscii ("Jun") == 0)
133 : 0 : return 6;
134 [ # # ]: 0 : else if (month.compareToAscii ("Jul") == 0)
135 : 0 : return 7;
136 [ # # ]: 0 : else if (month.compareToAscii ("Aug") == 0)
137 : 0 : return 8;
138 [ # # ]: 0 : else if (month.compareToAscii ("Sep") == 0)
139 : 0 : return 9;
140 [ # # ]: 0 : else if (month.compareToAscii ("Oct") == 0)
141 : 0 : return 10;
142 [ # # ]: 0 : else if (month.compareToAscii ("Nov") == 0)
143 : 0 : return 11;
144 [ # # ]: 0 : else if (month.compareToAscii ("Dec") == 0)
145 : 0 : return 12;
146 : : else
147 : 0 : return 0;
148 : : }
149 : :
150 : 0 : bool DateTimeHelper::RFC2068_To_DateTime (const OUString& s,
151 : : DateTime& dateTime)
152 : : {
153 : : int year;
154 : : int day;
155 : : int hours;
156 : : int minutes;
157 : : int seconds;
158 : : sal_Char string_month[3 + 1];
159 : : sal_Char string_day[3 + 1];
160 : :
161 : 0 : sal_Int32 found = s.indexOf (',');
162 [ # # ]: 0 : if (found != -1)
163 : : {
164 [ # # ]: 0 : OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
165 : :
166 : : // RFC 1123
167 : : found = sscanf (aDT.getStr(), "%3s, %2d %3s %4d %2d:%2d:%2d GMT",
168 : 0 : string_day, &day, string_month, &year, &hours, &minutes, &seconds);
169 [ # # ]: 0 : if (found != 7)
170 : : {
171 : : // RFC 1036
172 : : found = sscanf (aDT.getStr(), "%3s, %2d-%3s-%2d %2d:%2d:%2d GMT",
173 : 0 : string_day, &day, string_month, &year, &hours, &minutes, &seconds);
174 : : }
175 [ # # ]: 0 : found = (found == 7) ? 1 : 0;
176 : : }
177 : : else
178 : : {
179 [ # # ]: 0 : OString aDT (s.getStr(), s.getLength(), RTL_TEXTENCODING_ASCII_US);
180 : :
181 : : // ANSI C's asctime () format
182 : : found = sscanf (aDT.getStr(), "%3s %3s %d %2d:%2d:%2d %4d",
183 : : string_day, string_month,
184 : 0 : &day, &hours, &minutes, &seconds, &year);
185 [ # # ]: 0 : found = (found == 7) ? 1 : 0;
186 : : }
187 : :
188 [ # # ]: 0 : if (found)
189 : : {
190 : 0 : found = 0;
191 : :
192 : : int month = DateTimeHelper::convertMonthToInt (
193 : 0 : OUString::createFromAscii (string_month));
194 [ # # ]: 0 : if (month)
195 : : {
196 : : // Convert to local time...
197 : :
198 : : oslDateTime aDateTime;
199 : 0 : aDateTime.NanoSeconds = 0;
200 : 0 : aDateTime.Seconds = sal::static_int_cast< sal_uInt16 >(seconds);
201 : : // 0-59
202 : 0 : aDateTime.Minutes = sal::static_int_cast< sal_uInt16 >(minutes);
203 : : // 0-59
204 : 0 : aDateTime.Hours = sal::static_int_cast< sal_uInt16 >(hours);
205 : : // 0-23
206 : 0 : aDateTime.Day = sal::static_int_cast< sal_uInt16 >(day);
207 : : // 1-31
208 : 0 : aDateTime.DayOfWeek = 0; //dayofweek; // 0-6, 0 = Sunday
209 : 0 : aDateTime.Month = sal::static_int_cast< sal_uInt16 >(month);
210 : : // 1-12
211 : 0 : aDateTime.Year = sal::static_int_cast< sal_uInt16 >(year);
212 : :
213 : : TimeValue aTimeValue;
214 [ # # ]: 0 : if ( osl_getTimeValueFromDateTime( &aDateTime,
215 [ # # ]: 0 : &aTimeValue ) )
216 : : {
217 [ # # ]: 0 : if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
218 [ # # ]: 0 : &aTimeValue ) )
219 : : {
220 [ # # ]: 0 : if ( osl_getDateTimeFromTimeValue( &aTimeValue,
221 [ # # ]: 0 : &aDateTime ) )
222 : : {
223 : 0 : dateTime.Year = aDateTime.Year;
224 : 0 : dateTime.Month = aDateTime.Month;
225 : 0 : dateTime.Day = aDateTime.Day;
226 : 0 : dateTime.Hours = aDateTime.Hours;
227 : 0 : dateTime.Minutes = aDateTime.Minutes;
228 : 0 : dateTime.Seconds = aDateTime.Seconds;
229 : :
230 : 0 : found = 1;
231 : : }
232 : : }
233 : : }
234 : : }
235 : : }
236 : :
237 : 0 : return (found) ? true : false;
238 : : }
239 : :
240 : 0 : bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
241 : : {
242 [ # # ]: 0 : if (ISO8601_To_DateTime (s, dateTime))
243 : 0 : return true;
244 [ # # ]: 0 : else if (RFC2068_To_DateTime (s, dateTime))
245 : 0 : return true;
246 : : else
247 : 0 : return false;
248 : : }
249 : :
250 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|