LibreOffice Module ucb (master)  1
neon/DateTimeHelper.cxx
Go to the documentation of this file.
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 bool DateTimeHelper::ISO8601_To_DateTime (const OUString& s,
40  DateTime& dateTime)
41 {
42  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  &year, &month, &day, &hours, &minutes, &seconds );
50  if ( n == 6 )
51  {
52  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  &off_hours, &off_minutes );
60  if ( n == 8 )
61  {
62  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  &off_hours, &off_minutes );
70  if ( n == 8 )
71  {
72  fix = off_hours * 3600 + off_minutes * 60;
73  }
74  else
75  {
76  return false;
77  }
78  }
79  }
80 
81  // Convert to local time...
82 
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);
92 
93  TimeValue aTimeValue;
94  if ( osl_getTimeValueFromDateTime( &aDateTime, &aTimeValue ) )
95  {
96  aTimeValue.Seconds += fix;
97 
98  if ( osl_getLocalTimeFromSystemTime( &aTimeValue, &aTimeValue ) )
99  {
100  if ( osl_getDateTimeFromTimeValue( &aTimeValue, &aDateTime ) )
101  {
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;
108 
109  return true;
110  }
111  }
112  }
113 
114  return false;
115 }
116 
117 sal_Int32 DateTimeHelper::convertMonthToInt (const OUString& month)
118 {
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;
145 }
146 
147 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  sal_Int32 found = s.indexOf (',');
159  if (found != -1)
160  {
161  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  string_day, &day, string_month, &year, &hours, &minutes, &seconds);
166  if (found != 7)
167  {
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);
171  }
172  found = (found == 7) ? 1 : 0;
173  }
174  else
175  {
176  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  &day, &hours, &minutes, &seconds, &year);
182  found = (found == 7) ? 1 : 0;
183  }
184 
185  if (found)
186  {
187  found = 0;
188 
189  int month = DateTimeHelper::convertMonthToInt (
190  OUString::createFromAscii (string_month));
191  if (month)
192  {
193  // Convert to local time...
194 
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);
209 
210  TimeValue aTimeValue;
211  if ( osl_getTimeValueFromDateTime( &aDateTime,
212  &aTimeValue ) )
213  {
214  if ( osl_getLocalTimeFromSystemTime( &aTimeValue,
215  &aTimeValue ) )
216  {
217  if ( osl_getDateTimeFromTimeValue( &aTimeValue,
218  &aDateTime ) )
219  {
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;
226 
227  found = 1;
228  }
229  }
230  }
231  }
232  }
233 
234  return found != 0;
235 }
236 
237 bool DateTimeHelper::convert (const OUString& s, DateTime& dateTime)
238 {
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;
245 }
246 
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool convert(TaskManager const *pShell, uno::Reference< script::XTypeConverter > &xConverter, uno::Any &rValue, _type_ &aReturn)
Definition: filrow.cxx:39
char sal_Char