calendarui/commonutils/src/calendateutils.cpp
changeset 89 b57382753122
parent 75 7ac58b2aae6f
equal deleted inserted replaced
83:5aadd1120515 89:b57382753122
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2002-2004 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     9 * Initial Contributors:
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10 * Nokia Corporation - initial contribution.
    11 *
    11 *
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:  ?Description
    14 * Description:   	Utility class providing utility functions to know  beginning of the day, maximum/minimum date allowed in calendar, 
       
    15 *			day range, on same day, on same month etc.
    15 *
    16 *
    16 */
    17 */
    17 
    18 
    18 
    19 
       
    20 
    19 //debug
    21 //debug
    20 #include <qdatetime.h>
       
    21 #include "calendarui_debug.h"
    22 #include "calendarui_debug.h"
    22 
    23 
    23 #include "calendateutils.h"
    24 #include <calendateutils.h>
    24 #include <agendautil.h>
    25 #include <caltime.h> // For Min/MaxValidDate
    25 
       
    26 #include <hbextendedlocale.h>
       
    27 
    26 
    28 //  LOCAL CONSTANTS AND MACROS
    27 //  LOCAL CONSTANTS AND MACROS
    29 const int KDefaultStartTime(8);    // 8 am ( 0 to 23 hour scale)
    28 const TInt KDefaultStartTime(8);    // 8 am
    30 
    29 
    31 // ============================ MEMBER FUNCTIONS ==============================
    30 // ============================ MEMBER FUNCTIONS ==============================
    32 
    31 
    33 // ============================  CalenDateUtils  ==============================
    32 // ============================  CalenDateUtils  ==============================
    34 // -----------------------------------------------------------------------------
    33 // -----------------------------------------------------------------------------
    35 // ?classname::?member_function
    34 // ?classname::?member_function
    36 // ?implementation_description
    35 // ?implementation_description
    37 // (other items were commented in a header).
    36 // (other items were commented in a header).
    38 // -----------------------------------------------------------------------------
    37 // -----------------------------------------------------------------------------
    39 //
    38 //
    40  bool CalenDateUtils::onSameDay( const QDateTime& x, const QDateTime& y )
    39 EXPORT_C TBool CalenDateUtils::OnSameDay( const TTime& aX, const TTime& aY )
    41     {
    40     {
    42     return x.date().year()  == y.date().year() 
    41     TRACE_ENTRY_POINT;
    43         && x.date().month() == y.date().month()
    42     
    44         && x.date().day()   == y.date().day();
    43     TDateTime x = aX.DateTime();
    45     }
    44     TDateTime y = aY.DateTime();
    46 
    45     
    47 // -----------------------------------------------------------------------------
    46     TRACE_EXIT_POINT;
    48 // ?classname::?member_function
    47     return x.Year()  == y.Year() 
    49 // ?implementation_description
    48         && x.Month() == y.Month()
    50 // (other items were commented in a header).
    49         && x.Day()   == y.Day();
    51 // -----------------------------------------------------------------------------
    50     }
    52 //
    51 
    53  bool CalenDateUtils::onSameMonth( const QDateTime& x, const QDateTime& y )
    52 // -----------------------------------------------------------------------------
    54     {
    53 // ?classname::?member_function
    55     return ( x.date().year() == y.date().year() && x.date().month() == y.date().month() );
    54 // ?implementation_description
    56     }
    55 // (other items were commented in a header).
    57 
    56 // -----------------------------------------------------------------------------
    58 // -----------------------------------------------------------------------------
    57 //
    59 // ?classname::?member_function
    58 EXPORT_C TBool CalenDateUtils::OnSameMonth( const TTime& aX, const TTime& aY )
    60 // ?implementation_description
    59     {
    61 // (other items were commented in a header).
    60     TRACE_ENTRY_POINT;
    62 // -----------------------------------------------------------------------------
    61     
    63 //
    62     TDateTime x = aX.DateTime();
    64  QDateTime CalenDateUtils::beginningOfDay( const QDateTime& startTime )
    63     TDateTime y = aY.DateTime();
    65     {    
    64     
    66     QTime zeroTime(0,0,0,0);
    65     TRACE_EXIT_POINT;
    67     QDateTime ret(startTime.date(), zeroTime);
    66     return ( x.Year() == y.Year() && x.Month() == y.Month() );
    68     return ret;
    67     }
    69     }
    68 
    70 
    69 // -----------------------------------------------------------------------------
    71 // -----------------------------------------------------------------------------
    70 // ?classname::?member_function
    72 // ?classname::?member_function
    71 // ?implementation_description
    73 // ?implementation_description
    72 // (other items were commented in a header).
    74 // (other items were commented in a header).
    73 // -----------------------------------------------------------------------------
    75 // -----------------------------------------------------------------------------
    74 //
    76 //
    75 EXPORT_C TTime CalenDateUtils::BeginningOfDay( const TTime& aStartTime )
    77  QDateTime CalenDateUtils::displayTimeOnDay( const QDateTime& startTime,
    76     {
    78                                                  const QDateTime& day )
    77     TRACE_ENTRY_POINT;
    79     {
    78     
    80     if( ! onSameDay( startTime, day ) )
    79     TTime zero(TInt64(0));
       
    80     
       
    81     TRACE_EXIT_POINT;
       
    82     return zero + aStartTime.DaysFrom( zero );
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // ?classname::?member_function
       
    87 // ?implementation_description
       
    88 // (other items were commented in a header).
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 EXPORT_C TTime CalenDateUtils::DisplayTimeOnDay( const TTime& aStartTime,
       
    92                                                  const TTime& aDay )
       
    93     {
       
    94     TRACE_ENTRY_POINT;
       
    95     
       
    96     if( ! OnSameDay( aStartTime, aDay ) )
    81         {
    97         {
    82         return beginningOfDay( day );
    98         TRACE_EXIT_POINT;
       
    99         return BeginningOfDay( aDay );
    83         }
   100         }
    84     else 
   101     else 
    85         {
   102         {
    86         return startTime;
   103         TRACE_EXIT_POINT;
       
   104         return aStartTime;
    87         }
   105         }
    88     }
   106     }
    89 
   107 
    90 // -----------------------------------------------------------------------------
   108 // -----------------------------------------------------------------------------
    91 // ?classname::?member_function
   109 // ?classname::?member_function
    92 // ?implementation_description
   110 // ?implementation_description
    93 // (other items were commented in a header).
   111 // (other items were commented in a header).
    94 // -----------------------------------------------------------------------------
   112 // -----------------------------------------------------------------------------
    95 //
   113 //
    96  bool CalenDateUtils::timeRangesIntersect( const QDateTime& xStart,
   114 EXPORT_C TBool CalenDateUtils::TimeRangesIntersect( const TTime& aXStart,
    97                                                     const QDateTime& xEnd,
   115                                                     const TTime& aXEnd,
    98                                                     const QDateTime& yStart,
   116                                                     const TTime& aYStart,
    99                                                     const QDateTime& yEnd )
   117                                                     const TTime& aYEnd )
   100     {
   118     {
   101     return (! ( yEnd <=  xStart || xEnd <= yStart )
   119     TRACE_ENTRY_POINT;
   102         || (xStart == xEnd && yStart <= xStart && xStart < yEnd)
   120     
   103         || (yStart == yEnd && xStart <= yStart && yStart < xEnd)
   121     TRACE_EXIT_POINT;
   104         || (xStart == xEnd && yStart == yEnd && xStart == yStart));
   122     return (! ( aYEnd <= aXStart || aXEnd <= aYStart ))
   105     }
   123         || (aXStart == aXEnd && aYStart <= aXStart && aXStart < aYEnd)
   106 
   124         || (aYStart == aYEnd && aXStart <= aYStart && aYStart < aXEnd)
   107 // -----------------------------------------------------------------------------
   125         || (aXStart == aXEnd && aYStart == aYEnd && aXStart == aYStart);
   108 // ?classname::?member_function
   126     }
   109 // ?implementation_description
   127 
   110 // (other items were commented in a header).
   128 // -----------------------------------------------------------------------------
   111 // -----------------------------------------------------------------------------
   129 // ?classname::?member_function
   112 //
   130 // ?implementation_description
   113  bool CalenDateUtils::isValidDay( const QDateTime& time )
   131 // (other items were commented in a header).
   114     {
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C TBool CalenDateUtils::IsValidDay( const TTime& aTime )
       
   135     {
       
   136     TRACE_ENTRY_POINT;
       
   137     
   115     // Interim API supports range from 1900-2100, 
   138     // Interim API supports range from 1900-2100, 
   116     return ( minTime() <= time && time <= maxTime() );
   139     TRACE_EXIT_POINT;
   117     }
   140     return ( MinTime() <= aTime && aTime <= MaxTime() );
   118 
   141     }
   119 // -----------------------------------------------------------------------------
   142 
   120 // ?classname::?member_function
   143 // -----------------------------------------------------------------------------
   121 // ?implementation_description
   144 // ?classname::?member_function
   122 // (other items were commented in a header).
   145 // ?implementation_description
   123 // -----------------------------------------------------------------------------
   146 // (other items were commented in a header).
   124 //
   147 // -----------------------------------------------------------------------------
   125  QDateTime CalenDateUtils::limitToValidTime( const QDateTime& time )
   148 //
   126     {    
   149 EXPORT_C void CalenDateUtils::GetDayRangeL( const TTime& aStartDay,
   127     QDateTime valid = time;
   150                                             const TTime& aEndDay,
   128     valid = valid > maxTime() ? maxTime() : valid;
   151                                             CalCommon::TCalTimeRange& aRange )
   129     valid = valid < minTime() ? minTime() : valid;
   152     {
   130 
   153     TRACE_ENTRY_POINT;
       
   154 
       
   155     TDateTime start( aStartDay.DateTime() );
       
   156     TDateTime end( aEndDay.DateTime() );
       
   157 
       
   158     start.SetHour( 0 );
       
   159     start.SetMinute( 0 );
       
   160     start.SetSecond( 0 );
       
   161     start.SetMicroSecond( 0 );
       
   162 
       
   163     end.SetHour( 23 );
       
   164     end.SetMinute( 59 );
       
   165     end.SetSecond( 59 );
       
   166     end.SetMicroSecond( 0 );
       
   167 
       
   168     // prevent overflow
       
   169     TCalTime endDate;
       
   170     endDate.SetTimeLocalL( LimitToValidTime( TTime( end ) ) );
       
   171 
       
   172     TCalTime startDate;
       
   173     startDate.SetTimeLocalL( LimitToValidTime( TTime( start ) ) );
       
   174 
       
   175     CalCommon::TCalTimeRange dayrange( startDate, endDate );
       
   176 
       
   177     aRange = dayrange;
       
   178     
       
   179     TRACE_EXIT_POINT;
       
   180     }
       
   181 
       
   182 // -----------------------------------------------------------------------------
       
   183 // ?classname::?member_function
       
   184 // ?implementation_description
       
   185 // (other items were commented in a header).
       
   186 // -----------------------------------------------------------------------------
       
   187 //
       
   188 EXPORT_C TBool CalenDateUtils::IsNullTimeL( TCalTime& aTime )
       
   189     {
       
   190     TRACE_ENTRY_POINT;
       
   191     
       
   192     TRACE_EXIT_POINT;
       
   193     return( aTime.TimeLocalL() == Time::NullTTime() );
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // ?classname::?member_function
       
   198 // ?implementation_description
       
   199 // (other items were commented in a header).
       
   200 // -----------------------------------------------------------------------------
       
   201 //
       
   202 EXPORT_C TTime CalenDateUtils::LimitToValidTime( const TTime& aTime )
       
   203     {
       
   204     TRACE_ENTRY_POINT;
       
   205     
       
   206     TTime valid = aTime;
       
   207     valid = valid > MaxTime() ? MaxTime() : valid;
       
   208     valid = valid < MinTime() ? MinTime() : valid;
       
   209     
       
   210     TRACE_EXIT_POINT;
   131     return valid;
   211     return valid;
   132     }
   212     }
   133 
   213 
   134 // -----------------------------------------------------------------------------
   214 // -----------------------------------------------------------------------------
   135 // ?classname::?member_function
   215 // ?classname::?member_function
   136 // Returns maximum time allowed, 31.12.2100 0:00 is max so 30.12.2100 is last actual
   216 // Returns maximum time allowed, 31.12.2100 0:00 is max so 30.12.2100 is last actual
   137 // date to be used.
   217 // date to be used.
   138 // (other items were commented in a header).
   218 // (other items were commented in a header).
   139 // -----------------------------------------------------------------------------
   219 // -----------------------------------------------------------------------------
   140 //
   220 //
   141  QDateTime CalenDateUtils::maxTime()
   221 EXPORT_C TTime CalenDateUtils::MaxTime()
   142     { 
   222     {
   143     return AgendaUtil::maxTime();
   223     TRACE_ENTRY_POINT;
       
   224     
       
   225     TTime time( TCalTime::MaxTime() - TTimeIntervalMinutes( 1 ) );
       
   226     
       
   227     TRACE_EXIT_POINT;
       
   228     return time;
   144     }
   229     }
   145 
   230 
   146 // -----------------------------------------------------------------------------
   231 // -----------------------------------------------------------------------------
   147 // ?classname::?member_function
   232 // ?classname::?member_function
   148 // Returns minimum time allowed, 1.1.1900 0:00 is min
   233 // Returns minimum time allowed, 1.1.1900 0:00 is min
   149 // (other items were commented in a header).
   234 // (other items were commented in a header).
   150 // -----------------------------------------------------------------------------
   235 // -----------------------------------------------------------------------------
   151 //
   236 //
   152  QDateTime CalenDateUtils::minTime()
   237 EXPORT_C TTime CalenDateUtils::MinTime()
   153     {
   238     {
   154     return AgendaUtil::minTime();
   239     TRACE_ENTRY_POINT;
   155     }
   240     
   156 
   241     TRACE_EXIT_POINT;
   157 // -----------------------------------------------------------------------------
   242     return TCalTime::MinTime();
   158 // (other items were commented in a header).
   243     }
   159 // -----------------------------------------------------------------------------
   244 
   160 //
   245 // -----------------------------------------------------------------------------
   161  int CalenDateUtils::timeOfDay( const QDateTime& dateTime )
   246 // (other items were commented in a header).
   162     {    
   247 // -----------------------------------------------------------------------------
   163     QDateTime midnight = beginningOfDay( dateTime );
   248 //
   164     int resultInSec = midnight.secsTo(dateTime);
   249 EXPORT_C TTimeIntervalMinutes CalenDateUtils::TimeOfDay( const TTime& aDateTime )
   165     
   250     {
   166     return (resultInSec/60);
   251     TRACE_ENTRY_POINT;
   167     }
   252     
   168 
   253     TTime midnight = CalenDateUtils::BeginningOfDay( aDateTime );
   169 // -----------------------------------------------------------------------------
   254     TTimeIntervalMinutes result;
   170 // (other items were commented in a header).
   255     aDateTime.MinutesFrom( midnight, result );
   171 // -----------------------------------------------------------------------------
   256     
   172 //
   257     TRACE_EXIT_POINT;
   173  QDateTime CalenDateUtils::roundToPreviousHour( const QDateTime& dateTime ) 
   258     return result;
   174     {
   259     }
   175     QTime time = dateTime.time();
   260 
   176     time.setHMS(time.hour(),0,0,0);
   261 // -----------------------------------------------------------------------------
   177     return QDateTime( dateTime.date(), time );
   262 // (other items were commented in a header).
   178     }
   263 // -----------------------------------------------------------------------------
   179 
   264 //
   180 // -----------------------------------------------------------------------------
   265 EXPORT_C TTime CalenDateUtils::RoundToPreviousHour( const TTime& aTime ) 
   181 // (other items were commented in a header).
   266     {
   182 // -----------------------------------------------------------------------------
   267     TRACE_ENTRY_POINT;
   183 //
   268 
   184  int CalenDateUtils::roundToPreviousHour( const int& minutes )
   269     TDateTime dt = aTime.DateTime();
   185     {
   270     dt.SetMinute(0);
   186     return ( (minutes / 60) * 60 );
   271     dt.SetSecond(0);
   187     }
   272     dt.SetMicroSecond(0);
   188 
   273 
   189 // -----------------------------------------------------------------------------
   274     TRACE_EXIT_POINT;
   190 // (other items were commented in a header).
   275     return TTime( dt );
   191 // -----------------------------------------------------------------------------
   276     }
   192 //
   277 
   193  QDateTime CalenDateUtils::now()
   278 // -----------------------------------------------------------------------------
   194     {
   279 // (other items were commented in a header).
   195     return QDateTime::currentDateTime();
   280 // -----------------------------------------------------------------------------
   196     }
   281 //
   197 
   282 EXPORT_C TTimeIntervalMinutes CalenDateUtils::RoundToPreviousHour( const TTimeIntervalMinutes& aMinutes )
   198 // -----------------------------------------------------------------------------
   283     {
   199 // (other items were commented in a header).
   284     TRACE_ENTRY_POINT;
   200 // -----------------------------------------------------------------------------
   285 
   201 //
   286     TRACE_EXIT_POINT;
   202  QDateTime CalenDateUtils::today()
   287     return TTimeIntervalMinutes( (aMinutes.Int() / 60) * 60 );
   203     {
   288     }
   204     return CalenDateUtils::beginningOfDay( now() );
   289 
   205     }
   290 // -----------------------------------------------------------------------------
   206 
   291 // (other items were commented in a header).
   207 // -----------------------------------------------------------------------------
   292 // -----------------------------------------------------------------------------
   208 // (other items were commented in a header).
   293 //
   209 // -----------------------------------------------------------------------------
   294 EXPORT_C TTime CalenDateUtils::Now()
   210 //
   295     {
   211  bool CalenDateUtils::isOnToday( const QDateTime& time )
   296     TRACE_ENTRY_POINT;
   212     {
   297 
   213     return CalenDateUtils::onSameDay( time, today() );
   298     TTime now;
   214     }
   299     now.HomeTime();
   215 
   300 
   216 // -----------------------------------------------------------------------------
   301     TRACE_EXIT_POINT;
   217 // (other items were commented in a header).
   302     return now;
   218 // -----------------------------------------------------------------------------
   303     }
   219 //
   304 
   220  QDateTime CalenDateUtils::defaultTime( const QDateTime& date )
   305 // -----------------------------------------------------------------------------
   221     {
   306 // (other items were commented in a header).
   222     QDateTime dateTime;
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 EXPORT_C TTime CalenDateUtils::Today()
       
   310     {
       
   311     TRACE_ENTRY_POINT;
       
   312 
       
   313     TRACE_EXIT_POINT;
       
   314     return CalenDateUtils::BeginningOfDay( Now() );
       
   315     }
       
   316 
       
   317 // -----------------------------------------------------------------------------
       
   318 // (other items were commented in a header).
       
   319 // -----------------------------------------------------------------------------
       
   320 //
       
   321 EXPORT_C TBool CalenDateUtils::IsOnToday( const TTime& aTime )
       
   322     {
       
   323     TRACE_ENTRY_POINT;
       
   324 
       
   325     TRACE_EXIT_POINT;
       
   326     return CalenDateUtils::OnSameDay( aTime, Today() );
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // (other items were commented in a header).
       
   331 // -----------------------------------------------------------------------------
       
   332 //
       
   333 EXPORT_C TTime CalenDateUtils::DefaultTime( const TTime& aDate )
       
   334     {
       
   335     TRACE_ENTRY_POINT;
       
   336 
       
   337     TTime dateTime( Time::NullTTime() );
   223     // DD:MM:YY @ hh:mm:ss
   338     // DD:MM:YY @ hh:mm:ss
   224     dateTime = CalenDateUtils::beginningOfDay( date ); 
   339     dateTime = CalenDateUtils::BeginningOfDay( aDate ); 
   225     // DD:MM:YY @ 00:00:00
   340     // DD:MM:YY @ 00:00:00
   226     QTime time(KDefaultStartTime, 0, 0, 0);
   341     dateTime += TTimeIntervalHours( KDefaultStartTime ); // DD:MM:YY @ 08:00 am
   227     dateTime.setTime(time); // DD:MM:YY @ 08:00 am
   342         
   228   
   343     TRACE_EXIT_POINT;
   229     return dateTime;
   344     return dateTime;
   230     }      
   345     }      
   231 
   346 
   232  // -----------------------------------------------------------------------------
       
   233  // (other items were commented in a header).
       
   234  // -----------------------------------------------------------------------------
       
   235  //
       
   236  QDateTime CalenDateUtils::futureOf(const QDateTime& dateTime, int numOfDays)
       
   237      {
       
   238      QDateTime result;
       
   239      int dayNumber = dateTime.date().day();
       
   240      int numOfDaysInMonth = dateTime.date().daysInMonth();
       
   241      int month = dateTime.date().month();
       
   242      int year = dateTime.date().year();
       
   243      int buff = numOfDays;
       
   244      QDate date(year, month, dayNumber);
       
   245      while(dayNumber + buff > numOfDaysInMonth)
       
   246          {
       
   247          if(month == 12) {
       
   248          // If December,
       
   249          month = 1; // January
       
   250          year++;
       
   251          }
       
   252          else {
       
   253          month++;
       
   254          }
       
   255          // Create the qdate with these details
       
   256          date.setDate(year, month, 1);
       
   257          // check to see if it goes beyond the next month also
       
   258          buff = buff - (numOfDaysInMonth - dayNumber);
       
   259          dayNumber = 0;
       
   260          numOfDaysInMonth = date.daysInMonth();
       
   261          }
       
   262      
       
   263      // Add the buff days to the day number to get the result
       
   264      int day = dayNumber + buff;
       
   265      
       
   266      date.setYMD(date.year(), date.month(), day);
       
   267      result.setDate(date);
       
   268      result.setTime(dateTime.time());
       
   269      return result;
       
   270      }
       
   271  
       
   272  /*!
       
   273 	 Retruns the dateformat based current locale settings.
       
   274   */
       
   275  QString CalenDateUtils::dateFormatString()
       
   276  {
       
   277 	 HbExtendedLocale locale = HbExtendedLocale::system();
       
   278  
       
   279 	 QString dateFormat;
       
   280 	 switch (locale.dateStyle()) {
       
   281 		 case HbExtendedLocale::American:
       
   282 			 dateFormat.append("MM");
       
   283 			 dateFormat.append(locale.dateSeparator(1));
       
   284 			 dateFormat.append("dd");
       
   285 			 dateFormat.append(locale.dateSeparator(1));
       
   286 			 dateFormat.append("yyyy");
       
   287 			 break;
       
   288  
       
   289 		 case HbExtendedLocale::European:
       
   290 			 dateFormat.append("dd");
       
   291 			 dateFormat.append(locale.dateSeparator(1));
       
   292 			 dateFormat.append("MM");
       
   293 			 dateFormat.append(locale.dateSeparator(1));
       
   294 			 dateFormat.append("yyyy");
       
   295 			 break;
       
   296  
       
   297 		 case HbExtendedLocale::Japanese:
       
   298 			 dateFormat.append("yyyy");
       
   299 			 dateFormat.append(locale.dateSeparator(1));
       
   300 			 dateFormat.append("MM");
       
   301 			 dateFormat.append(locale.dateSeparator(1));
       
   302 			 dateFormat.append("dd");
       
   303 			 break;
       
   304 	 }
       
   305  
       
   306 	 return dateFormat;
       
   307  }
       
   308  
       
   309 // End of File
   347 // End of File