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