All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DateTimeUtils.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/datatype/DateTimeUtils.h
22 
23  \brief Utilitary function for the date and time types of the data type module.
24 */
25 
26 // TerraLib
27 #include "Date.h"
28 #include "DateDuration.h"
29 #include "DatePeriod.h"
30 #include "DateTimePeriod.h"
31 #include "DateTimeInstant.h"
32 #include "DateTime.h"
33 #include "DateTimeUtils.h"
34 #include "Enums.h"
35 #include "OrdinalInstant.h"
36 #include "OrdinalPeriod.h"
37 #include "TimeDuration.h"
38 #include "TimeInstant.h"
39 #include "TimeInstantTZ.h"
40 #include "TimePeriod.h"
41 #include "TimePeriodTZ.h"
42 
44 {
45  const te::dt::DateTime* initialTime = t1;
46  const te::dt::DateTime* finalTime = t2;
47  if(t1->operator>(*t2))
48  {
49  initialTime = t2;
50  finalTime = t1;
51  }
52 
53  DateTimeType timeType = initialTime->getDateTimeType();
54 
55  switch(timeType)
56  {
57  case DATE:
58  return new DatePeriod(*(dynamic_cast<const Date*>(initialTime)), *(dynamic_cast<const Date*>(finalTime)));
59  case DATE_PERIOD:
60  return new DatePeriod(dynamic_cast<const DatePeriod*>(initialTime)->getInitialDate(), dynamic_cast<const DatePeriod*>(finalTime)->getFinalDate());
61  case TIME_INSTANT:
62  return new TimePeriod(*(dynamic_cast<const TimeInstant*>(initialTime)), *(dynamic_cast<const TimeInstant*>(finalTime)));
63  case TIME_PERIOD:
64  return new TimePeriod(dynamic_cast<const TimePeriod*>(initialTime)->getInitialTimeInstant(),
65  dynamic_cast<const TimePeriod*>(finalTime)->getFinalTimeInstant());
66  case TIME_INSTANT_TZ:
67  return new TimePeriodTZ(*(dynamic_cast<const TimeInstantTZ*>(initialTime)), *(dynamic_cast<const TimeInstantTZ*>(finalTime)));
68  case TIME_PERIOD_TZ:
69  return new TimePeriodTZ(dynamic_cast<const TimePeriodTZ*>(initialTime)->getInitialTimeInstant(),
70  dynamic_cast<const TimePeriodTZ*>(finalTime)->getFinalTimeInstant());
72  return new OrdinalPeriod(dynamic_cast<const OrdinalInstant*>(initialTime)->getTimeInstant(),
73  dynamic_cast<const OrdinalInstant*>(finalTime)->getTimeInstant());
75  return new OrdinalPeriod(dynamic_cast<const OrdinalPeriod*>(initialTime)->getInitialTime(),
76  dynamic_cast<const OrdinalPeriod*>(finalTime)->getFinalTime());
77  default:
78  return 0;
79  }
80 }
81 
83 {
84  long output = 0;
85 
86  DateTimeType timeType = t1->getDateTimeType();
87  switch(timeType)
88  {
89  case DATE:
90  output = dynamic_cast<const Date*>(t1)->operator-(*(dynamic_cast<const Date*>(t2)));
91  return abs(output);
92  case TIME_INSTANT:
93  output = dynamic_cast<const TimeInstant*>(t1)->operator-(*(dynamic_cast<const TimeInstant*>(t2)));
94  return abs(output);
95  case TIME_INSTANT_TZ:
96  output = dynamic_cast<const TimeInstantTZ*>(t1)->operator-(*(dynamic_cast<const TimeInstantTZ*>(t2)));
97  return abs(output);
99  output = (long)abs(dynamic_cast<const OrdinalInstant*>(t1)->getTimeInstant().getValue() -
100  dynamic_cast<const OrdinalInstant*>(t2)->getTimeInstant().getValue());
101  return abs(output);
102 
103  default:
104  break;
105  }
106 
107  if(timeType==DATE_PERIOD)
108  {
109  Date date1 = dynamic_cast<const DatePeriod*>(t1)->getFinalDate();
110  Date date2 = dynamic_cast<const DatePeriod*>(t2)->getInitialDate();
111  if(t1->operator>(*t2))
112  {
113  date1 = dynamic_cast<const DatePeriod*>(t2)->getFinalDate();
114  date2 = dynamic_cast<const DatePeriod*>(t1)->getInitialDate();
115  }
116  return abs(date2 - date1);
117  }
118 
119  if(timeType==TIME_PERIOD)
120  {
121  TimeInstant instant1 = dynamic_cast<const TimePeriod*>(t1)->getFinalTimeInstant();
122  TimeInstant instant2 = dynamic_cast<const TimePeriod*>(t2)->getInitialTimeInstant();
123  if(t1->operator>(*t2))
124  {
125  instant1 = dynamic_cast<const TimePeriod*>(t2)->getFinalTimeInstant();
126  instant2 = dynamic_cast<const TimePeriod*>(t1)->getInitialTimeInstant();
127  }
128  return abs(instant2 - instant1);
129  }
130 
131  if(timeType==TIME_PERIOD_TZ)
132  {
133  TimeInstantTZ instant1 = dynamic_cast<const TimePeriodTZ*>(t1)->getFinalTimeInstant();
134  TimeInstantTZ instant2 = dynamic_cast<const TimePeriodTZ*>(t2)->getInitialTimeInstant();
135  if(t1->operator>(*t2))
136  {
137  instant1 = dynamic_cast<const TimePeriodTZ*>(t2)->getFinalTimeInstant();
138  instant2 = dynamic_cast<const TimePeriodTZ*>(t1)->getInitialTimeInstant();
139  }
140  return abs(instant2 - instant1);
141  }
142 
143  if(timeType==ORDINAL_TIME_PERIOD)
144  {
145  int64_t instant1 = dynamic_cast<const OrdinalPeriod*>(t1)->getFinalTime().getValue();
146  int64_t instant2 = dynamic_cast<const OrdinalPeriod*>(t2)->getInitialTime().getValue();
147  if(t1->operator>(*t2))
148  {
149  instant1 = dynamic_cast<const OrdinalPeriod*>(t2)->getFinalTime().getValue();
150  instant2 = dynamic_cast<const OrdinalPeriod*>(t1)->getInitialTime().getValue();
151  }
152  return (long)abs(instant2 - instant1);
153  }
154 
155  return output;
156 }
157 
159 {
160  DateTimeType timeType = t1->getDateTimeType();
161  te::dt::DateTimePeriod* output = 0;
162 
163  switch(timeType)
164  {
165  case DATE:
166  output = new DatePeriod(*dynamic_cast<const Date*>(t1), *dynamic_cast<const Date*>(t2));
167  return output;
168  case TIME_INSTANT:
169  output = new TimePeriod(*dynamic_cast<const TimeInstant*>(t1), *dynamic_cast<const TimeInstant*>(t2));
170  return output;
171  case TIME_INSTANT_TZ:
172  output = new TimePeriodTZ(*dynamic_cast<const TimeInstantTZ*>(t1), *dynamic_cast<const TimeInstantTZ*>(t2));
173  return output;
175  output = new OrdinalPeriod( dynamic_cast<const OrdinalInstant*>(t1)->getTimeInstant().getValue(),
176  dynamic_cast<const OrdinalInstant*>(t2)->getTimeInstant().getValue());
177  return output;
178  default:
179  return output;
180  }
181 }
182 
A class to represent ordinal time period.
Definition: OrdinalPeriod.h:55
TEDATATYPEEXPORT DateTimePeriod * GetTimePeriod(const DateTimeInstant *t1, const DateTimeInstant *t2)
It creates a time period based on two time instants.
An abstract class for date and time types.
Utilitary function for the date and time types of the data type module.
TEDATATYPEEXPORT long GetDistance(const te::dt::DateTime *t1, const te::dt::DateTime *t2)
It returns the distance between two datetime types.
A class to represent time instant.
Definition: TimeInstant.h:55
A class to represent time duration.
A class to represent date period.
A class to represent ordinal time period.
A base class for date data types.
Definition: Date.h:53
A class to represent ordinal time instant.
A class to represent time period.
A class to represent time instant.
A class to represent time instant with time zone.
A class to represent time period with time zone.
Definition: TimePeriodTZ.h:54
A class to represent time period.
Definition: TimePeriod.h:54
virtual DateTimeType getDateTimeType() const =0
It returns the subtype of the date and time type.
A class to represent ordinal time instant.
An abstract class to represent a period of date and time.
A class to represent time period with time zone.
General enumerations for the data type module.
DateTimeType
The subtype of date and time type, based on ISO 8621.
Definition: Enums.h:45
A class to represent date period based on the Gregorian Calendar.
Definition: DatePeriod.h:51
TEDATATYPEEXPORT DateTimePeriod * GetTemporalExtent(const DateTime *t1, const DateTime *t2)
It returns the temporal extent of two date and time types.
An abstract class to represent a period of date and time.
A class to represent time instant with time zone.
Definition: TimeInstantTZ.h:52
A class to represent date duration.
An abstract class to represent an instant of date and time.
An abstract class to represent an instant of date and time.
A base class for date data types.