calendarui/commonutils/src/calendateutils.cpp
branchRCL_3
changeset 66 bd7edf625bdd
parent 65 12af337248b1
equal deleted inserted replaced
65:12af337248b1 66:bd7edf625bdd
     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 
    26 //  LOCAL CONSTANTS AND MACROS
    27 //  LOCAL CONSTANTS AND MACROS
    27 const int KDefaultStartTime(8);    // 8 am ( 0 to 23 hour scale)
    28 const TInt KDefaultStartTime(8);    // 8 am
    28 
    29 
    29 // ============================ MEMBER FUNCTIONS ==============================
    30 // ============================ MEMBER FUNCTIONS ==============================
    30 
    31 
    31 // ============================  CalenDateUtils  ==============================
    32 // ============================  CalenDateUtils  ==============================
    32 // -----------------------------------------------------------------------------
    33 // -----------------------------------------------------------------------------
    33 // ?classname::?member_function
    34 // ?classname::?member_function
    34 // ?implementation_description
    35 // ?implementation_description
    35 // (other items were commented in a header).
    36 // (other items were commented in a header).
    36 // -----------------------------------------------------------------------------
    37 // -----------------------------------------------------------------------------
    37 //
    38 //
    38  bool CalenDateUtils::onSameDay( const QDateTime& x, const QDateTime& y )
    39 EXPORT_C TBool CalenDateUtils::OnSameDay( const TTime& aX, const TTime& aY )
    39     {
    40     {
    40     return x.date().year()  == y.date().year() 
    41     TRACE_ENTRY_POINT;
    41         && x.date().month() == y.date().month()
    42     
    42         && x.date().day()   == y.date().day();
    43     TDateTime x = aX.DateTime();
    43     }
    44     TDateTime y = aY.DateTime();
    44 
    45     
    45 // -----------------------------------------------------------------------------
    46     TRACE_EXIT_POINT;
    46 // ?classname::?member_function
    47     return x.Year()  == y.Year() 
    47 // ?implementation_description
    48         && x.Month() == y.Month()
    48 // (other items were commented in a header).
    49         && x.Day()   == y.Day();
    49 // -----------------------------------------------------------------------------
    50     }
    50 //
    51 
    51  bool CalenDateUtils::onSameMonth( const QDateTime& x, const QDateTime& y )
    52 // -----------------------------------------------------------------------------
    52     {
    53 // ?classname::?member_function
    53     return ( x.date().year() == y.date().year() && x.date().month() == y.date().month() );
    54 // ?implementation_description
    54     }
    55 // (other items were commented in a header).
    55 
    56 // -----------------------------------------------------------------------------
    56 // -----------------------------------------------------------------------------
    57 //
    57 // ?classname::?member_function
    58 EXPORT_C TBool CalenDateUtils::OnSameMonth( const TTime& aX, const TTime& aY )
    58 // ?implementation_description
    59     {
    59 // (other items were commented in a header).
    60     TRACE_ENTRY_POINT;
    60 // -----------------------------------------------------------------------------
    61     
    61 //
    62     TDateTime x = aX.DateTime();
    62  QDateTime CalenDateUtils::beginningOfDay( const QDateTime& startTime )
    63     TDateTime y = aY.DateTime();
    63     {    
    64     
    64     QTime zeroTime(0,0,0,0);
    65     TRACE_EXIT_POINT;
    65     QDateTime ret(startTime.date(), zeroTime);
    66     return ( x.Year() == y.Year() && x.Month() == y.Month() );
    66     return ret;
    67     }
    67     }
    68 
    68 
    69 // -----------------------------------------------------------------------------
    69 // -----------------------------------------------------------------------------
    70 // ?classname::?member_function
    70 // ?classname::?member_function
    71 // ?implementation_description
    71 // ?implementation_description
    72 // (other items were commented in a header).
    72 // (other items were commented in a header).
    73 // -----------------------------------------------------------------------------
    73 // -----------------------------------------------------------------------------
    74 //
    74 //
    75 EXPORT_C TTime CalenDateUtils::BeginningOfDay( const TTime& aStartTime )
    75  QDateTime CalenDateUtils::displayTimeOnDay( const QDateTime& startTime,
    76     {
    76                                                  const QDateTime& day )
    77     TRACE_ENTRY_POINT;
    77     {
    78     
    78     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 ) )
    79         {
    97         {
    80         return beginningOfDay( day );
    98         TRACE_EXIT_POINT;
       
    99         return BeginningOfDay( aDay );
    81         }
   100         }
    82     else 
   101     else 
    83         {
   102         {
    84         return startTime;
   103         TRACE_EXIT_POINT;
       
   104         return aStartTime;
    85         }
   105         }
    86     }
   106     }
    87 
   107 
    88 // -----------------------------------------------------------------------------
   108 // -----------------------------------------------------------------------------
    89 // ?classname::?member_function
   109 // ?classname::?member_function
    90 // ?implementation_description
   110 // ?implementation_description
    91 // (other items were commented in a header).
   111 // (other items were commented in a header).
    92 // -----------------------------------------------------------------------------
   112 // -----------------------------------------------------------------------------
    93 //
   113 //
    94  bool CalenDateUtils::timeRangesIntersect( const QDateTime& xStart,
   114 EXPORT_C TBool CalenDateUtils::TimeRangesIntersect( const TTime& aXStart,
    95                                                     const QDateTime& xEnd,
   115                                                     const TTime& aXEnd,
    96                                                     const QDateTime& yStart,
   116                                                     const TTime& aYStart,
    97                                                     const QDateTime& yEnd )
   117                                                     const TTime& aYEnd )
    98     {
   118     {
    99     return (! ( yEnd <=  xStart || xEnd <= yStart )
   119     TRACE_ENTRY_POINT;
   100         || (xStart == xEnd && yStart <= xStart && xStart < yEnd)
   120     
   101         || (yStart == yEnd && xStart <= yStart && yStart < xEnd)
   121     TRACE_EXIT_POINT;
   102         || (xStart == xEnd && yStart == yEnd && xStart == yStart));
   122     return (! ( aYEnd <= aXStart || aXEnd <= aYStart ))
   103     }
   123         || (aXStart == aXEnd && aYStart <= aXStart && aXStart < aYEnd)
   104 
   124         || (aYStart == aYEnd && aXStart <= aYStart && aYStart < aXEnd)
   105 // -----------------------------------------------------------------------------
   125         || (aXStart == aXEnd && aYStart == aYEnd && aXStart == aYStart);
   106 // ?classname::?member_function
   126     }
   107 // ?implementation_description
   127 
   108 // (other items were commented in a header).
   128 // -----------------------------------------------------------------------------
   109 // -----------------------------------------------------------------------------
   129 // ?classname::?member_function
   110 //
   130 // ?implementation_description
   111  bool CalenDateUtils::isValidDay( const QDateTime& time )
   131 // (other items were commented in a header).
   112     {
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 EXPORT_C TBool CalenDateUtils::IsValidDay( const TTime& aTime )
       
   135     {
       
   136     TRACE_ENTRY_POINT;
       
   137     
   113     // Interim API supports range from 1900-2100, 
   138     // Interim API supports range from 1900-2100, 
   114     return ( minTime() <= time && time <= maxTime() );
   139     TRACE_EXIT_POINT;
   115     }
   140     return ( MinTime() <= aTime && aTime <= MaxTime() );
   116 
   141     }
   117 // -----------------------------------------------------------------------------
   142 
   118 // ?classname::?member_function
   143 // -----------------------------------------------------------------------------
   119 // ?implementation_description
   144 // ?classname::?member_function
   120 // (other items were commented in a header).
   145 // ?implementation_description
   121 // -----------------------------------------------------------------------------
   146 // (other items were commented in a header).
   122 //
   147 // -----------------------------------------------------------------------------
   123  QDateTime CalenDateUtils::limitToValidTime( const QDateTime& time )
   148 //
   124     {    
   149 EXPORT_C void CalenDateUtils::GetDayRangeL( const TTime& aStartDay,
   125     QDateTime valid = time;
   150                                             const TTime& aEndDay,
   126     valid = valid > maxTime() ? maxTime() : valid;
   151                                             CalCommon::TCalTimeRange& aRange )
   127     valid = valid < minTime() ? minTime() : valid;
   152     {
   128 
   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;
   129     return valid;
   211     return valid;
   130     }
   212     }
   131 
   213 
   132 // -----------------------------------------------------------------------------
   214 // -----------------------------------------------------------------------------
   133 // ?classname::?member_function
   215 // ?classname::?member_function
   134 // 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
   135 // date to be used.
   217 // date to be used.
   136 // (other items were commented in a header).
   218 // (other items were commented in a header).
   137 // -----------------------------------------------------------------------------
   219 // -----------------------------------------------------------------------------
   138 //
   220 //
   139  QDateTime CalenDateUtils::maxTime()
   221 EXPORT_C TTime CalenDateUtils::MaxTime()
   140     { 
   222     {
   141     return AgendaUtil::maxTime();
   223     TRACE_ENTRY_POINT;
       
   224     
       
   225     TTime time( TCalTime::MaxTime() - TTimeIntervalMinutes( 1 ) );
       
   226     
       
   227     TRACE_EXIT_POINT;
       
   228     return time;
   142     }
   229     }
   143 
   230 
   144 // -----------------------------------------------------------------------------
   231 // -----------------------------------------------------------------------------
   145 // ?classname::?member_function
   232 // ?classname::?member_function
   146 // Returns minimum time allowed, 1.1.1900 0:00 is min
   233 // Returns minimum time allowed, 1.1.1900 0:00 is min
   147 // (other items were commented in a header).
   234 // (other items were commented in a header).
   148 // -----------------------------------------------------------------------------
   235 // -----------------------------------------------------------------------------
   149 //
   236 //
   150  QDateTime CalenDateUtils::minTime()
   237 EXPORT_C TTime CalenDateUtils::MinTime()
   151     {
   238     {
   152     return AgendaUtil::minTime();
   239     TRACE_ENTRY_POINT;
   153     }
   240     
   154 
   241     TRACE_EXIT_POINT;
   155 // -----------------------------------------------------------------------------
   242     return TCalTime::MinTime();
   156 // (other items were commented in a header).
   243     }
   157 // -----------------------------------------------------------------------------
   244 
   158 //
   245 // -----------------------------------------------------------------------------
   159  int CalenDateUtils::timeOfDay( const QDateTime& dateTime )
   246 // (other items were commented in a header).
   160     {    
   247 // -----------------------------------------------------------------------------
   161     QDateTime midnight = beginningOfDay( dateTime );
   248 //
   162     int resultInSec = midnight.secsTo(dateTime);
   249 EXPORT_C TTimeIntervalMinutes CalenDateUtils::TimeOfDay( const TTime& aDateTime )
   163     
   250     {
   164     return (resultInSec/60);
   251     TRACE_ENTRY_POINT;
   165     }
   252     
   166 
   253     TTime midnight = CalenDateUtils::BeginningOfDay( aDateTime );
   167 // -----------------------------------------------------------------------------
   254     TTimeIntervalMinutes result;
   168 // (other items were commented in a header).
   255     aDateTime.MinutesFrom( midnight, result );
   169 // -----------------------------------------------------------------------------
   256     
   170 //
   257     TRACE_EXIT_POINT;
   171  QDateTime CalenDateUtils::roundToPreviousHour( const QDateTime& dateTime ) 
   258     return result;
   172     {
   259     }
   173     QTime time = dateTime.time();
   260 
   174     time.setHMS(time.hour(),0,0,0);
   261 // -----------------------------------------------------------------------------
   175     return QDateTime( dateTime.date(), time );
   262 // (other items were commented in a header).
   176     }
   263 // -----------------------------------------------------------------------------
   177 
   264 //
   178 // -----------------------------------------------------------------------------
   265 EXPORT_C TTime CalenDateUtils::RoundToPreviousHour( const TTime& aTime ) 
   179 // (other items were commented in a header).
   266     {
   180 // -----------------------------------------------------------------------------
   267     TRACE_ENTRY_POINT;
   181 //
   268 
   182  int CalenDateUtils::roundToPreviousHour( const int& minutes )
   269     TDateTime dt = aTime.DateTime();
   183     {
   270     dt.SetMinute(0);
   184     return ( (minutes / 60) * 60 );
   271     dt.SetSecond(0);
   185     }
   272     dt.SetMicroSecond(0);
   186 
   273 
   187 // -----------------------------------------------------------------------------
   274     TRACE_EXIT_POINT;
   188 // (other items were commented in a header).
   275     return TTime( dt );
   189 // -----------------------------------------------------------------------------
   276     }
   190 //
   277 
   191  QDateTime CalenDateUtils::now()
   278 // -----------------------------------------------------------------------------
   192     {
   279 // (other items were commented in a header).
   193     return QDateTime::currentDateTime();
   280 // -----------------------------------------------------------------------------
   194     }
   281 //
   195 
   282 EXPORT_C TTimeIntervalMinutes CalenDateUtils::RoundToPreviousHour( const TTimeIntervalMinutes& aMinutes )
   196 // -----------------------------------------------------------------------------
   283     {
   197 // (other items were commented in a header).
   284     TRACE_ENTRY_POINT;
   198 // -----------------------------------------------------------------------------
   285 
   199 //
   286     TRACE_EXIT_POINT;
   200  QDateTime CalenDateUtils::today()
   287     return TTimeIntervalMinutes( (aMinutes.Int() / 60) * 60 );
   201     {
   288     }
   202     return CalenDateUtils::beginningOfDay( now() );
   289 
   203     }
   290 // -----------------------------------------------------------------------------
   204 
   291 // (other items were commented in a header).
   205 // -----------------------------------------------------------------------------
   292 // -----------------------------------------------------------------------------
   206 // (other items were commented in a header).
   293 //
   207 // -----------------------------------------------------------------------------
   294 EXPORT_C TTime CalenDateUtils::Now()
   208 //
   295     {
   209  bool CalenDateUtils::isOnToday( const QDateTime& time )
   296     TRACE_ENTRY_POINT;
   210     {
   297 
   211     return CalenDateUtils::onSameDay( time, today() );
   298     TTime now;
   212     }
   299     now.HomeTime();
   213 
   300 
   214 // -----------------------------------------------------------------------------
   301     TRACE_EXIT_POINT;
   215 // (other items were commented in a header).
   302     return now;
   216 // -----------------------------------------------------------------------------
   303     }
   217 //
   304 
   218  QDateTime CalenDateUtils::defaultTime( const QDateTime& date )
   305 // -----------------------------------------------------------------------------
   219     {
   306 // (other items were commented in a header).
   220     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() );
   221     // DD:MM:YY @ hh:mm:ss
   338     // DD:MM:YY @ hh:mm:ss
   222     dateTime = CalenDateUtils::beginningOfDay( date ); 
   339     dateTime = CalenDateUtils::BeginningOfDay( aDate ); 
   223     // DD:MM:YY @ 00:00:00
   340     // DD:MM:YY @ 00:00:00
   224     QTime time(KDefaultStartTime, 0, 0, 0);
   341     dateTime += TTimeIntervalHours( KDefaultStartTime ); // DD:MM:YY @ 08:00 am
   225     dateTime.setTime(time); // DD:MM:YY @ 08:00 am
   342         
   226   
   343     TRACE_EXIT_POINT;
   227     return dateTime;
   344     return dateTime;
   228     }      
   345     }      
   229 
   346 
   230  // -----------------------------------------------------------------------------
       
   231  // (other items were commented in a header).
       
   232  // -----------------------------------------------------------------------------
       
   233  //
       
   234  QDateTime CalenDateUtils::futureOf(const QDateTime& dateTime, int numOfDays)
       
   235      {
       
   236      QDateTime result;
       
   237      int dayNumber = dateTime.date().day();
       
   238      int numOfDaysInMonth = dateTime.date().daysInMonth();
       
   239      int month = dateTime.date().month();
       
   240      int year = dateTime.date().year();
       
   241      int buff = numOfDays;
       
   242      QDate date(year, month, dayNumber);
       
   243      while(dayNumber + buff > numOfDaysInMonth)
       
   244          {
       
   245          if(month == 12) {
       
   246          // If December,
       
   247          month = 1; // January
       
   248          year++;
       
   249          }
       
   250          else {
       
   251          month++;
       
   252          }
       
   253          // Create the qdate with these details
       
   254          date.setDate(year, month, 1);
       
   255          // check to see if it goes beyond the next month also
       
   256          buff = buff - (numOfDaysInMonth - dayNumber);
       
   257          dayNumber = 0;
       
   258          numOfDaysInMonth = date.daysInMonth();
       
   259          }
       
   260      
       
   261      // Add the buff days to the day number to get the result
       
   262      int day = dayNumber + buff;
       
   263      
       
   264      date.setYMD(date.year(), date.month(), day);
       
   265      result.setDate(date);
       
   266      result.setTime(dateTime.time());
       
   267      return result;
       
   268      }
       
   269 // End of File
   347 // End of File