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