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