os/kernelhwsrv/kernel/eka/euser/us_time.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\euser\us_time.cpp
sl@0
    15
// System date and time functions
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
sl@0
    20
#include "us_std.h"
sl@0
    21
sl@0
    22
// Date and time related constants
sl@0
    23
sl@0
    24
static const TInt KMinutesToMicroSeconds = 60000000;
sl@0
    25
static const TInt KSecondsToMicroSeconds = 1000000;
sl@0
    26
static const TInt64 KDaysToMicroSeconds = I64LIT(86400000000);
sl@0
    27
static const TInt64 KHoursToMicroSeconds = I64LIT(3600000000);
sl@0
    28
sl@0
    29
// Days in each month
sl@0
    30
LOCAL_D const TInt8 mTab[2][12]=
sl@0
    31
    {
sl@0
    32
    {31,28,31,30,31,30,31,31,30,31,30,31}, // 28 days in Feb
sl@0
    33
    {31,29,31,30,31,30,31,31,30,31,30,31}  // 29 days in Feb
sl@0
    34
    };
sl@0
    35
sl@0
    36
// Days in year before 1st of each month
sl@0
    37
LOCAL_D const TInt cmTab[2][12]=
sl@0
    38
	{
sl@0
    39
	{0,31,59,90,120,151,181,212,243,273,304,334},
sl@0
    40
	{0,31,60,91,121,152,182,213,244,274,305,335}
sl@0
    41
	};
sl@0
    42
sl@0
    43
//
sl@0
    44
// Time::FormatL overflow handler
sl@0
    45
//
sl@0
    46
#if defined(_UNICODE)
sl@0
    47
NONSHARABLE_CLASS(TTimeOverflowLeave) : public TDes16Overflow
sl@0
    48
	{
sl@0
    49
public:
sl@0
    50
	virtual void Overflow(TDes16 &aDes);
sl@0
    51
	};
sl@0
    52
void TTimeOverflowLeave::Overflow(TDes16 &/*aDes*/)
sl@0
    53
	{
sl@0
    54
	User::Leave(KErrOverflow);
sl@0
    55
	}
sl@0
    56
#else
sl@0
    57
NONSHARABLE_CLASS(TTimeOverflowLeave) : public TDes8Overflow
sl@0
    58
	{
sl@0
    59
public:
sl@0
    60
	virtual void Overflow(TDes8 &aDes);
sl@0
    61
	};
sl@0
    62
void TTimeOverflowLeave::Overflow(TDes8 &/*aDes*/)
sl@0
    63
	{
sl@0
    64
	User::Leave(KErrOverflow);
sl@0
    65
	}
sl@0
    66
#endif
sl@0
    67
//
sl@0
    68
sl@0
    69
EXPORT_C TDateTime::TDateTime(TInt aYear,TMonth aMonth,TInt aDay,TInt aHour,TInt aMinute,TInt aSecond,TInt aMicroSecond)
sl@0
    70
//
sl@0
    71
// always panic on a bad date/time field
sl@0
    72
//
sl@0
    73
/**
sl@0
    74
Constructs the TDateTime object with the seven fields which comprise a date 
sl@0
    75
and time.
sl@0
    76
sl@0
    77
@param aYear        The year. No check is made for validity.
sl@0
    78
@param aMonth       The month. Range is EJanuary to EDecember.
sl@0
    79
@param aDay         The day. Range is zero to number of days in month minus one.
sl@0
    80
@param aHour        The hour. Range is 0 to 23.
sl@0
    81
@param aMinute      The minute. Range is 0 to 59.
sl@0
    82
@param aSecond      The second. Range is 0 to 59
sl@0
    83
@param aMicroSecond The microsecond. Range is 0 to 999999
sl@0
    84
sl@0
    85
@panic USER 3, if an attempt is made to set an invalid value for any of 
sl@0
    86
       the fields, except for the year. No check is made upon the validity
sl@0
    87
       of the year.
sl@0
    88
*/
sl@0
    89
	{
sl@0
    90
sl@0
    91
	TInt ret=Set(aYear,aMonth,aDay,aHour,aMinute,aSecond,aMicroSecond);
sl@0
    92
	__ASSERT_ALWAYS(ret==KErrNone,Panic(ETDateTimeBadDateTime));
sl@0
    93
	}
sl@0
    94
sl@0
    95
EXPORT_C TInt TDateTime::Set(TInt aYear,TMonth aMonth,TInt aDay,TInt aHour,TInt aMinute,TInt aSecond,TInt aMicroSecond)
sl@0
    96
//
sl@0
    97
// set the various time fields checking that each is valid
sl@0
    98
// bomb out as soon as invalid field is set to forestall causing a panic
sl@0
    99
//
sl@0
   100
/**
sl@0
   101
Sets all date and time components.
sl@0
   102
sl@0
   103
Note:
sl@0
   104
sl@0
   105
1. When setting the day and month, subtract one because the ranges are offset 
sl@0
   106
   from zero. 
sl@0
   107
sl@0
   108
2. If the function returns an error, only those fields preceding the field which 
sl@0
   109
   caused the error will be changed. For example, if the hour is out of range, 
sl@0
   110
   the year, month and day will be set, all other components will remain unchanged.
sl@0
   111
sl@0
   112
@param aYear        Year. No check is made on its validity, except that if the
sl@0
   113
                    date is set to February 29th, the year can only be set to a
sl@0
   114
                    leap year, otherwise  an error is returned.
sl@0
   115
@param aMonth       Month. The valid range is EJanuary to EDecember. If an
sl@0
   116
                    attempt is made to set an invalid month, or if the current
sl@0
   117
                    day number in the month is greater than or equal to the
sl@0
   118
                    number of days in the new month, an error is returned.
sl@0
   119
@param aDay         The number of the day within the month, offset from zero.
sl@0
   120
                    If greater than or equal to the total number of days in
sl@0
   121
                    the month,an error is returned.
sl@0
   122
@param aHour        Hour. Range is 0 to 23.
sl@0
   123
@param aMinute      Minute. Range is 0 to 59.
sl@0
   124
@param aSecond      Second. Range is 0 to 59.
sl@0
   125
@param aMicroSecond Microsecond. Range is 0 to 999999.
sl@0
   126
sl@0
   127
@return KErrNone if successful, KErrGeneral if not.
sl@0
   128
*/
sl@0
   129
	{
sl@0
   130
sl@0
   131
	iYear=aYear;
sl@0
   132
sl@0
   133
	if (aMonth<EJanuary || aMonth>EDecember)
sl@0
   134
		return KErrGeneral;
sl@0
   135
	iMonth=aMonth;
sl@0
   136
sl@0
   137
	if (aDay<0 || aDay>=Time::DaysInMonth(iYear,iMonth))
sl@0
   138
		return KErrGeneral;
sl@0
   139
	iDay=aDay;
sl@0
   140
sl@0
   141
	if (aHour<0 || aHour>=24)
sl@0
   142
		return KErrGeneral;
sl@0
   143
	iHour=aHour;
sl@0
   144
sl@0
   145
	if (aMinute<0 || aMinute>=60)
sl@0
   146
		return KErrGeneral;
sl@0
   147
	iMinute=aMinute;
sl@0
   148
sl@0
   149
	if (aSecond<0 || aSecond>=60)
sl@0
   150
		return KErrGeneral;
sl@0
   151
	iSecond=aSecond;
sl@0
   152
sl@0
   153
	if (aMicroSecond<0 || aMicroSecond>=1000000)
sl@0
   154
		return KErrGeneral;
sl@0
   155
	iMicroSecond=aMicroSecond;
sl@0
   156
sl@0
   157
	return KErrNone;
sl@0
   158
	}
sl@0
   159
sl@0
   160
EXPORT_C TInt TDateTime::SetYear(TInt aYear)
sl@0
   161
//
sl@0
   162
// doesnt let you reset 29th February to non-leap year, no check on year range
sl@0
   163
//
sl@0
   164
/**
sl@0
   165
Sets the year without a leap year check.
sl@0
   166
sl@0
   167
No check is made on the validity of the year except that if the current date
sl@0
   168
is February 29th, the year can only be changed to another leap year, otherwise
sl@0
   169
an error is returned.
sl@0
   170
sl@0
   171
@param aYear The year.
sl@0
   172
sl@0
   173
@return KErrNone if successful, KErrGeneral if not.
sl@0
   174
*/
sl@0
   175
	{
sl@0
   176
sl@0
   177
	if (iDay>=Time::DaysInMonth(aYear,iMonth))
sl@0
   178
		return KErrGeneral;
sl@0
   179
	iYear=aYear;
sl@0
   180
	return KErrNone;
sl@0
   181
	}
sl@0
   182
sl@0
   183
EXPORT_C TInt TDateTime::SetYearLeapCheck(TInt aYear)
sl@0
   184
//
sl@0
   185
// lets you reset 29th February to non-leap year(moves date to 28th/Feb), no check on year range
sl@0
   186
//
sl@0
   187
/**
sl@0
   188
Sets the year with a leap year check.
sl@0
   189
sl@0
   190
Unlike SetYear(), if the date is the 29th February, this function allows
sl@0
   191
the year to be set to a non-leap year. In this case, the date is reset to
sl@0
   192
the 28th February.
sl@0
   193
sl@0
   194
@param aYear The year.
sl@0
   195
sl@0
   196
@return KErrNone if successful, KErrGeneral if not.
sl@0
   197
sl@0
   198
@see TDateTime::SetYear
sl@0
   199
*/
sl@0
   200
	{
sl@0
   201
sl@0
   202
	if (iDay>=Time::DaysInMonth(aYear,iMonth))
sl@0
   203
        iDay=27;
sl@0
   204
    iYear=aYear;
sl@0
   205
	return KErrNone;
sl@0
   206
	}
sl@0
   207
sl@0
   208
EXPORT_C TInt TDateTime::SetMonth(TMonth aMonth)
sl@0
   209
/**
sl@0
   210
Sets the month component of the date/time.
sl@0
   211
sl@0
   212
@param aMonth The month to be set. The range is from EJanuary to EDecember.
sl@0
   213
              If an attempt is made to set an invalid month, or if the current
sl@0
   214
              day number in the month is greater than or equal to the number of
sl@0
   215
              days in the new month, an error is returned.
sl@0
   216
              
sl@0
   217
@return KErrNone if successful, KErrGeneral if not.
sl@0
   218
*/
sl@0
   219
	{
sl@0
   220
sl@0
   221
	if (aMonth<EJanuary || aMonth>EDecember || iDay>=Time::DaysInMonth(iYear,aMonth))
sl@0
   222
		return KErrGeneral;
sl@0
   223
	iMonth=aMonth;
sl@0
   224
	return KErrNone;
sl@0
   225
	}
sl@0
   226
sl@0
   227
EXPORT_C TInt TDateTime::SetDay(TInt aDay)
sl@0
   228
/**
sl@0
   229
Sets the day component of the date/time.
sl@0
   230
sl@0
   231
@param aDay The number of the day within the month, offset from zero. If equal 
sl@0
   232
            to or greater than the total number of days in the month, an error
sl@0
   233
            is returned.
sl@0
   234
            
sl@0
   235
@return KErrNone if successful, KErrGeneral if not.
sl@0
   236
*/
sl@0
   237
	{
sl@0
   238
sl@0
   239
	if (aDay<0 || aDay>=Time::DaysInMonth(iYear,iMonth))
sl@0
   240
		return KErrGeneral;
sl@0
   241
	iDay=aDay;
sl@0
   242
	return KErrNone;
sl@0
   243
	}
sl@0
   244
sl@0
   245
EXPORT_C TInt TDateTime::SetHour(TInt aHour)
sl@0
   246
/**
sl@0
   247
Sets the hour component of the date/time.
sl@0
   248
sl@0
   249
@param aHour The hour. Range is 0 to 23.
sl@0
   250
sl@0
   251
@return KErrNone if successful, KErrGeneral if not.
sl@0
   252
*/
sl@0
   253
	{
sl@0
   254
sl@0
   255
	if (aHour<0 || aHour>=24) // GC - bug fix
sl@0
   256
		return KErrGeneral;
sl@0
   257
	iHour=aHour;
sl@0
   258
	return KErrNone;
sl@0
   259
	}
sl@0
   260
sl@0
   261
EXPORT_C TInt TDateTime::SetMinute(TInt aMinute)
sl@0
   262
/**
sl@0
   263
Sets the minute component of the date/time.
sl@0
   264
sl@0
   265
@param aMinute The minute. Range is 0 to 59.
sl@0
   266
sl@0
   267
@return KErrNone if successful, KErrGeneral if not.
sl@0
   268
*/
sl@0
   269
	{
sl@0
   270
sl@0
   271
	if (aMinute<0 || aMinute>=60)
sl@0
   272
		return KErrGeneral;
sl@0
   273
	iMinute=aMinute;
sl@0
   274
	return KErrNone;
sl@0
   275
	}
sl@0
   276
sl@0
   277
EXPORT_C TInt TDateTime::SetSecond(TInt aSecond)
sl@0
   278
/**
sl@0
   279
Sets the second component of the date/time.
sl@0
   280
sl@0
   281
@param aSecond The second. Range is 0 to 59.
sl@0
   282
sl@0
   283
@return KErrNone if successful, KErrGeneral if not.
sl@0
   284
*/
sl@0
   285
	{
sl@0
   286
sl@0
   287
	if (aSecond<0 || aSecond>=60)
sl@0
   288
		return KErrGeneral;
sl@0
   289
	iSecond=aSecond;
sl@0
   290
	return KErrNone;
sl@0
   291
	}
sl@0
   292
sl@0
   293
EXPORT_C TInt TDateTime::SetMicroSecond(TInt aMicroSecond)
sl@0
   294
/**
sl@0
   295
Sets the microsecond component of the date/time.
sl@0
   296
sl@0
   297
@param aMicroSecond The microsecond. Range is 0 to 999999.
sl@0
   298
sl@0
   299
@return KErrNone if successful, KErrGeneral if not.
sl@0
   300
*/
sl@0
   301
	{
sl@0
   302
sl@0
   303
	if (aMicroSecond<0 || aMicroSecond>=1000000)
sl@0
   304
		return KErrGeneral;
sl@0
   305
	iMicroSecond=aMicroSecond;
sl@0
   306
	return KErrNone;
sl@0
   307
	}
sl@0
   308
sl@0
   309
// class TTime
sl@0
   310
sl@0
   311
EXPORT_C TTime::TTime(const TDesC &aString)
sl@0
   312
/**
sl@0
   313
Constructs a TTime object with a text string.
sl@0
   314
sl@0
   315
The string consists of up to three components, any or all of which
sl@0
   316
may be omitted:
sl@0
   317
sl@0
   318
1. year, month and day, followed by a colon
sl@0
   319
sl@0
   320
2. hour, minute and second, followed by a dot
sl@0
   321
sl@0
   322
3. microsecond
sl@0
   323
sl@0
   324
When all three components are present, the string should take the form:
sl@0
   325
sl@0
   326
YYYYMMDD:HHMMSS.MMMMMM
sl@0
   327
sl@0
   328
The conversion from text to time is carried out in the same manner as that 
sl@0
   329
used in TTime::Set().
sl@0
   330
sl@0
   331
For a list of the range of valid values for date and time components,
sl@0
   332
see TDateTime::Set().
sl@0
   333
sl@0
   334
@param aString Date and time string for initializing the TTime object. 
sl@0
   335
sl@0
   336
@panic USER 113, if the string is syntactically incorrect, for example, if 
sl@0
   337
                 neither a colon nor a dot is present, or if any component of
sl@0
   338
                 the date or time is assigned an invalid value, or the year
sl@0
   339
                 is negative.
sl@0
   340
sl@0
   341
@see TTime::Set
sl@0
   342
@see TDateTime::Set
sl@0
   343
*/
sl@0
   344
	{
sl@0
   345
sl@0
   346
	__ASSERT_ALWAYS(Set(aString)==KErrNone,Panic(ETTimeValueOutOfRange));
sl@0
   347
	}
sl@0
   348
sl@0
   349
EXPORT_C TTime::TTime(const TDateTime &aDateTime) : iTime(Convert(aDateTime).Int64())
sl@0
   350
/**
sl@0
   351
Constructs a TTime object with the seven fields which comprise a date and time.
sl@0
   352
sl@0
   353
@param aDateTime Date and time to which to initialise the TTime object.
sl@0
   354
*/
sl@0
   355
sl@0
   356
    {}
sl@0
   357
sl@0
   358
EXPORT_C TInt TTime::Set(const TDesC &aString)
sl@0
   359
//
sl@0
   360
// Convert string to time. String is in the format:
sl@0
   361
//
sl@0
   362
// YYYYMMDD:HHMMSS.MMMMMM
sl@0
   363
//
sl@0
   364
// Any part may be ommitted, but either the
sl@0
   365
// dot or colon or both must be present
sl@0
   366
//
sl@0
   367
/**
sl@0
   368
Assigns a date and time contained in a descriptor to this TTime object.
sl@0
   369
sl@0
   370
The string consists of up to three components, any or all of which may
sl@0
   371
be omitted:
sl@0
   372
sl@0
   373
1. year, month and day, followed by a colon 
sl@0
   374
sl@0
   375
2. hour, minute and second, followed by a dot 
sl@0
   376
sl@0
   377
3. microsecond
sl@0
   378
sl@0
   379
When all three components are present, the string should take the form:
sl@0
   380
sl@0
   381
YYYYMMDD:HHMMSS.MMMMMM
sl@0
   382
sl@0
   383
If omitted, the first component is set to January 1st 0 AD nominal Gregorian. 
sl@0
   384
If either the second or third components are omitted, they are set to zero.
sl@0
   385
sl@0
   386
Notes:
sl@0
   387
sl@0
   388
1. The month and day values are offset from zero.
sl@0
   389
sl@0
   390
2. The only situations in which either the colon or dot may be omitted are as 
sl@0
   391
   follows:
sl@0
   392
   
sl@0
   393
   2.1 If the microsecond component is omitted, the preceding dot may also
sl@0
   394
       be omitted.
sl@0
   395
sl@0
   396
   2.2 The colon can be omitted only if a dot is located at string position
sl@0
   397
       zero (indicating that the first two components are missing), or at
sl@0
   398
       string position six (indicating that the first component only is
sl@0
   399
       missing).
sl@0
   400
sl@0
   401
@param aString The date and time to be assigned to this TTime object.
sl@0
   402
       
sl@0
   403
@return KErrNone if successful,
sl@0
   404
        KErrGeneral if the string is syntactically incorrect, for example,
sl@0
   405
        if neither a colon nor a dot is present, or if any component of the
sl@0
   406
        date or time is given an invalid value, or the year is negative.
sl@0
   407
        For a list of valid values for date and time components,
sl@0
   408
        see TDateTime::Set().
sl@0
   409
        If an error occurs, the date and time will remain unchanged.
sl@0
   410
*/
sl@0
   411
	{
sl@0
   412
sl@0
   413
//
sl@0
   414
// Get position of the colon and dot separators
sl@0
   415
//
sl@0
   416
    TInt colon=aString.Locate(':');
sl@0
   417
    TInt dot=aString.Locate('.');
sl@0
   418
sl@0
   419
    if(colon==KErrNotFound && dot==KErrNotFound)
sl@0
   420
        return(KErrGeneral);
sl@0
   421
//
sl@0
   422
// Zero parts that aren't supplied
sl@0
   423
//
sl@0
   424
    TInt yy=0;
sl@0
   425
    TInt mm=0;
sl@0
   426
    TInt dd=0;
sl@0
   427
    TInt hr=0;
sl@0
   428
    TInt mi=0;
sl@0
   429
    TInt se=0;
sl@0
   430
    TInt ms=0;
sl@0
   431
//
sl@0
   432
// Convert YYYYMMDD if present
sl@0
   433
//
sl@0
   434
    switch(colon)
sl@0
   435
        {
sl@0
   436
        case 0:
sl@0
   437
       	    break;
sl@0
   438
        case KErrNotFound:
sl@0
   439
            if(dot!=0 && dot!=6)
sl@0
   440
                return(KErrGeneral);
sl@0
   441
            colon=-1;
sl@0
   442
            break;
sl@0
   443
        case 8:
sl@0
   444
	   		{
sl@0
   445
            TLex y=aString.Left(4);
sl@0
   446
            TLex m=aString.Mid(4,2);
sl@0
   447
            TLex d=aString.Mid(6,2);
sl@0
   448
            y.Val(yy);
sl@0
   449
            m.Val(mm);
sl@0
   450
            d.Val(dd);
sl@0
   451
	    	}
sl@0
   452
            break;
sl@0
   453
        default: // Colon in wrong position - return error
sl@0
   454
            return(KErrGeneral);
sl@0
   455
        }
sl@0
   456
//
sl@0
   457
// Convert HHMMSS if present
sl@0
   458
//
sl@0
   459
    if(dot==KErrNotFound)
sl@0
   460
        dot=aString.Length();
sl@0
   461
     
sl@0
   462
    if(dot==colon+7)
sl@0
   463
        {
sl@0
   464
        TLex h=aString.Mid(dot-6,2);
sl@0
   465
        TLex m=aString.Mid(dot-4,2);
sl@0
   466
        TLex s=aString.Mid(dot-2,2);
sl@0
   467
        h.Val(hr);
sl@0
   468
        m.Val(mi);
sl@0
   469
        s.Val(se);
sl@0
   470
        }
sl@0
   471
    else if(dot!=KErrNotFound && dot!=0 && dot!=colon+1)
sl@0
   472
    	return(KErrGeneral);
sl@0
   473
sl@0
   474
    if(dot!=KErrNotFound)
sl@0
   475
        {
sl@0
   476
        if(aString.Length()>dot+7)
sl@0
   477
            return(KErrGeneral); // microseconds is more than 6 digits
sl@0
   478
        if(dot<aString.Length())
sl@0
   479
        	{
sl@0
   480
        	TLex m=aString.Mid(dot+1);
sl@0
   481
        	m.Val(ms);
sl@0
   482
        	}
sl@0
   483
        }
sl@0
   484
        
sl@0
   485
//
sl@0
   486
// Set the time! Do not construct newtime using the values or
sl@0
   487
// it may cause TTime::Set() to panic rather than return an error
sl@0
   488
//
sl@0
   489
	TDateTime newtime;
sl@0
   490
	if(newtime.Set(yy,TMonth(mm),dd,hr,mi,se,ms)!=KErrNone)
sl@0
   491
		return(KErrGeneral);
sl@0
   492
    (*this)=newtime;
sl@0
   493
	return KErrNone;
sl@0
   494
	}
sl@0
   495
sl@0
   496
EXPORT_C TInt TTime::HomeTimeSecure()
sl@0
   497
/**
sl@0
   498
Sets the date and time of this TTime to the secure home time. 
sl@0
   499
Returns KErrNoSecureTime if there is no secure time source
sl@0
   500
*/
sl@0
   501
	{
sl@0
   502
	TInt utOffset=0;
sl@0
   503
	TInt r = Exec::TimeNowSecure(*(TInt64*)this,utOffset);
sl@0
   504
    operator+=(TTimeIntervalSeconds(utOffset));
sl@0
   505
	return r;
sl@0
   506
	}
sl@0
   507
sl@0
   508
EXPORT_C TInt TTime::UniversalTimeSecure()
sl@0
   509
/**
sl@0
   510
Sets the date and time of this TTime to the secure universal time.
sl@0
   511
*/
sl@0
   512
	{
sl@0
   513
	TInt utOffset=0;
sl@0
   514
	return Exec::TimeNowSecure(*(TInt64*)this,utOffset);
sl@0
   515
	}
sl@0
   516
sl@0
   517
EXPORT_C void TTime::HomeTime()
sl@0
   518
/**
sl@0
   519
Sets the date and time of this TTime to the home time.
sl@0
   520
*/
sl@0
   521
	{
sl@0
   522
	TInt utOffset=0;
sl@0
   523
	Exec::TimeNow(*(TInt64*)this,utOffset);
sl@0
   524
    operator+=(TTimeIntervalSeconds(utOffset));
sl@0
   525
	}
sl@0
   526
sl@0
   527
EXPORT_C void TTime::UniversalTime()
sl@0
   528
/**
sl@0
   529
Sets the date and time of this TTime to the universal time.
sl@0
   530
*/
sl@0
   531
	{
sl@0
   532
	TInt utOffset=0;
sl@0
   533
	Exec::TimeNow(*(TInt64*)this,utOffset);
sl@0
   534
	}
sl@0
   535
sl@0
   536
EXPORT_C void TTime::RoundUpToNextMinute()
sl@0
   537
/**
sl@0
   538
Rounds this TTime up to the next minute.
sl@0
   539
sl@0
   540
Both the seconds and microseconds components are set to zero.
sl@0
   541
*/
sl@0
   542
	{
sl@0
   543
sl@0
   544
	if (iTime>0)
sl@0
   545
		iTime+=59999999;
sl@0
   546
//*	TInt64 remainder;
sl@0
   547
//*	Int64().DivMod(60000000,remainder);
sl@0
   548
//*	iTime-=remainder;	
sl@0
   549
	iTime-=iTime%60000000;
sl@0
   550
	}
sl@0
   551
sl@0
   552
TTime TTime::Convert(const TDateTime &aDateTime)
sl@0
   553
//
sl@0
   554
// converts TDateTime into a TTime, doesnt check for overflows
sl@0
   555
//
sl@0
   556
	{
sl@0
   557
	
sl@0
   558
	TInt days=365*aDateTime.Year()+Time::LeapYearsUpTo(aDateTime.Year());
sl@0
   559
	TBool isleap=Time::IsLeapYear(aDateTime.Year());
sl@0
   560
	days+=cmTab[isleap][aDateTime.Month()];
sl@0
   561
	days+=aDateTime.Day();
sl@0
   562
sl@0
   563
	TUint sum=aDateTime.MicroSecond()+aDateTime.Second()*KSecondsToMicroSeconds+aDateTime.Minute()*KMinutesToMicroSeconds;
sl@0
   564
	return(((TInt64(days*3)<<3)+TInt64(aDateTime.Hour()))*KHoursToMicroSeconds+TInt64(sum));
sl@0
   565
	}
sl@0
   566
sl@0
   567
EXPORT_C TTime &TTime::operator=(const TDateTime &aDateTime)
sl@0
   568
/**
sl@0
   569
Assigns a TDateTime object to this TTime object.
sl@0
   570
sl@0
   571
@param aDateTime The date and time to assign to this TTime object.
sl@0
   572
sl@0
   573
@return This TTime object.
sl@0
   574
*/
sl@0
   575
	{
sl@0
   576
sl@0
   577
	iTime=Convert(aDateTime).Int64();
sl@0
   578
	return(*this);
sl@0
   579
	}
sl@0
   580
sl@0
   581
EXPORT_C TDateTime TTime::DateTime() const
sl@0
   582
//
sl@0
   583
// converts iTime back into its TDateTime components
sl@0
   584
//
sl@0
   585
/**
sl@0
   586
Converts the TTime object into a TDateTime object.
sl@0
   587
sl@0
   588
This conversion must be done before the seven fields which comprise a date
sl@0
   589
and time can be accessed.
sl@0
   590
sl@0
   591
@return The components of the time, indicating year, month, day, hour, minute, 
sl@0
   592
        second, microsecond.
sl@0
   593
*/
sl@0
   594
	{
sl@0
   595
sl@0
   596
	TInt64 rem;
sl@0
   597
	TInt64 daysSince0AD64(iTime);
sl@0
   598
	
sl@0
   599
	rem = daysSince0AD64 % KDaysToMicroSeconds;
sl@0
   600
	daysSince0AD64 /= KDaysToMicroSeconds;
sl@0
   601
sl@0
   602
	TInt daysSince0AD = static_cast<TInt>(daysSince0AD64);
sl@0
   603
sl@0
   604
	TInt year;
sl@0
   605
	TInt daysLeft;
sl@0
   606
	if (iTime<0)
sl@0
   607
		{ // -1 to make daysLeft +ve and assume leap year every 4 years
sl@0
   608
		if (rem!=TInt64(0))
sl@0
   609
			{
sl@0
   610
			daysSince0AD--;
sl@0
   611
			rem=iTime-TInt64(daysSince0AD)*KDaysToMicroSeconds;
sl@0
   612
			}
sl@0
   613
		year=(4*daysSince0AD)/((4*365)+1);
sl@0
   614
		if ((4*daysSince0AD)%((4*365)+1))
sl@0
   615
			year--;
sl@0
   616
		daysLeft=daysSince0AD-((year*365)+Time::LeapYearsUpTo(year));
sl@0
   617
		}
sl@0
   618
	else
sl@0
   619
		{ // after 1600 leap years less than every four years
sl@0
   620
		year=(4*daysSince0AD)/((4*365)+1);
sl@0
   621
		daysLeft=daysSince0AD-((year*365)+Time::LeapYearsUpTo(year));
sl@0
   622
		TInt daysInYear=365+Time::IsLeapYear(year);
sl@0
   623
	    while (daysLeft>=daysInYear)
sl@0
   624
		    {
sl@0
   625
			year++;
sl@0
   626
	        daysLeft-=daysInYear;
sl@0
   627
			daysInYear=365+Time::IsLeapYear(year);
sl@0
   628
			}
sl@0
   629
		}
sl@0
   630
sl@0
   631
	TDateTime result(0,EJanuary,0,0,0,0,0);
sl@0
   632
	result.SetYear(year);
sl@0
   633
sl@0
   634
   	TBool isleap=Time::IsLeapYear(year);
sl@0
   635
    TInt month=11;
sl@0
   636
	const TInt* pCM=&(cmTab[isleap][11])+1;
sl@0
   637
	while(daysLeft<*--pCM)
sl@0
   638
		month--;
sl@0
   639
	daysLeft-=*pCM;
sl@0
   640
sl@0
   641
	result.SetMonth((TMonth)month);
sl@0
   642
	result.SetDay(daysLeft);
sl@0
   643
sl@0
   644
	TInt hour = static_cast<TInt>(rem >> 10) / 3515625;	// 3515625=KHoursToMicroSeconds/1024
sl@0
   645
	result.SetHour(hour);
sl@0
   646
	TUint rem32=I64LOW(rem-(TInt64(hour*3515625)<<10));
sl@0
   647
	TUint min=rem32/KMinutesToMicroSeconds;
sl@0
   648
	result.SetMinute((TInt)min);
sl@0
   649
	rem32-=min*KMinutesToMicroSeconds;
sl@0
   650
	TUint sec=rem32/KSecondsToMicroSeconds;
sl@0
   651
	result.SetSecond((TInt)sec);
sl@0
   652
	rem32-=sec*KSecondsToMicroSeconds;
sl@0
   653
	result.SetMicroSecond(TInt(rem32));
sl@0
   654
	return(result);
sl@0
   655
	}
sl@0
   656
sl@0
   657
EXPORT_C TTimeIntervalMicroSeconds TTime::MicroSecondsFrom(TTime aTime) const
sl@0
   658
//
sl@0
   659
// this - aTime
sl@0
   660
//
sl@0
   661
/**
sl@0
   662
Calculates the number of microseconds difference between the specified TTime
sl@0
   663
and this TTime.
sl@0
   664
sl@0
   665
@param aTime The time to be compared with this TTime.
sl@0
   666
sl@0
   667
@return Difference in microseconds between the two times. If the time specified 
sl@0
   668
        in the argument is later than this TTime, this value is negative.
sl@0
   669
*/
sl@0
   670
	{
sl@0
   671
sl@0
   672
	TInt64 difference=iTime-aTime.Int64();
sl@0
   673
	return(difference);
sl@0
   674
	}
sl@0
   675
sl@0
   676
EXPORT_C TInt TTime::SecondsFrom(TTime aTime,TTimeIntervalSeconds &aInterval) const
sl@0
   677
//
sl@0
   678
// this - aTime as whole seconds
sl@0
   679
// this function may fail if difference > no of seconds that can be represented in a TInt
sl@0
   680
//
sl@0
   681
/**
sl@0
   682
Calculates the number of seconds difference between the specified TTime and
sl@0
   683
this TTime.
sl@0
   684
sl@0
   685
The difference may be positive or negative.
sl@0
   686
sl@0
   687
@param aTime     The time to be compared with this TTime.
sl@0
   688
@param aInterval On return contains the difference in seconds between the two 
sl@0
   689
                 times. If the time specified in the first argument is later than
sl@0
   690
                 this TTime, then this returned value is negative.
sl@0
   691
                 
sl@0
   692
@return Error code. KErrNone if successful. 
sl@0
   693
                    KErrOverflow, if the calculated interval is too large for
sl@0
   694
                    a 32-bit integer.
sl@0
   695
*/
sl@0
   696
	{
sl@0
   697
	TInt64 diff;
sl@0
   698
	if (iTime>aTime.Int64())
sl@0
   699
		{
sl@0
   700
		diff= TInt64(TUint64(iTime-aTime.Int64())/KSecondsToMicroSeconds);	
sl@0
   701
		}
sl@0
   702
	else 
sl@0
   703
		{
sl@0
   704
		diff= -TInt64(TUint64(aTime.Int64()-iTime)/KSecondsToMicroSeconds);
sl@0
   705
		}	
sl@0
   706
	if (diff>KMaxTInt || diff<KMinTInt)	
sl@0
   707
	    return KErrOverflow; 
sl@0
   708
	aInterval = static_cast<TInt>(diff);
sl@0
   709
	return KErrNone;
sl@0
   710
	}
sl@0
   711
	
sl@0
   712
EXPORT_C TInt TTime::MinutesFrom(TTime aTime,TTimeIntervalMinutes &aInterval) const
sl@0
   713
//
sl@0
   714
// iTime - aTime as whole minutes
sl@0
   715
// function may fail if difference can't be represented as a TInt
sl@0
   716
//
sl@0
   717
/**
sl@0
   718
Calculates the number of minutes difference between the specified TTime and
sl@0
   719
this TTime.
sl@0
   720
sl@0
   721
The difference may be positive or negative.
sl@0
   722
sl@0
   723
@param aTime     The time to be compared with this TTime.
sl@0
   724
@param aInterval On return contains the difference in minutes between the two 
sl@0
   725
                 times. If the time specified in the first argument is later
sl@0
   726
                 than this TTime, then this returned value is negative.
sl@0
   727
                 
sl@0
   728
@return Error code. KErrNone if successful. 
sl@0
   729
                    KErrOverflow, if the calculated interval is too large for
sl@0
   730
                    a 32-bit integer.
sl@0
   731
*/
sl@0
   732
	{
sl@0
   733
	TInt64 diff;
sl@0
   734
	if (iTime>aTime.Int64())
sl@0
   735
		{
sl@0
   736
		diff= TInt64(TUint64(iTime-aTime.Int64())/KMinutesToMicroSeconds);	
sl@0
   737
		}
sl@0
   738
	else 
sl@0
   739
		{
sl@0
   740
		diff= -TInt64(TUint64(aTime.Int64()-iTime)/KMinutesToMicroSeconds);
sl@0
   741
		}	
sl@0
   742
	if (diff>KMaxTInt || diff<KMinTInt)	
sl@0
   743
	    return KErrOverflow; 
sl@0
   744
	aInterval = static_cast<TInt>(diff);
sl@0
   745
	return KErrNone; 
sl@0
   746
	}
sl@0
   747
sl@0
   748
EXPORT_C TInt TTime::HoursFrom(TTime aTime,TTimeIntervalHours &aInterval) const
sl@0
   749
//
sl@0
   750
// iTime - aTime as whole hours
sl@0
   751
// function may fail if difference can't be represented as a TInt
sl@0
   752
//
sl@0
   753
/**
sl@0
   754
Calculates the number of hours difference between the specified TTime and
sl@0
   755
this TTime. 
sl@0
   756
sl@0
   757
The difference may be positive or negative.
sl@0
   758
sl@0
   759
@param aTime     The time to be compared with this TTime.
sl@0
   760
@param aInterval On return contains the difference in hours between the two 
sl@0
   761
                 times. If the time specified in the first argument is later
sl@0
   762
                 than this TTime, then this returned value is negative.
sl@0
   763
                 
sl@0
   764
@return Error code. KErrNone if successful. 
sl@0
   765
                    KErrOverflow, if the calculated interval is too large for
sl@0
   766
                    a 32-bit integer.
sl@0
   767
*/
sl@0
   768
	{
sl@0
   769
	TInt64 diff;
sl@0
   770
	if (iTime>aTime.Int64())
sl@0
   771
		{
sl@0
   772
		diff= TInt64(TUint64(iTime-aTime.Int64())/KHoursToMicroSeconds);	
sl@0
   773
		}
sl@0
   774
	else 
sl@0
   775
		{
sl@0
   776
		diff= -TInt64(TUint64(aTime.Int64()-iTime)/KHoursToMicroSeconds);
sl@0
   777
		}
sl@0
   778
	if (diff>KMaxTInt || diff<KMinTInt)	
sl@0
   779
	    return KErrOverflow; 
sl@0
   780
	aInterval = static_cast<TInt>(diff);
sl@0
   781
	return KErrNone;
sl@0
   782
	}  
sl@0
   783
sl@0
   784
sl@0
   785
EXPORT_C TTimeIntervalDays TTime::DaysFrom(TTime aTime) const
sl@0
   786
//
sl@0
   787
// iTime - aTime as whole days
sl@0
   788
//
sl@0
   789
/**
sl@0
   790
Calculates the number of days difference between the specified TTime and
sl@0
   791
this TTime. 
sl@0
   792
sl@0
   793
The difference may be positive or negative.
sl@0
   794
sl@0
   795
@param aTime  The time to be compared with this TTime.
sl@0
   796
sl@0
   797
@return Difference in days between the two times. If the time specified in 
sl@0
   798
        aTime is later than this TTime, the returned value will be negative.
sl@0
   799
*/
sl@0
   800
	{
sl@0
   801
	if (iTime>aTime.Int64())
sl@0
   802
		{
sl@0
   803
		return TInt(TUint64(iTime-aTime.Int64())/KDaysToMicroSeconds);	
sl@0
   804
		}
sl@0
   805
	else 
sl@0
   806
		{
sl@0
   807
		return -TInt(TUint64(aTime.Int64()-iTime)/KDaysToMicroSeconds);
sl@0
   808
		}	
sl@0
   809
	}
sl@0
   810
sl@0
   811
EXPORT_C TTimeIntervalMonths TTime::MonthsFrom(TTime aTime) const
sl@0
   812
//
sl@0
   813
// iTime - aTime as whole months - ie aTime must be on a later day in the month and later in that day
sl@0
   814
// except for last days etc eg 31st October - 30 November is one month to be consistent with other
sl@0
   815
// functions
sl@0
   816
//
sl@0
   817
/**
sl@0
   818
Calculates the number of months between the specified TTime and this TTime.
sl@0
   819
sl@0
   820
The result may be positive or negative.
sl@0
   821
sl@0
   822
The interval in months between two TTimes is calculated by incrementing it 
sl@0
   823
by one each time the same day number and time in the previous or following 
sl@0
   824
month has been reached. Exceptions to this rule occur when this TTime is on 
sl@0
   825
the last day of the month. In this case, the following rules apply:
sl@0
   826
sl@0
   827
When comparing this TTime with a later time:
sl@0
   828
sl@0
   829
1. if the following month is shorter, one month is deemed to separate the times 
sl@0
   830
   when the same time on the last day of the following month is reached. In this 
sl@0
   831
   case, the two day numbers are not the same.
sl@0
   832
sl@0
   833
When comparing this TTime with an earlier time:
sl@0
   834
sl@0
   835
1. if the previous month is shorter, one month is deemed to separate the times 
sl@0
   836
   when the last microsecond of the previous month is reached (23:59:59.999999 
sl@0
   837
   on the last day of the month).
sl@0
   838
sl@0
   839
2. if the previous month is longer, one month is deemed to separate the times 
sl@0
   840
   when the same time on the last day of previous month is reached. In this case, 
sl@0
   841
   the two day numbers are not the same.
sl@0
   842
sl@0
   843
@param aTime The time to be compared with this TTime.
sl@0
   844
sl@0
   845
@return Difference in months between the two times. If the time specified in 
sl@0
   846
        the argument is later than this TTime, the interval is negative.
sl@0
   847
*/
sl@0
   848
	{
sl@0
   849
sl@0
   850
	TDateTime dateTimei=DateTime();
sl@0
   851
	TDateTime dateTimea=aTime.DateTime();
sl@0
   852
	
sl@0
   853
	TInt monthsDifference=(dateTimei.Year()-dateTimea.Year())*12+(dateTimei.Month()-dateTimea.Month());
sl@0
   854
sl@0
   855
	if (monthsDifference>0)
sl@0
   856
		{
sl@0
   857
		if (dateTimei.Day()<=dateTimea.Day())
sl@0
   858
			{
sl@0
   859
			if (iTime%KDaysToMicroSeconds<aTime.Int64()%KDaysToMicroSeconds || (dateTimei.Day()!=dateTimea.Day() && dateTimei.Day()!=DaysInMonth()-1))
sl@0
   860
				monthsDifference--;
sl@0
   861
			}
sl@0
   862
		}
sl@0
   863
	else
sl@0
   864
		if (monthsDifference!=0)//monthsDifference<0
sl@0
   865
			{
sl@0
   866
			if (dateTimei.Day()>=dateTimea.Day())
sl@0
   867
				{
sl@0
   868
				if (iTime%KDaysToMicroSeconds>aTime.Int64()%KDaysToMicroSeconds || (dateTimei.Day()!=dateTimea.Day() && dateTimea.Day()!=aTime.DaysInMonth()-1))
sl@0
   869
					monthsDifference++;
sl@0
   870
				}
sl@0
   871
			}
sl@0
   872
sl@0
   873
	return(monthsDifference);			
sl@0
   874
	}
sl@0
   875
sl@0
   876
EXPORT_C TTimeIntervalYears TTime::YearsFrom(TTime aTime) const
sl@0
   877
//
sl@0
   878
// as above,but for twelve months
sl@0
   879
//
sl@0
   880
/**
sl@0
   881
Calculates the number of years between the specified TTime and this TTime.
sl@0
   882
sl@0
   883
The result may be positive or negative.
sl@0
   884
sl@0
   885
Note that the interval in years between two TTimes is calculated by
sl@0
   886
incrementing it by one each time the same day number and time in the previous
sl@0
   887
or following year has been reached. The exception to this rule occurs when this
sl@0
   888
TTime is the last day in February in a leap year. In this case, one year is
sl@0
   889
deemed to have passed when the same time of day on the last day in the preceding 
sl@0
   890
or following February has been reached.
sl@0
   891
sl@0
   892
@param aTime The time to be compared with this TTime.
sl@0
   893
sl@0
   894
@return Difference in years between the two times. If the time specified in 
sl@0
   895
        the argument is later than this TTime, the interval is negative.
sl@0
   896
*/
sl@0
   897
	{
sl@0
   898
sl@0
   899
	TTimeIntervalMonths mos= TTime::MonthsFrom(aTime);
sl@0
   900
	TTimeIntervalYears ret=mos.Int()/12;
sl@0
   901
	return(ret);			
sl@0
   902
	}
sl@0
   903
sl@0
   904
EXPORT_C TTime TTime::operator+(TTimeIntervalYears aYear) const
sl@0
   905
/**
sl@0
   906
Adds a time interval to this TTime, returning the result
sl@0
   907
as a TTime.
sl@0
   908
sl@0
   909
Note that in a leap year, when adding one year to the 29th February, the result
sl@0
   910
is the 28th February in the following year.
sl@0
   911
sl@0
   912
Note also that this TTime object is not changed.
sl@0
   913
sl@0
   914
@param aYear A time interval in years. The argument is stored as a 32 bit
sl@0
   915
             signed integer. The maximum value which it can represent is
sl@0
   916
             2147483647. Any attempt to add more than this amount will
sl@0
   917
             produce incorrect results.
sl@0
   918
sl@0
   919
@return The new time.
sl@0
   920
*/
sl@0
   921
	{
sl@0
   922
sl@0
   923
	return((*this)+TTimeIntervalMonths(aYear.Int()*12));
sl@0
   924
	}
sl@0
   925
sl@0
   926
EXPORT_C TTime TTime::operator+(TTimeIntervalMonths aMonth) const
sl@0
   927
/**
sl@0
   928
Adds a time interval to this TTime, returning the result
sl@0
   929
as a TTime.
sl@0
   930
sl@0
   931
Note that when adding one month to the last day in the month, if the following
sl@0
   932
month is shorter, the result is the last day in the following month.
sl@0
   933
For example, when adding one month to 31st August, the result is
sl@0
   934
the 30th September.
sl@0
   935
sl@0
   936
Note also that this TTime object is not changed.
sl@0
   937
sl@0
   938
@param aMonth A time interval in months. The argument is stored as a 32 bit
sl@0
   939
              signed integer. The maximum value which it can represent is
sl@0
   940
              2147483647. Any attempt to add more than this amount will
sl@0
   941
              produce incorrect results.
sl@0
   942
sl@0
   943
@return The new time.
sl@0
   944
*/
sl@0
   945
	{
sl@0
   946
sl@0
   947
	TDateTime dateTime=DateTime();
sl@0
   948
	TInt month=dateTime.Month()+(dateTime.Year()*12)+aMonth.Int();
sl@0
   949
	TInt day=dateTime.Day();
sl@0
   950
	TInt year=month/12;
sl@0
   951
	month%=12;
sl@0
   952
	if (month<0)
sl@0
   953
		{
sl@0
   954
		year--;
sl@0
   955
		month+=12;
sl@0
   956
		}
sl@0
   957
	TInt daysInMonth=(mTab[Time::IsLeapYear(year)][month]-1); 
sl@0
   958
	if (day>=daysInMonth)
sl@0
   959
		day=daysInMonth;
sl@0
   960
	__ASSERT_ALWAYS(dateTime.Set(year,TMonth(month),day,dateTime.Hour(),dateTime.Minute(),dateTime.Second(),dateTime.MicroSecond())==KErrNone,Panic(ETDateTimeBadDateTime));
sl@0
   961
	return(dateTime);
sl@0
   962
	}
sl@0
   963
							 
sl@0
   964
EXPORT_C TTime TTime::operator+(TTimeIntervalDays aDay) const
sl@0
   965
/**
sl@0
   966
Adds a time interval to this TTime, returning the result
sl@0
   967
as a TTime.
sl@0
   968
sl@0
   969
Note that this TTime object is not changed.
sl@0
   970
sl@0
   971
@param aDay A time interval in days. The argument is stored as a 32 bit
sl@0
   972
            signed integer. The maximum value which it can represent is
sl@0
   973
            2147483647. Any attempt to add more than this amount will
sl@0
   974
            produce incorrect results.
sl@0
   975
sl@0
   976
@return The new time.
sl@0
   977
*/
sl@0
   978
	{ 
sl@0
   979
sl@0
   980
	return(iTime+TInt64(aDay.Int())*KDaysToMicroSeconds);
sl@0
   981
	}
sl@0
   982
sl@0
   983
EXPORT_C TTime TTime::operator+(TTimeIntervalHours aHour) const
sl@0
   984
/**
sl@0
   985
Adds a time interval to this TTime, returning the result
sl@0
   986
as a TTime.
sl@0
   987
sl@0
   988
Note that this TTime object is not changed.
sl@0
   989
sl@0
   990
@param aHour A time interval in hours. The argument is stored as a 32 bit
sl@0
   991
             signed integer. The maximum value which it can represent is
sl@0
   992
             2147483647. Any attempt to add more than this amount will
sl@0
   993
             produce incorrect results.
sl@0
   994
sl@0
   995
@return The new time.
sl@0
   996
*/
sl@0
   997
	{
sl@0
   998
sl@0
   999
	return(iTime+TInt64(aHour.Int())*KHoursToMicroSeconds);
sl@0
  1000
	}
sl@0
  1001
sl@0
  1002
EXPORT_C TTime TTime::operator+(TTimeIntervalMinutes aMinute) const
sl@0
  1003
/**
sl@0
  1004
Adds a time interval to this TTime, returning the result
sl@0
  1005
as a TTime.
sl@0
  1006
sl@0
  1007
Note that this TTime object is not changed.
sl@0
  1008
sl@0
  1009
@param aMinute A time interval in minutes. The argument is stored as a 32 bit
sl@0
  1010
               signed integer. The maximum value which it can represent is
sl@0
  1011
               2147483647. Any attempt to add more than this amount will
sl@0
  1012
               produce incorrect results.
sl@0
  1013
sl@0
  1014
@return The new time.
sl@0
  1015
*/
sl@0
  1016
	{
sl@0
  1017
sl@0
  1018
	return(iTime+TInt64(aMinute.Int())*KMinutesToMicroSeconds);
sl@0
  1019
	}
sl@0
  1020
sl@0
  1021
EXPORT_C TTime TTime::operator+(TTimeIntervalSeconds aSecond) const
sl@0
  1022
/**
sl@0
  1023
Adds a time interval to this TTime, returning the result
sl@0
  1024
as a TTime.
sl@0
  1025
sl@0
  1026
Note that this TTime object is not changed.
sl@0
  1027
sl@0
  1028
@param aSecond A time interval in seconds. The argument is stored as a 32 bit
sl@0
  1029
               signed integer. The maximum value which it can represent is
sl@0
  1030
               2147483647. Any attempt to add more than this amount will
sl@0
  1031
               produce incorrect results.
sl@0
  1032
sl@0
  1033
@return The new time.
sl@0
  1034
*/
sl@0
  1035
	{
sl@0
  1036
sl@0
  1037
	return(iTime+TInt64(aSecond.Int())*KSecondsToMicroSeconds);
sl@0
  1038
	}
sl@0
  1039
sl@0
  1040
 
sl@0
  1041
EXPORT_C TTime TTime::operator+(TTimeIntervalMicroSeconds aMicroSecond) const
sl@0
  1042
/**
sl@0
  1043
Adds a time interval to this TTime, returning the result
sl@0
  1044
as a TTime.
sl@0
  1045
sl@0
  1046
Note that this TTime object is not changed.
sl@0
  1047
sl@0
  1048
@param aMicroSecond A time interval in microseconds.
sl@0
  1049
sl@0
  1050
@return The new time.
sl@0
  1051
*/
sl@0
  1052
	{
sl@0
  1053
sl@0
  1054
	return(iTime+(aMicroSecond.Int64()));
sl@0
  1055
	}
sl@0
  1056
sl@0
  1057
EXPORT_C TTime TTime::operator+(TTimeIntervalMicroSeconds32 aMicroSecond) const
sl@0
  1058
/**
sl@0
  1059
Adds a time interval to this TTime, returning the result
sl@0
  1060
as a TTime.
sl@0
  1061
sl@0
  1062
Note that this TTime object is not changed.
sl@0
  1063
sl@0
  1064
@param aMicroSecond A time interval in microseconds. The argument is stored as
sl@0
  1065
                    a 32 bit signed integer. The maximum value which it can
sl@0
  1066
                    represent is 2147483647. Any attempt to add more than this
sl@0
  1067
                    amount will produce incorrect results.
sl@0
  1068
sl@0
  1069
@return The new time.
sl@0
  1070
*/
sl@0
  1071
	{
sl@0
  1072
sl@0
  1073
	return(iTime+aMicroSecond.Int());
sl@0
  1074
	}
sl@0
  1075
sl@0
  1076
EXPORT_C TTime TTime::operator-(TTimeIntervalYears aYear) const
sl@0
  1077
/**
sl@0
  1078
Substracts a time interval from this TTime, returning the result
sl@0
  1079
as a TTime.
sl@0
  1080
sl@0
  1081
Note that in a leap year, when subtracting one year from the 29th February,
sl@0
  1082
the result is 28th February in the preceding year.
sl@0
  1083
sl@0
  1084
Note also that this TTime object is not changed.
sl@0
  1085
sl@0
  1086
@param aYear A time interval in years. The argument is stored as
sl@0
  1087
             a 32 bit signed integer. The maximum value which it can
sl@0
  1088
             represent is 2147483647. Any attempt to subtract more than this
sl@0
  1089
             amount will produce incorrect results.
sl@0
  1090
sl@0
  1091
@return The new time.
sl@0
  1092
*/
sl@0
  1093
	{
sl@0
  1094
sl@0
  1095
	return((*this)-TTimeIntervalMonths(aYear.Int()*12));
sl@0
  1096
	}
sl@0
  1097
sl@0
  1098
EXPORT_C TTime TTime::operator-(TTimeIntervalMonths aMonth) const
sl@0
  1099
/**
sl@0
  1100
Substracts a time interval from this TTime, returning the result
sl@0
  1101
as a TTime.
sl@0
  1102
sl@0
  1103
Note that when subtracting one month from the last day in the month, if the
sl@0
  1104
preceding month is shorter, the result is the last day in the preceding month.
sl@0
  1105
For example, when subtracting 1 month from 31st October, the result is
sl@0
  1106
the 30th September.
sl@0
  1107
sl@0
  1108
Note also that this TTime object is not changed.
sl@0
  1109
sl@0
  1110
@param aMonth A time interval in months. The argument is stored as
sl@0
  1111
              a 32 bit signed integer. The maximum value which it can
sl@0
  1112
              represent is 2147483647. Any attempt to subtract more than this
sl@0
  1113
              amount will produce incorrect results.
sl@0
  1114
sl@0
  1115
@return The new time.
sl@0
  1116
*/
sl@0
  1117
	{
sl@0
  1118
sl@0
  1119
	return((*this)+TTimeIntervalMonths(aMonth.Int()*-1));
sl@0
  1120
	}
sl@0
  1121
							 
sl@0
  1122
EXPORT_C TTime TTime::operator-(TTimeIntervalDays aDay) const
sl@0
  1123
/**
sl@0
  1124
Substracts a time interval from this TTime, returning the result
sl@0
  1125
as a TTime.
sl@0
  1126
sl@0
  1127
Note that this TTime object is not changed.
sl@0
  1128
sl@0
  1129
@param aDay A time interval in days. The argument is stored as
sl@0
  1130
            a 32 bit signed integer. The maximum value which it can
sl@0
  1131
            represent is 2147483647. Any attempt to subtract more than this
sl@0
  1132
            amount will produce incorrect results.
sl@0
  1133
sl@0
  1134
@return The new time.
sl@0
  1135
*/
sl@0
  1136
	{ 
sl@0
  1137
sl@0
  1138
	return(iTime-TInt64(aDay.Int())*KDaysToMicroSeconds);
sl@0
  1139
	}
sl@0
  1140
sl@0
  1141
EXPORT_C TTime TTime::operator-(TTimeIntervalHours aHour) const
sl@0
  1142
/**
sl@0
  1143
Substracts a time interval from this TTime, returning the result
sl@0
  1144
as a TTime.
sl@0
  1145
sl@0
  1146
Note that this TTime object is not changed.
sl@0
  1147
sl@0
  1148
@param aHour A time interval in hours. The argument is stored as
sl@0
  1149
             a 32 bit signed integer. The maximum value which it can
sl@0
  1150
             represent is 2147483647. Any attempt to subtract more than this
sl@0
  1151
             amount will produce incorrect results.
sl@0
  1152
sl@0
  1153
@return The new time.
sl@0
  1154
*/
sl@0
  1155
	{
sl@0
  1156
sl@0
  1157
	return(iTime-TInt64(aHour.Int())*KHoursToMicroSeconds);
sl@0
  1158
	}
sl@0
  1159
sl@0
  1160
EXPORT_C TTime TTime::operator-(TTimeIntervalMinutes aMinute) const
sl@0
  1161
/**
sl@0
  1162
Substracts a time interval from this TTime, returning the result
sl@0
  1163
as a TTime.
sl@0
  1164
sl@0
  1165
Note that this TTime object is not changed.
sl@0
  1166
sl@0
  1167
@param aMinute A time interval in minutes. The argument is stored as
sl@0
  1168
               a 32 bit signed integer. The maximum value which it can
sl@0
  1169
               represent is 2147483647. Any attempt to subtract more than this
sl@0
  1170
               amount will produce incorrect results.
sl@0
  1171
sl@0
  1172
@return The new time.
sl@0
  1173
*/
sl@0
  1174
	{
sl@0
  1175
sl@0
  1176
	return(iTime-TInt64(aMinute.Int())*KMinutesToMicroSeconds);
sl@0
  1177
	}
sl@0
  1178
sl@0
  1179
EXPORT_C TTime TTime::operator-(TTimeIntervalSeconds aSecond) const
sl@0
  1180
/**
sl@0
  1181
Substracts a time interval from this TTime, returning the result
sl@0
  1182
as a TTime.
sl@0
  1183
sl@0
  1184
Note that this TTime object is not changed.
sl@0
  1185
sl@0
  1186
@param aSecond A time interval in seconds. The argument is stored as
sl@0
  1187
               a 32 bit signed integer. The maximum value which it can
sl@0
  1188
               represent is 2147483647. Any attempt to subtract more than this
sl@0
  1189
               amount will produce incorrect results.
sl@0
  1190
sl@0
  1191
@return The new time.
sl@0
  1192
*/
sl@0
  1193
	{
sl@0
  1194
sl@0
  1195
	return(iTime-TInt64(aSecond.Int())*KSecondsToMicroSeconds);
sl@0
  1196
	}
sl@0
  1197
sl@0
  1198
EXPORT_C TTime TTime::operator-(TTimeIntervalMicroSeconds aMicroSecond) const
sl@0
  1199
/**
sl@0
  1200
Substracts a time interval from this TTime, returning the result
sl@0
  1201
as a TTime.
sl@0
  1202
sl@0
  1203
Note that this TTime object is not changed.
sl@0
  1204
sl@0
  1205
@param aMicroSecond A time interval in microseconds.
sl@0
  1206
sl@0
  1207
@return The new time.
sl@0
  1208
*/
sl@0
  1209
	{
sl@0
  1210
sl@0
  1211
	return(iTime-(aMicroSecond.Int64()));
sl@0
  1212
	}
sl@0
  1213
sl@0
  1214
EXPORT_C TTime TTime::operator-(TTimeIntervalMicroSeconds32 aMicroSecond) const
sl@0
  1215
/**
sl@0
  1216
Substracts a time interval from this TTime, returning the result
sl@0
  1217
as a TTime.
sl@0
  1218
sl@0
  1219
Note that this TTime object is not changed.
sl@0
  1220
sl@0
  1221
@param aMicroSecond A time interval in microseconds. The argument is stored as
sl@0
  1222
                    a 32 bit signed integer. The maximum value which it can
sl@0
  1223
                    represent is 2147483647. Any attempt to subtract more than
sl@0
  1224
                    this amount will produce incorrect results.
sl@0
  1225
sl@0
  1226
@return The new time.
sl@0
  1227
*/
sl@0
  1228
	{
sl@0
  1229
sl@0
  1230
	return(iTime-aMicroSecond.Int());
sl@0
  1231
	}
sl@0
  1232
sl@0
  1233
EXPORT_C TTime &TTime::operator+=(TTimeIntervalYears aYear)
sl@0
  1234
/**
sl@0
  1235
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1236
sl@0
  1237
@param aYear A time interval in years.
sl@0
  1238
sl@0
  1239
@return A reference to this TTime.
sl@0
  1240
*/
sl@0
  1241
	{	
sl@0
  1242
sl@0
  1243
	TTime tim=(*this)+aYear;
sl@0
  1244
	iTime=tim.Int64();
sl@0
  1245
	return(*this);
sl@0
  1246
	}
sl@0
  1247
sl@0
  1248
EXPORT_C TTime &TTime::operator+=(TTimeIntervalMonths aMonth)
sl@0
  1249
/**
sl@0
  1250
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1251
sl@0
  1252
@param aMonth A time interval in months.
sl@0
  1253
sl@0
  1254
@return A reference to this TTime.
sl@0
  1255
*/
sl@0
  1256
	{
sl@0
  1257
sl@0
  1258
	TTime tim=(*this)+aMonth;
sl@0
  1259
	iTime=tim.Int64();
sl@0
  1260
	return(*this);
sl@0
  1261
	}
sl@0
  1262
sl@0
  1263
EXPORT_C TTime &TTime::operator+=(TTimeIntervalDays aDay)
sl@0
  1264
/**
sl@0
  1265
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1266
sl@0
  1267
@param aDay A time interval in days.
sl@0
  1268
sl@0
  1269
@return A reference to this TTime.
sl@0
  1270
*/
sl@0
  1271
	{
sl@0
  1272
sl@0
  1273
	iTime+=TInt64(aDay.Int())*KDaysToMicroSeconds;
sl@0
  1274
	return(*this);
sl@0
  1275
	}
sl@0
  1276
sl@0
  1277
EXPORT_C TTime &TTime::operator+=(TTimeIntervalHours aHour)
sl@0
  1278
/**
sl@0
  1279
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1280
sl@0
  1281
@param aHour A time interval in hours.
sl@0
  1282
sl@0
  1283
@return A reference to this TTime.
sl@0
  1284
*/
sl@0
  1285
	{
sl@0
  1286
sl@0
  1287
	iTime+=TInt64(aHour.Int())*KHoursToMicroSeconds;
sl@0
  1288
	return(*this);
sl@0
  1289
	}
sl@0
  1290
sl@0
  1291
EXPORT_C TTime &TTime::operator+=(TTimeIntervalMinutes aMinute)
sl@0
  1292
/**
sl@0
  1293
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1294
sl@0
  1295
@param aMinute A time interval in minutes.
sl@0
  1296
sl@0
  1297
@return A reference to this TTime.
sl@0
  1298
*/
sl@0
  1299
	{
sl@0
  1300
sl@0
  1301
	iTime+=TInt64(aMinute.Int())*KMinutesToMicroSeconds;
sl@0
  1302
	return(*this);
sl@0
  1303
	}
sl@0
  1304
sl@0
  1305
EXPORT_C TTime &TTime::operator+=(TTimeIntervalSeconds aSecond)
sl@0
  1306
/**
sl@0
  1307
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1308
sl@0
  1309
@param aSecond A time interval in seconds.
sl@0
  1310
sl@0
  1311
@return A reference to this TTime.
sl@0
  1312
*/
sl@0
  1313
	{
sl@0
  1314
sl@0
  1315
	iTime+=TInt64(aSecond.Int())*KSecondsToMicroSeconds;
sl@0
  1316
	return(*this);
sl@0
  1317
	}
sl@0
  1318
sl@0
  1319
EXPORT_C TTime &TTime::operator+=(TTimeIntervalMicroSeconds aMicroSecond)
sl@0
  1320
/**
sl@0
  1321
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1322
sl@0
  1323
@param aMicroSecond A time interval in microseconds.
sl@0
  1324
sl@0
  1325
@return A reference to this TTime.
sl@0
  1326
*/
sl@0
  1327
	{
sl@0
  1328
sl@0
  1329
	iTime+=aMicroSecond.Int64();
sl@0
  1330
	return(*this);
sl@0
  1331
	}
sl@0
  1332
 
sl@0
  1333
EXPORT_C TTime &TTime::operator+=(TTimeIntervalMicroSeconds32 aMicroSecond)
sl@0
  1334
/**
sl@0
  1335
Adds a time interval to this TTime, returning a reference to this TTime.
sl@0
  1336
sl@0
  1337
@param aMicroSecond A time interval in microseconds, as a 32-bit integer.
sl@0
  1338
sl@0
  1339
@return A reference to this TTime.
sl@0
  1340
*/
sl@0
  1341
	{
sl@0
  1342
sl@0
  1343
	iTime+=aMicroSecond.Int();
sl@0
  1344
	return(*this);
sl@0
  1345
	}
sl@0
  1346
 
sl@0
  1347
EXPORT_C TTime &TTime::operator-=(TTimeIntervalYears aYear)
sl@0
  1348
/**
sl@0
  1349
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1350
sl@0
  1351
@param aYear A time interval in years.
sl@0
  1352
sl@0
  1353
@return A reference to this TTime.
sl@0
  1354
*/
sl@0
  1355
	{	
sl@0
  1356
sl@0
  1357
	TTime tim=(*this)-aYear;
sl@0
  1358
	iTime=tim.Int64();
sl@0
  1359
	return(*this);
sl@0
  1360
	}
sl@0
  1361
sl@0
  1362
EXPORT_C TTime &TTime::operator-=(TTimeIntervalMonths aMonth)
sl@0
  1363
/**
sl@0
  1364
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1365
sl@0
  1366
@param aMonth A time interval in months.
sl@0
  1367
sl@0
  1368
@return A reference to this TTime.
sl@0
  1369
*/
sl@0
  1370
	{
sl@0
  1371
sl@0
  1372
	TTime tim=(*this)-aMonth;
sl@0
  1373
	iTime=tim.Int64();
sl@0
  1374
	return(*this);
sl@0
  1375
	}
sl@0
  1376
sl@0
  1377
EXPORT_C TTime &TTime::operator-=(TTimeIntervalDays aDay)
sl@0
  1378
/**
sl@0
  1379
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1380
sl@0
  1381
@param aDay A time interval in days.
sl@0
  1382
sl@0
  1383
@return A reference to this TTime.
sl@0
  1384
*/
sl@0
  1385
	{
sl@0
  1386
sl@0
  1387
	iTime-=TInt64(aDay.Int())*KDaysToMicroSeconds;
sl@0
  1388
	return(*this);
sl@0
  1389
	}
sl@0
  1390
sl@0
  1391
EXPORT_C TTime &TTime::operator-=(TTimeIntervalHours aHour)
sl@0
  1392
/**
sl@0
  1393
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1394
sl@0
  1395
@param aHour A time interval in hours.
sl@0
  1396
sl@0
  1397
@return A reference to this TTime.
sl@0
  1398
*/
sl@0
  1399
	{
sl@0
  1400
sl@0
  1401
	iTime-=TInt64(aHour.Int())*KHoursToMicroSeconds;
sl@0
  1402
	return(*this);
sl@0
  1403
	}
sl@0
  1404
sl@0
  1405
EXPORT_C TTime &TTime::operator-=(TTimeIntervalMinutes aMinute)
sl@0
  1406
/**
sl@0
  1407
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1408
sl@0
  1409
@param aMinute A time interval in minutes.
sl@0
  1410
sl@0
  1411
@return A reference to this TTime.
sl@0
  1412
*/
sl@0
  1413
	{
sl@0
  1414
sl@0
  1415
	iTime-=TInt64(aMinute.Int())*KMinutesToMicroSeconds;
sl@0
  1416
	return(*this);
sl@0
  1417
	}
sl@0
  1418
sl@0
  1419
EXPORT_C TTime &TTime::operator-=(TTimeIntervalSeconds aSecond)
sl@0
  1420
/**
sl@0
  1421
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1422
sl@0
  1423
@param aSecond A time interval in seconds.
sl@0
  1424
sl@0
  1425
@return A reference to this TTime.
sl@0
  1426
*/
sl@0
  1427
	{
sl@0
  1428
sl@0
  1429
	iTime-=TInt64(aSecond.Int())*KSecondsToMicroSeconds;
sl@0
  1430
	return(*this);
sl@0
  1431
	}
sl@0
  1432
sl@0
  1433
EXPORT_C TTime &TTime::operator-=(TTimeIntervalMicroSeconds aMicroSecond)
sl@0
  1434
/**
sl@0
  1435
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1436
sl@0
  1437
@param aMicroSecond A time interval in microseconds.
sl@0
  1438
sl@0
  1439
@return A reference to this TTime.
sl@0
  1440
*/
sl@0
  1441
	{
sl@0
  1442
sl@0
  1443
	iTime-=aMicroSecond.Int64();
sl@0
  1444
	return(*this);
sl@0
  1445
	}
sl@0
  1446
sl@0
  1447
EXPORT_C TTime &TTime::operator-=(TTimeIntervalMicroSeconds32 aMicroSecond)
sl@0
  1448
/**
sl@0
  1449
Subtracts a time interval from this TTime, returning a reference to this TTime.
sl@0
  1450
sl@0
  1451
@param aMicroSecond A time interval in microseconds, as a 32-bit integer.
sl@0
  1452
sl@0
  1453
@return A reference to this TTime.
sl@0
  1454
*/
sl@0
  1455
	{
sl@0
  1456
sl@0
  1457
	iTime-=aMicroSecond.Int();
sl@0
  1458
	return(*this);
sl@0
  1459
	}
sl@0
  1460
 
sl@0
  1461
EXPORT_C TInt TTime::DaysInMonth() const
sl@0
  1462
/**
sl@0
  1463
Gets the number of days in the current month.
sl@0
  1464
sl@0
  1465
@return The number of days in the month.
sl@0
  1466
*/
sl@0
  1467
	{
sl@0
  1468
sl@0
  1469
	TDateTime dateTime=DateTime();
sl@0
  1470
	return(Time::DaysInMonth(dateTime.Year(),dateTime.Month()));
sl@0
  1471
	}
sl@0
  1472
sl@0
  1473
EXPORT_C TDay TTime::DayNoInWeek() const
sl@0
  1474
//
sl@0
  1475
// 1st January 0AD was a Monday
sl@0
  1476
//
sl@0
  1477
/**
sl@0
  1478
Gets the day number within the current week.
sl@0
  1479
sl@0
  1480
This is a value in the range zero to six inclusive, and honours the 
sl@0
  1481
setting specified in TLocale::SetStartOfWeek().
sl@0
  1482
sl@0
  1483
By default the first day in the week is Monday.
sl@0
  1484
sl@0
  1485
@return The number of the day within the week. The range is EMonday to ESunday.
sl@0
  1486
sl@0
  1487
@see TLocale::SetStartOfWeek
sl@0
  1488
*/
sl@0
  1489
	{
sl@0
  1490
sl@0
  1491
sl@0
  1492
	TInt64 fullDays=iTime/KDaysToMicroSeconds;
sl@0
  1493
	TInt day = static_cast<TInt>(fullDays) % 7;
sl@0
  1494
	if (iTime<0)
sl@0
  1495
		{
sl@0
  1496
		if (fullDays*KDaysToMicroSeconds!=iTime)
sl@0
  1497
			day+=6;
sl@0
  1498
		else
sl@0
  1499
			if (day!=0)
sl@0
  1500
				day+=7;
sl@0
  1501
		}
sl@0
  1502
	return((TDay)day);
sl@0
  1503
	}
sl@0
  1504
sl@0
  1505
EXPORT_C TInt TTime::DayNoInMonth() const
sl@0
  1506
/**
sl@0
  1507
Gets the day number in the month.
sl@0
  1508
sl@0
  1509
@return The day number in the month. The first day in the month is numbered 
sl@0
  1510
        zero.
sl@0
  1511
*/
sl@0
  1512
	{
sl@0
  1513
sl@0
  1514
	return(DateTime().Day());
sl@0
  1515
	}
sl@0
  1516
sl@0
  1517
EXPORT_C TInt TTime::DayNoInYear() const
sl@0
  1518
//
sl@0
  1519
// day number in comparison to 1st January
sl@0
  1520
//
sl@0
  1521
/**
sl@0
  1522
Gets the day number in the year. 
sl@0
  1523
sl@0
  1524
@return The day number in the year. The first day in the year is day one.
sl@0
  1525
*/
sl@0
  1526
	{
sl@0
  1527
sl@0
  1528
	TDateTime dateTime=DateTime();
sl@0
  1529
	TTime jan1st=TDateTime(dateTime.Year(),EJanuary,0,0,0,0,0);
sl@0
  1530
	return(DayNoInYear(jan1st));
sl@0
  1531
	}
sl@0
  1532
sl@0
  1533
EXPORT_C TInt TTime::DayNoInYear(TTime aStartDate) const
sl@0
  1534
//
sl@0
  1535
// day number in comparison to given date, check is made to ensure first day is within a year before aDay
sl@0
  1536
//
sl@0
  1537
/**
sl@0
  1538
Gets the day number in the year when the start of the year is aStartDate. 
sl@0
  1539
sl@0
  1540
If no start date is specified, the default is January 1st.
sl@0
  1541
sl@0
  1542
@param aStartDate Indicates the date which is to be considered the start of 
sl@0
  1543
                  the year. Default is 1st January.
sl@0
  1544
                  
sl@0
  1545
@return The day number in the year. The first day in the year is day one.
sl@0
  1546
*/
sl@0
  1547
	{
sl@0
  1548
sl@0
  1549
	TInt y=DateTime().Year();
sl@0
  1550
	TMonth m=aStartDate.DateTime().Month();
sl@0
  1551
	TInt d=aStartDate.DateTime().Day();
sl@0
  1552
    if (d>=Time::DaysInMonth(y,m))
sl@0
  1553
        d=27;
sl@0
  1554
    TDateTime yearStart(y,m,d,0,0,0,0);              // LEAP YEAR PROBLEMS ???
sl@0
  1555
	aStartDate=yearStart;
sl@0
  1556
	if (aStartDate>*this)
sl@0
  1557
		{
sl@0
  1558
		yearStart.SetYearLeapCheck(y-1);
sl@0
  1559
		aStartDate=yearStart;
sl@0
  1560
		}
sl@0
  1561
    return((DaysFrom(aStartDate).Int())+1) ;
sl@0
  1562
    }
sl@0
  1563
sl@0
  1564
EXPORT_C TInt TTime::WeekNoInYear() const
sl@0
  1565
/**
sl@0
  1566
Gets the number of the current week in the year.
sl@0
  1567
sl@0
  1568
@return Week number in the year.
sl@0
  1569
*/
sl@0
  1570
	{
sl@0
  1571
sl@0
  1572
	return(WeekNoInYear(EFirstFourDayWeek));
sl@0
  1573
	}
sl@0
  1574
sl@0
  1575
EXPORT_C TInt TTime::WeekNoInYear(TTime aStartDate) const
sl@0
  1576
/**
sl@0
  1577
Gets the number of the current week in the year when the year starts
sl@0
  1578
on aStartDate. 
sl@0
  1579
sl@0
  1580
@param aStartDate If specified, indicates the date which is to be considered 
sl@0
  1581
                  the start of the year. Default is 1st January.
sl@0
  1582
                  
sl@0
  1583
@return Week number in the year.
sl@0
  1584
*/
sl@0
  1585
	{
sl@0
  1586
    
sl@0
  1587
    return(WeekNoInYear(aStartDate,EFirstFourDayWeek));
sl@0
  1588
	}
sl@0
  1589
sl@0
  1590
EXPORT_C TInt TTime::WeekNoInYear(TFirstWeekRule aRule) const
sl@0
  1591
/**
sl@0
  1592
Finds the number of the current week in the year using the first week rule 
sl@0
  1593
specified in aRule. 
sl@0
  1594
sl@0
  1595
@param aRule Determines how the first week in the year is to be calculated. 
sl@0
  1596
             By default EFirstFourDayWeek.
sl@0
  1597
             
sl@0
  1598
@return Week number in the year.
sl@0
  1599
*/
sl@0
  1600
	{
sl@0
  1601
	
sl@0
  1602
	TInt year=DateTime().Year();
sl@0
  1603
	TTime startDate=TDateTime(year,EJanuary,0,0,0,0,0);
sl@0
  1604
	return(WeekNoInYear(startDate,aRule));
sl@0
  1605
	}
sl@0
  1606
sl@0
  1607
EXPORT_C TInt TTime::WeekNoInYear(TTime aStartDate,TFirstWeekRule aRule) const
sl@0
  1608
//
sl@0
  1609
// number of weeks between aTime and aStartDate according to given rule
sl@0
  1610
// the first week starts either on the week containing the first day (EFirstWeek), 
sl@0
  1611
// the first week having at least four days within the new year (EFirstFourDayWeek,
sl@0
  1612
//  default) or the first full week in the year (EFirstFullWeek)
sl@0
  1613
//
sl@0
  1614
/**
sl@0
  1615
Finds the number of the current week in the year when the year starts from 
sl@0
  1616
aStartDate and when using the start week rule aRule.
sl@0
  1617
sl@0
  1618
@param aStartDate If specified, indicates the date which is to be considered 
sl@0
  1619
                  the start of the year. Default is 1st January.
sl@0
  1620
@param aRule      Determines how the first week in the year is to be
sl@0
  1621
                  calculated. By default EFirstFourDayWeek.
sl@0
  1622
                  
sl@0
  1623
@return Week number in the year.
sl@0
  1624
*/
sl@0
  1625
	{                    
sl@0
  1626
	TInt dayNoInWeek=DayNoInWeek();
sl@0
  1627
	TInt dayNoInYear=(DayNoInYear(aStartDate))-1;    // puts start into correct year
sl@0
  1628
	TDateTime startDateTime(aStartDate.DateTime());
sl@0
  1629
	TDateTime nextYearStartDate(startDateTime);
sl@0
  1630
	nextYearStartDate.SetYearLeapCheck(DateTime().Year());    // find start of next year
sl@0
  1631
	TTime nextYearStartTime(nextYearStartDate);            // makes sure start date for year
sl@0
  1632
	if (*this>nextYearStartTime)                           // is in the very next year
sl@0
  1633
		{
sl@0
  1634
		nextYearStartDate.SetYearLeapCheck(nextYearStartDate.Year()+1);
sl@0
  1635
		nextYearStartTime=nextYearStartDate;
sl@0
  1636
		}
sl@0
  1637
	nextYearStartTime+=TTimeIntervalMicroSeconds(KDaysToMicroSeconds-1); // avoid problems if the time is not midnight
sl@0
  1638
	TLocale local;
sl@0
  1639
	TDay startOfFirstWeek=local.StartOfWeek();
sl@0
  1640
	// calculate the day-in-week number (0 to 6) based on the locale start-of-week
sl@0
  1641
	dayNoInWeek -= startOfFirstWeek;
sl@0
  1642
	if (dayNoInWeek < 0)
sl@0
  1643
		dayNoInWeek += 7;
sl@0
  1644
	// calculate the days from the start-of-week to the start-of-next-year
sl@0
  1645
	TInt daysFrom=nextYearStartTime.DaysFrom(*this).Int()+dayNoInWeek;
sl@0
  1646
	// calculate the days from start-of-year to start-of-week (note this may be negative, but never < -6)
sl@0
  1647
	TInt days=dayNoInYear-dayNoInWeek;
sl@0
  1648
sl@0
  1649
	// the rule allows a certain number of week-1 days to lie in the previous year
sl@0
  1650
	TInt prevyeardays;
sl@0
  1651
	switch (aRule)
sl@0
  1652
		{
sl@0
  1653
	default:
sl@0
  1654
		return -1;
sl@0
  1655
	case EFirstWeek:
sl@0
  1656
		prevyeardays = 6;
sl@0
  1657
		break;
sl@0
  1658
	case EFirstFourDayWeek:
sl@0
  1659
		prevyeardays = 3;
sl@0
  1660
		break;
sl@0
  1661
	case EFirstFullWeek:
sl@0
  1662
		prevyeardays = 0;
sl@0
  1663
		break;
sl@0
  1664
		}
sl@0
  1665
sl@0
  1666
	// check for a week which belongs to last year
sl@0
  1667
	if (days + prevyeardays < 0)
sl@0
  1668
		{
sl@0
  1669
		// in week 52 or 53 of last year, find the week # of the first day in the week
sl@0
  1670
		startDateTime.SetYearLeapCheck(startDateTime.Year()-1);
sl@0
  1671
		return (*this-TTimeIntervalDays(dayNoInWeek)).WeekNoInYear(TTime(startDateTime),aRule);
sl@0
  1672
		}
sl@0
  1673
sl@0
  1674
	// check for a week which belongs to next year
sl@0
  1675
	if (daysFrom <= prevyeardays)
sl@0
  1676
		return 1;
sl@0
  1677
sl@0
  1678
	// calculate the week number, accounting for the requested week-1 rule
sl@0
  1679
	return (days + 7 + prevyeardays)/7;
sl@0
  1680
	}
sl@0
  1681
sl@0
  1682
EXPORT_C void TTime::FormatL(TDes &aDes,const TDesC &aFormat) const
sl@0
  1683
//
sl@0
  1684
// Fill aString with current Date and Time according to given aFormat string
sl@0
  1685
//
sl@0
  1686
/**
sl@0
  1687
Puts this TTime into a descriptor and formats it according to the format string 
sl@0
  1688
specified in the second argument.
sl@0
  1689
sl@0
  1690
Many of the formatting commands use the 
sl@0
  1691
system's locale settings for the date and time, for example the characters 
sl@0
  1692
used to separate components of the date and time and the ordering of day, 
sl@0
  1693
month and year. The list of formatting commands below is divided into two 
sl@0
  1694
sections, the first of which lists the commands which operate without reference 
sl@0
  1695
to the locale's date and time settings (see class TLocale) and the second 
sl@0
  1696
table lists the commands which do use these settings.
sl@0
  1697
sl@0
  1698
The following formatting commands do not honour the locale-specific system 
sl@0
  1699
settings:
sl@0
  1700
sl@0
  1701
\%\% : Include a single '%' character in the string
sl@0
  1702
sl@0
  1703
\%* : Abbreviate following item (the following item should not be preceded 
sl@0
  1704
by a '%' character).
sl@0
  1705
sl@0
  1706
\%C : Interpret the argument as the six digit microsecond component of the 
sl@0
  1707
time. In its abbreviated form, ('%*C') this should be followed by an integer 
sl@0
  1708
between zero and six, where the integer indicates the number of digits to display.
sl@0
  1709
sl@0
  1710
\%D : Interpret the argument as the two digit day number in the month. Abbreviation 
sl@0
  1711
suppresses leading zero.
sl@0
  1712
sl@0
  1713
\%E : Interpret the argument as the day name. Abbreviation is language-specific 
sl@0
  1714
(e.g. English uses the first three letters).
sl@0
  1715
sl@0
  1716
\%F : Use this command for locale-independent ordering of date components. 
sl@0
  1717
This orders the following day/month/year component(s) (\%D, \%M, \%Y for example) 
sl@0
  1718
according to the order in which they are specified in the string. This removes 
sl@0
  1719
the need to use \%1 to \%5 (described below).
sl@0
  1720
sl@0
  1721
\%H : Interpret the argument as the one or two digit hour component of the 
sl@0
  1722
time in 24 hour time format. Abbreviation suppresses leading zero. For locale-dependent 
sl@0
  1723
hour formatting, use \%J.
sl@0
  1724
sl@0
  1725
\%I : Interpret the argument as the one or two digit hour component of the 
sl@0
  1726
time in 12 hour time format. The leading zero is automatically suppressed 
sl@0
  1727
so that abbreviation has no effect. For locale-dependent hour formatting, 
sl@0
  1728
use \%J.
sl@0
  1729
sl@0
  1730
\%M : Interpret the argument as the one or two digit month number. Abbreviation 
sl@0
  1731
suppresses leading zero.
sl@0
  1732
sl@0
  1733
\%N : Interpret the argument as the month name. Abbreviation is language specific, e.g. 
sl@0
  1734
English uses the first three letters only. When using locale-dependent formatting, 
sl@0
  1735
(that is, \%F has not previously been specified), specifying \%N causes any 
sl@0
  1736
subsequent occurrence of a month specifier in the string to insert the month 
sl@0
  1737
as text rather than in numeric form. When using locale-independent formatting, 
sl@0
  1738
specifying \%N causes the month to be inserted as text at that position, but 
sl@0
  1739
any subsequent occurrence of \%M will cause the month to be inserted in numeric 
sl@0
  1740
form.
sl@0
  1741
sl@0
  1742
\%S : Interpret the argument as the one or two digit seconds component of the 
sl@0
  1743
time. Abbreviation suppresses leading zero.
sl@0
  1744
sl@0
  1745
\%T : Interpret the argument as the one or two digit minutes component of the 
sl@0
  1746
time. Abbreviation suppresses leading zero.
sl@0
  1747
sl@0
  1748
\%W : Interpret the argument as the one or two digit week number in year. Abbreviation 
sl@0
  1749
suppresses leading zero.
sl@0
  1750
sl@0
  1751
\%X : Interpret the argument as the date suffix. Cannot be abbreviated. When 
sl@0
  1752
using locale-dependent formatting (that is, \%F has not previously been specified), 
sl@0
  1753
\%X causes all further occurrences of the day number to be displayed with the 
sl@0
  1754
date suffix. When using locale-independent formatting, a date suffix will 
sl@0
  1755
be inserted only after the occurrence of the day number which \%X follows in 
sl@0
  1756
the format string. Any further occurrence of \%D without a following \%X will 
sl@0
  1757
insert the day number without a suffix.
sl@0
  1758
sl@0
  1759
\%Y : Interpret the argument as the four digit year number. Abbreviation suppresses 
sl@0
  1760
the first two digits.
sl@0
  1761
sl@0
  1762
\%Z : Interpret the argument as the one, two or three digit day number in the 
sl@0
  1763
year. Abbreviation suppresses leading zeros.
sl@0
  1764
sl@0
  1765
The following formatting commands do honour the locale-specific system settings:
sl@0
  1766
sl@0
  1767
\%. : Interpret the argument as the decimal separator character (as set by 
sl@0
  1768
TLocale::SetDecimalSeparator()). The decimal separator is used to separate 
sl@0
  1769
seconds and microseconds, if present.
sl@0
  1770
sl@0
  1771
\%: : Interpret the argument as one of the four time separator characters (as 
sl@0
  1772
set by TLocale::SetTimeSeparator()). Must be followed by an integer between 
sl@0
  1773
zero and three inclusive to indicate which time separator character is being 
sl@0
  1774
referred to.
sl@0
  1775
sl@0
  1776
\%/ : Interpret the argument as one of the four date separator characters (as 
sl@0
  1777
set by TLocale::SetDateSeparator()). Must be followed by an integer between 
sl@0
  1778
zero and three inclusive to indicate which date separator character is being 
sl@0
  1779
referred to.
sl@0
  1780
sl@0
  1781
\%1 : Interpret the argument as the first component of a three component date 
sl@0
  1782
(i.e. day, month or year) where the order has been set by TLocale::SetDateFormat(). 
sl@0
  1783
When the date format is EDateEuropean, this is the day, when EDateAmerican, 
sl@0
  1784
the month, and when EDateJapanese, the year. For more information on this 
sl@0
  1785
and the following four formatting commands, see the Notes section immediately 
sl@0
  1786
below.
sl@0
  1787
sl@0
  1788
\%2 : Interpret the argument as the second component of a three component date 
sl@0
  1789
where the order has been set by TLocale::SetDateFormat(). When the date format 
sl@0
  1790
is EDateEuropean, this is the month, when EDateAmerican, the day and when 
sl@0
  1791
EDateJapanese, the month.
sl@0
  1792
sl@0
  1793
\%3 : Interpret the argument as the third component of a three component date 
sl@0
  1794
where the order has been set by TLocale::SetDateFormat(). When the date format 
sl@0
  1795
is EDateEuropean, or EDateAmerican this is the year and when EDateJapanese, 
sl@0
  1796
the day.
sl@0
  1797
sl@0
  1798
\%4 : Interpret the argument as the first component of a two component date 
sl@0
  1799
(day and month) where the order has been set by TLocale::SetDateFormat(). 
sl@0
  1800
When the date format is EDateEuropean this is the day, and when EDateAmerican 
sl@0
  1801
or EDateJapanese, the month.
sl@0
  1802
sl@0
  1803
\%5 : Interpret the argument as the second component of a two component date 
sl@0
  1804
(day and month) where the order has been set by TLocale::SetDateFormat(). 
sl@0
  1805
When the date format is EDateEuropean this is the month, and when EDateAmerican 
sl@0
  1806
or EDateJapanese, the day.
sl@0
  1807
sl@0
  1808
\%A : Interpret the argument as "am" or "pm" text according to the current 
sl@0
  1809
language and time of day. Unlike the \%B formatting command (described below), 
sl@0
  1810
\%A disregards the locale's 12 or 24 hour clock setting, so that when used 
sl@0
  1811
without an inserted + or - sign, am/pm text will always be displayed. Whether 
sl@0
  1812
a space is inserted between the am/pm text and the time depends on the locale-specific 
sl@0
  1813
settings. However, if abbreviated (\%*A), no space is inserted, regardless 
sl@0
  1814
of the locale's settings. The am/pm text appears before or after the time, 
sl@0
  1815
according to the position of the \%A, regardless of the locale-specific settings. 
sl@0
  1816
For example, the following ordering of formatting commands causes am/pm text 
sl@0
  1817
to be printed after the time: \%H \%T \%S \%A. Optionally, a minus or plus sign 
sl@0
  1818
may be inserted between the "%" and the "A". This operates as follows:
sl@0
  1819
sl@0
  1820
\%-A causes am/pm text to be inserted into the descriptor only if the am/pm 
sl@0
  1821
symbol position has been set in the locale to ELocaleBefore. Cannot be abbreviated 
sl@0
  1822
using asterisk.
sl@0
  1823
sl@0
  1824
\%+A causes am/pm text to be inserted into the descriptor only if the am/pm 
sl@0
  1825
symbol position has been set in the locale to ELocaleAfter. Cannot be abbreviated 
sl@0
  1826
using asterisk. For example, the following formatting commands will cause 
sl@0
  1827
am/pm text to be displayed after the time if the am/pm position has been set 
sl@0
  1828
in the locale to ELocaleAfter or before the time if ELocaleBefore: \%-A \%H 
sl@0
  1829
\%T \%S \%+A.
sl@0
  1830
sl@0
  1831
\%B Interpret the argument as am or pm text according to the current language 
sl@0
  1832
and time of day. Unlike the \%A command, when using \%B, am/pm text is displayed 
sl@0
  1833
only if the clock setting in the locale is 12-hour. Whether a space is inserted 
sl@0
  1834
between the am/pm text and the time depends on the locale-specific settings. 
sl@0
  1835
However, if abbreviated (\%*B), no space is inserted, regardless of the locale's 
sl@0
  1836
settings. The am/pm text appears before or after the time, according to the 
sl@0
  1837
location of the "%B", regardless of the locale-specific settings. For example, 
sl@0
  1838
the following formatting commands cause am/pm text to be printed after the 
sl@0
  1839
time: \%H \%T \%S \%B. Optionally, a minus or plus sign may be inserted between 
sl@0
  1840
the "%" and the "B". This operates as follows:
sl@0
  1841
sl@0
  1842
\%-B causes am/pm text to be inserted into the descriptor only if using a 12 
sl@0
  1843
hour clock and the am/pm symbol position has been set in the locale to ELocaleBefore. 
sl@0
  1844
Cannot be abbreviated using asterisk.
sl@0
  1845
sl@0
  1846
\%+B causes am/pm text to be inserted into the descriptor only if using a 12 
sl@0
  1847
hour clock and the am/pm symbol position has been set in the locale to ELocaleAfter. 
sl@0
  1848
Cannot be abbreviated using asterisk. For example, the following formatting 
sl@0
  1849
commands cause am/pm text to be printed after the time if the am/pm position 
sl@0
  1850
has been set in the locale to ELocaleAfter or before the time if ELocaleBefore: 
sl@0
  1851
\%-B \%H \%T \%S \%+B.
sl@0
  1852
sl@0
  1853
\%J Interpret the argument as the hour component of the time in either 12 or 
sl@0
  1854
24 hour clock format depending on the locale's clock format setting. When 
sl@0
  1855
the clock format has been set to 12 hour, leading zeros are automatically 
sl@0
  1856
suppressed so that abbreviation has no effect. Abbreviation suppresses leading 
sl@0
  1857
zero only when using a 24 hour clock.
sl@0
  1858
sl@0
  1859
Notes:
sl@0
  1860
sl@0
  1861
The \%1, \%2, \%3, \%4 and \%5 formatting commands are used in conjunction with 
sl@0
  1862
\%D, \%M and \%Y to format the date locale-dependently. When formatting the date 
sl@0
  1863
locale-dependently, the order of the day, month and year components within 
sl@0
  1864
the string is determined by the order of the \%1 to \%5 formatting commands, 
sl@0
  1865
not that of \%D, \%M, \%Y.
sl@0
  1866
sl@0
  1867
When formatting the date locale-independently (that is, \%F has been specified 
sl@0
  1868
in the format string), the \%1 to \%5 formatting commands are not required, 
sl@0
  1869
and should be omitted. In this case, the order of the date components is determined 
sl@0
  1870
by the order of the \%D, \%M, \%Y format commands within aFormat.
sl@0
  1871
sl@0
  1872
Up to four date separators and up to four time separators can be used to separate 
sl@0
  1873
the components of a date or time. When formatting a numerical date consisting 
sl@0
  1874
of the day, month and year or a time containing hours, minutes and seconds, 
sl@0
  1875
all four separators should always be specified in the format command string. 
sl@0
  1876
Usually, the leading and trailing separators should not be displayed. In this 
sl@0
  1877
case, the first and fourth separators should still be specified, but should 
sl@0
  1878
be represented by a null character.
sl@0
  1879
sl@0
  1880
The date format follows the pattern:
sl@0
  1881
sl@0
  1882
DateSeparator[0] DateComponent1 DateSeparator[1] DateComponent2 DateSeparator[2] 
sl@0
  1883
DateComponent3 DateSeparator[3]
sl@0
  1884
sl@0
  1885
where the ordering of date components is determined by the locale's date format 
sl@0
  1886
setting.
sl@0
  1887
sl@0
  1888
The time format follows the pattern:
sl@0
  1889
sl@0
  1890
TimeSeparator[0] Hours TimeSeparator[1] Minutes TimeSeparator[2] Seconds TimeSeparator[3]
sl@0
  1891
sl@0
  1892
If the time includes a microseconds component, the third separator should 
sl@0
  1893
occur after the microseconds, and the seconds and microseconds should be separated 
sl@0
  1894
by the decimal separator. When formatting a two component time, the following 
sl@0
  1895
rules apply:
sl@0
  1896
sl@0
  1897
if the time consists of hours and minutes, the third time delimiter should 
sl@0
  1898
be omitted 
sl@0
  1899
sl@0
  1900
if the time consists of minutes and seconds, the second time delimiter should 
sl@0
  1901
be omitted
sl@0
  1902
sl@0
  1903
@param aDes    Descriptor, which,  on return contains the formatted date/time string.
sl@0
  1904
@param aFormat Format string which determines the format of the date and time.
sl@0
  1905
sl@0
  1906
@leave KErrOverflow The date/time string is too long for the descriptor aDes.
sl@0
  1907
@leave KErrGeneral  A formatting error has occurred.
sl@0
  1908
*/
sl@0
  1909
	{
sl@0
  1910
	TLocale local;
sl@0
  1911
	FormatL(aDes,aFormat,local);
sl@0
  1912
	}
sl@0
  1913
sl@0
  1914
EXPORT_C void TTime::FormatL(TDes &aDes,const TDesC &aFormat,const TLocale &aLocale) const
sl@0
  1915
//
sl@0
  1916
// Fill aString with current Date and Time according to given aFormat string
sl@0
  1917
//
sl@0
  1918
/**
sl@0
  1919
Puts this TTime into a descriptor and formats it according to the format string 
sl@0
  1920
specified in the second argument.
sl@0
  1921
sl@0
  1922
Many of the formatting commands use the 
sl@0
  1923
system's locale settings for the date and time, for example the characters 
sl@0
  1924
used to separate components of the date and time and the ordering of day, 
sl@0
  1925
month and year. The list of formatting commands below is divided into two 
sl@0
  1926
sections, the first of which lists the commands which operate without reference 
sl@0
  1927
to the locale's date and time settings (see class TLocale) and the second 
sl@0
  1928
table lists the commands which do use these settings.
sl@0
  1929
sl@0
  1930
The following formatting commands do not honour the locale-specific system 
sl@0
  1931
settings:
sl@0
  1932
sl@0
  1933
\%\% : Include a single '%' character in the string
sl@0
  1934
sl@0
  1935
\%* : Abbreviate following item (the following item should not be preceded 
sl@0
  1936
by a '%' character).
sl@0
  1937
sl@0
  1938
\%C : Interpret the argument as the six digit microsecond component of the 
sl@0
  1939
time. In its abbreviated form, ('%*C') this should be followed by an integer 
sl@0
  1940
between zero and six, where the integer indicates the number of digits to display.
sl@0
  1941
sl@0
  1942
\%D : Interpret the argument as the two digit day number in the month. Abbreviation 
sl@0
  1943
suppresses leading zero.
sl@0
  1944
sl@0
  1945
\%E : Interpret the argument as the day name. Abbreviation is language-specific 
sl@0
  1946
(e.g. English uses the first three letters).
sl@0
  1947
sl@0
  1948
\%F : Use this command for locale-independent ordering of date components. 
sl@0
  1949
This orders the following day/month/year component(s) (\%D, \%M, \%Y for example) 
sl@0
  1950
according to the order in which they are specified in the string. This removes 
sl@0
  1951
the need to use \%1 to \%5 (described below).
sl@0
  1952
sl@0
  1953
\%H : Interpret the argument as the one or two digit hour component of the 
sl@0
  1954
time in 24 hour time format. Abbreviation suppresses leading zero. For locale-dependent 
sl@0
  1955
hour formatting, use \%J.
sl@0
  1956
sl@0
  1957
\%I : Interpret the argument as the one or two digit hour component of the 
sl@0
  1958
time in 12 hour time format. The leading zero is automatically suppressed 
sl@0
  1959
so that abbreviation has no effect. For locale-dependent hour formatting, 
sl@0
  1960
use \%J.
sl@0
  1961
sl@0
  1962
\%M : Interpret the argument as the one or two digit month number. Abbreviation 
sl@0
  1963
suppresses leading zero.
sl@0
  1964
sl@0
  1965
\%N : Interpret the argument as the month name. Abbreviation is language specific, e.g. 
sl@0
  1966
English uses the first three letters only. When using locale-dependent formatting, 
sl@0
  1967
(that is, \%F has not previously been specified), specifying \%N causes any 
sl@0
  1968
subsequent occurrence of a month specifier in the string to insert the month 
sl@0
  1969
as text rather than in numeric form. When using locale-independent formatting, 
sl@0
  1970
specifying \%N causes the month to be inserted as text at that position, but 
sl@0
  1971
any subsequent occurrence of \%M will cause the month to be inserted in numeric 
sl@0
  1972
form.
sl@0
  1973
sl@0
  1974
\%S : Interpret the argument as the one or two digit seconds component of the 
sl@0
  1975
time. Abbreviation suppresses leading zero.
sl@0
  1976
sl@0
  1977
\%T : Interpret the argument as the one or two digit minutes component of the 
sl@0
  1978
time. Abbreviation suppresses leading zero.
sl@0
  1979
sl@0
  1980
\%W : Interpret the argument as the one or two digit week number in year. Abbreviation 
sl@0
  1981
suppresses leading zero.
sl@0
  1982
sl@0
  1983
\%X : Interpret the argument as the date suffix. Cannot be abbreviated. When 
sl@0
  1984
using locale-dependent formatting (that is, \%F has not previously been specified), 
sl@0
  1985
\%X causes all further occurrences of the day number to be displayed with the 
sl@0
  1986
date suffix. When using locale-independent formatting, a date suffix will 
sl@0
  1987
be inserted only after the occurrence of the day number which \%X follows in 
sl@0
  1988
the format string. Any further occurrence of \%D without a following \%X will 
sl@0
  1989
insert the day number without a suffix.
sl@0
  1990
sl@0
  1991
\%Y : Interpret the argument as the four digit year number. Abbreviation suppresses 
sl@0
  1992
the first two digits.
sl@0
  1993
sl@0
  1994
\%Z : Interpret the argument as the one, two or three digit day number in the 
sl@0
  1995
year. Abbreviation suppresses leading zeros.
sl@0
  1996
sl@0
  1997
The following formatting commands do honour the locale-specific system settings:
sl@0
  1998
sl@0
  1999
\%. : Interpret the argument as the decimal separator character (as set by 
sl@0
  2000
TLocale::SetDecimalSeparator()). The decimal separator is used to separate 
sl@0
  2001
seconds and microseconds, if present.
sl@0
  2002
sl@0
  2003
\%: : Interpret the argument as one of the four time separator characters (as 
sl@0
  2004
set by TLocale::SetTimeSeparator()). Must be followed by an integer between 
sl@0
  2005
zero and three inclusive to indicate which time separator character is being 
sl@0
  2006
referred to.
sl@0
  2007
sl@0
  2008
\%/ : Interpret the argument as one of the four date separator characters (as 
sl@0
  2009
set by TLocale::SetDateSeparator()). Must be followed by an integer between 
sl@0
  2010
zero and three inclusive to indicate which date separator character is being 
sl@0
  2011
referred to.
sl@0
  2012
sl@0
  2013
\%1 : Interpret the argument as the first component of a three component date 
sl@0
  2014
(i.e. day, month or year) where the order has been set by TLocale::SetDateFormat(). 
sl@0
  2015
When the date format is EDateEuropean, this is the day, when EDateAmerican, 
sl@0
  2016
the month, and when EDateJapanese, the year. For more information on this 
sl@0
  2017
and the following four formatting commands, see the Notes section immediately 
sl@0
  2018
below.
sl@0
  2019
sl@0
  2020
\%2 : Interpret the argument as the second component of a three component date 
sl@0
  2021
where the order has been set by TLocale::SetDateFormat(). When the date format 
sl@0
  2022
is EDateEuropean, this is the month, when EDateAmerican, the day and when 
sl@0
  2023
EDateJapanese, the month.
sl@0
  2024
sl@0
  2025
\%3 : Interpret the argument as the third component of a three component date 
sl@0
  2026
where the order has been set by TLocale::SetDateFormat(). When the date format 
sl@0
  2027
is EDateEuropean, or EDateAmerican this is the year and when EDateJapanese, 
sl@0
  2028
the day.
sl@0
  2029
sl@0
  2030
\%4 : Interpret the argument as the first component of a two component date 
sl@0
  2031
(day and month) where the order has been set by TLocale::SetDateFormat(). 
sl@0
  2032
When the date format is EDateEuropean this is the day, and when EDateAmerican 
sl@0
  2033
or EDateJapanese, the month.
sl@0
  2034
sl@0
  2035
\%5 : Interpret the argument as the second component of a two component date 
sl@0
  2036
(day and month) where the order has been set by TLocale::SetDateFormat(). 
sl@0
  2037
When the date format is EDateEuropean this is the month, and when EDateAmerican 
sl@0
  2038
or EDateJapanese, the day.
sl@0
  2039
sl@0
  2040
\%A : Interpret the argument as "am" or "pm" text according to the current 
sl@0
  2041
language and time of day. Unlike the \%B formatting command (described below), 
sl@0
  2042
\%A disregards the locale's 12 or 24 hour clock setting, so that when used 
sl@0
  2043
without an inserted + or - sign, am/pm text will always be displayed. Whether 
sl@0
  2044
a space is inserted between the am/pm text and the time depends on the locale-specific 
sl@0
  2045
settings. However, if abbreviated (\%*A), no space is inserted, regardless 
sl@0
  2046
of the locale's settings. The am/pm text appears before or after the time, 
sl@0
  2047
according to the position of the \%A, regardless of the locale-specific settings. 
sl@0
  2048
For example, the following ordering of formatting commands causes am/pm text 
sl@0
  2049
to be printed after the time: \%H \%T \%S \%A. Optionally, a minus or plus sign 
sl@0
  2050
may be inserted between the "%" and the "A". This operates as follows:
sl@0
  2051
sl@0
  2052
\%-A causes am/pm text to be inserted into the descriptor only if the am/pm 
sl@0
  2053
symbol position has been set in the locale to ELocaleBefore. Cannot be abbreviated 
sl@0
  2054
using asterisk.
sl@0
  2055
sl@0
  2056
\%+A causes am/pm text to be inserted into the descriptor only if the am/pm 
sl@0
  2057
symbol position has been set in the locale to ELocaleAfter. Cannot be abbreviated 
sl@0
  2058
using asterisk. For example, the following formatting commands will cause 
sl@0
  2059
am/pm text to be displayed after the time if the am/pm position has been set 
sl@0
  2060
in the locale to ELocaleAfter or before the time if ELocaleBefore: \%-A \%H 
sl@0
  2061
\%T \%S \%+A.
sl@0
  2062
sl@0
  2063
\%B Interpret the argument as am or pm text according to the current language 
sl@0
  2064
and time of day. Unlike the \%A command, when using \%B, am/pm text is displayed 
sl@0
  2065
only if the clock setting in the locale is 12-hour. Whether a space is inserted 
sl@0
  2066
between the am/pm text and the time depends on the locale-specific settings. 
sl@0
  2067
However, if abbreviated (\%*B), no space is inserted, regardless of the locale's 
sl@0
  2068
settings. The am/pm text appears before or after the time, according to the 
sl@0
  2069
location of the "%B", regardless of the locale-specific settings. For example, 
sl@0
  2070
the following formatting commands cause am/pm text to be printed after the 
sl@0
  2071
time: \%H \%T \%S \%B. Optionally, a minus or plus sign may be inserted between 
sl@0
  2072
the "%" and the "B". This operates as follows:
sl@0
  2073
sl@0
  2074
\%-B causes am/pm text to be inserted into the descriptor only if using a 12 
sl@0
  2075
hour clock and the am/pm symbol position has been set in the locale to ELocaleBefore. 
sl@0
  2076
Cannot be abbreviated using asterisk.
sl@0
  2077
sl@0
  2078
\%+B causes am/pm text to be inserted into the descriptor only if using a 12 
sl@0
  2079
hour clock and the am/pm symbol position has been set in the locale to ELocaleAfter. 
sl@0
  2080
Cannot be abbreviated using asterisk. For example, the following formatting 
sl@0
  2081
commands cause am/pm text to be printed after the time if the am/pm position 
sl@0
  2082
has been set in the locale to ELocaleAfter or before the time if ELocaleBefore: 
sl@0
  2083
\%-B \%H \%T \%S \%+B.
sl@0
  2084
sl@0
  2085
\%J Interpret the argument as the hour component of the time in either 12 or 
sl@0
  2086
24 hour clock format depending on the locale's clock format setting. When 
sl@0
  2087
the clock format has been set to 12 hour, leading zeros are automatically 
sl@0
  2088
suppressed so that abbreviation has no effect. Abbreviation suppresses leading 
sl@0
  2089
zero only when using a 24 hour clock.
sl@0
  2090
sl@0
  2091
Notes:
sl@0
  2092
sl@0
  2093
The \%1, \%2, \%3, \%4 and \%5 formatting commands are used in conjunction with 
sl@0
  2094
\%D, \%M and \%Y to format the date locale-dependently. When formatting the date 
sl@0
  2095
locale-dependently, the order of the day, month and year components within 
sl@0
  2096
the string is determined by the order of the \%1 to \%5 formatting commands, 
sl@0
  2097
not that of \%D, \%M, \%Y.
sl@0
  2098
sl@0
  2099
When formatting the date locale-independently (that is, \%F has been specified 
sl@0
  2100
in the format string), the \%1 to \%5 formatting commands are not required, 
sl@0
  2101
and should be omitted. In this case, the order of the date components is determined 
sl@0
  2102
by the order of the \%D, \%M, \%Y format commands within aFormat.
sl@0
  2103
sl@0
  2104
Up to four date separators and up to four time separators can be used to separate 
sl@0
  2105
the components of a date or time. When formatting a numerical date consisting 
sl@0
  2106
of the day, month and year or a time containing hours, minutes and seconds, 
sl@0
  2107
all four separators should always be specified in the format command string. 
sl@0
  2108
Usually, the leading and trailing separators should not be displayed. In this 
sl@0
  2109
case, the first and fourth separators should still be specified, but should 
sl@0
  2110
be represented by a null character.
sl@0
  2111
sl@0
  2112
The date format follows the pattern:
sl@0
  2113
sl@0
  2114
DateSeparator[0] DateComponent1 DateSeparator[1] DateComponent2 DateSeparator[2] 
sl@0
  2115
DateComponent3 DateSeparator[3]
sl@0
  2116
sl@0
  2117
where the ordering of date components is determined by the locale's date format 
sl@0
  2118
setting.
sl@0
  2119
sl@0
  2120
The time format follows the pattern:
sl@0
  2121
sl@0
  2122
TimeSeparator[0] Hours TimeSeparator[1] Minutes TimeSeparator[2] Seconds TimeSeparator[3]
sl@0
  2123
sl@0
  2124
If the time includes a microseconds component, the third separator should 
sl@0
  2125
occur after the microseconds, and the seconds and microseconds should be separated 
sl@0
  2126
by the decimal separator. When formatting a two component time, the following 
sl@0
  2127
rules apply:
sl@0
  2128
sl@0
  2129
if the time consists of hours and minutes, the third time delimiter should 
sl@0
  2130
be omitted 
sl@0
  2131
sl@0
  2132
if the time consists of minutes and seconds, the second time delimiter should 
sl@0
  2133
be omitted
sl@0
  2134
sl@0
  2135
@param aDes    Descriptor, which,  on return contains the formatted date/time string.
sl@0
  2136
@param aFormat Format string which determines the format of the date and time.
sl@0
  2137
@param aLocale Specific locale which formatting will be based on.
sl@0
  2138
sl@0
  2139
@leave KErrOverflow The date/time string is too long for the descriptor aDes.
sl@0
  2140
@leave KErrGeneral  A formatting error has occurred.
sl@0
  2141
*/
sl@0
  2142
	{
sl@0
  2143
sl@0
  2144
	TDateTime dateTime=DateTime();
sl@0
  2145
	aDes.Zero(); // ensure string is empty at start
sl@0
  2146
sl@0
  2147
 	TLex aFmt(aFormat);
sl@0
  2148
	TBool fix=EFalse; // fixed date format
sl@0
  2149
	TBool da=EFalse; // day unabreviated
sl@0
  2150
	TBool ma=EFalse; // month unabreviated
sl@0
  2151
	TBool ya=EFalse; // year unabreviated
sl@0
  2152
	TBool suff=EFalse; // default no suffix
sl@0
  2153
	TBool mnam=EFalse; // default month as a number
sl@0
  2154
	TTimeOverflowLeave overflowLeave;
sl@0
  2155
sl@0
  2156
   	while (!aFmt.Eos())
sl@0
  2157
		{
sl@0
  2158
		TChar ch=aFmt.Get();
sl@0
  2159
		TBool abb=EFalse;
sl@0
  2160
		const TInt NoPosSpecified=-1;
sl@0
  2161
		TInt pos=NoPosSpecified;
sl@0
  2162
		if (ch=='%')
sl@0
  2163
			ch=aFmt.Get();
sl@0
  2164
		else // not formatting,just want to add some characters to string
sl@0
  2165
			goto doAppend; 
sl@0
  2166
		if (ch=='*') // => abbreviate next field
sl@0
  2167
			{
sl@0
  2168
			abb=ETrue;
sl@0
  2169
			ch=aFmt.Get();
sl@0
  2170
			}
sl@0
  2171
		else if (ch=='+' || ch=='-') // => leading or following Am/Pm
sl@0
  2172
			{
sl@0
  2173
			pos= ((ch=='+') ? ELocaleAfter : ELocaleBefore);
sl@0
  2174
			ch=aFmt.Get();
sl@0
  2175
			if (ch!='A' && ch!='B')
sl@0
  2176
				User::Leave(KErrGeneral);
sl@0
  2177
			}
sl@0
  2178
		switch (ch)
sl@0
  2179
			{
sl@0
  2180
		case ':': // local time separator
sl@0
  2181
				{
sl@0
  2182
				if (aDes.Length()==aDes.MaxLength())
sl@0
  2183
					User::Leave(KErrOverflow);
sl@0
  2184
				ch=aFmt.Get();//Which separator?
sl@0
  2185
				if (ch<'0' || ch>='0'+KMaxTimeSeparators)
sl@0
  2186
					User::Leave(KErrGeneral);
sl@0
  2187
				ch-='0';
sl@0
  2188
				TChar separator=aLocale.TimeSeparator(ch);
sl@0
  2189
				if (separator!=0)
sl@0
  2190
					aDes.Append(separator);
sl@0
  2191
				}
sl@0
  2192
			break;
sl@0
  2193
		case '/': // local date separator
sl@0
  2194
				{
sl@0
  2195
				if (aDes.Length()==aDes.MaxLength())
sl@0
  2196
					User::Leave(KErrOverflow);
sl@0
  2197
				ch=aFmt.Get();//Which separator?
sl@0
  2198
				if (ch<'0' || ch>='0'+KMaxDateSeparators)
sl@0
  2199
					User::Leave(KErrGeneral);
sl@0
  2200
				ch-='0';
sl@0
  2201
				TChar separator=aLocale.DateSeparator(ch);
sl@0
  2202
				if (separator!=0)
sl@0
  2203
					aDes.Append(separator);
sl@0
  2204
				}
sl@0
  2205
			break;
sl@0
  2206
		case '.': // local decimal separator
sl@0
  2207
				{
sl@0
  2208
				if (aDes.Length()==aDes.MaxLength())
sl@0
  2209
					User::Leave(KErrOverflow);
sl@0
  2210
				aDes.Append(aLocale.DecimalSeparator());
sl@0
  2211
				}
sl@0
  2212
			break;
sl@0
  2213
		case '1': // 1st element of date,local order
sl@0
  2214
			switch (aLocale.DateFormat())
sl@0
  2215
				{
sl@0
  2216
			case EDateAmerican:
sl@0
  2217
				goto doMonth;
sl@0
  2218
			case EDateJapanese:
sl@0
  2219
				goto doYear;
sl@0
  2220
			default: // European
sl@0
  2221
				goto doDay;
sl@0
  2222
				}
sl@0
  2223
		case '2': // 2nd element of date,local order
sl@0
  2224
			switch (aLocale.DateFormat())
sl@0
  2225
				{
sl@0
  2226
			case EDateAmerican:
sl@0
  2227
				goto doDay;
sl@0
  2228
			default: // European and Japanese have month second
sl@0
  2229
				goto doMonth;
sl@0
  2230
				}
sl@0
  2231
		case '3': // 3rd element of date,local order
sl@0
  2232
			switch (aLocale.DateFormat())
sl@0
  2233
				{
sl@0
  2234
			case EDateJapanese:
sl@0
  2235
				goto doDay;
sl@0
  2236
			default: // European and American have year last
sl@0
  2237
				goto doYear;
sl@0
  2238
				}
sl@0
  2239
		case '4': // 1st element of date (no year),local order
sl@0
  2240
			switch (aLocale.DateFormat())
sl@0
  2241
				{
sl@0
  2242
			case EDateEuropean:
sl@0
  2243
				goto doDay;
sl@0
  2244
			default:
sl@0
  2245
				goto doMonth;
sl@0
  2246
				}
sl@0
  2247
		case '5': // 2nd element of date (no year),local order
sl@0
  2248
			switch (aLocale.DateFormat())
sl@0
  2249
				{
sl@0
  2250
			case EDateEuropean:
sl@0
  2251
				goto doMonth;
sl@0
  2252
			default:
sl@0
  2253
				goto doDay;
sl@0
  2254
				}
sl@0
  2255
		case 'A': // am/pm text
sl@0
  2256
doAmPm:
sl@0
  2257
            {
sl@0
  2258
            if (pos==NoPosSpecified || pos==aLocale.AmPmSymbolPosition())
sl@0
  2259
				{
sl@0
  2260
				TBuf<KMaxAmPmName+1> format(_S("%S"));
sl@0
  2261
				if (!abb && aLocale.AmPmSpaceBetween())
sl@0
  2262
					{
sl@0
  2263
					if (aLocale.AmPmSymbolPosition()==ELocaleBefore)
sl@0
  2264
						format.Append(' ');
sl@0
  2265
					else
sl@0
  2266
						{
sl@0
  2267
						if (aDes.Length()==aDes.MaxLength())
sl@0
  2268
							User::Leave(KErrOverflow);
sl@0
  2269
						aDes.Append(' ');
sl@0
  2270
						}
sl@0
  2271
					}
sl@0
  2272
				TAmPmName amPm((dateTime.Hour()<12) ? EAm : EPm);
sl@0
  2273
				aDes.AppendFormat(format,&overflowLeave,&amPm);
sl@0
  2274
				}
sl@0
  2275
			break;
sl@0
  2276
            }
sl@0
  2277
		case 'B': // am/pm text if local time format is 12 hour clock
sl@0
  2278
			if (aLocale.TimeFormat()==ETime24)
sl@0
  2279
				break;
sl@0
  2280
			else
sl@0
  2281
				goto doAmPm;
sl@0
  2282
		case 'C':
sl@0
  2283
			{
sl@0
  2284
			TBuf<6> digits;
sl@0
  2285
			digits.AppendFormat(_L("%06d"),dateTime.MicroSecond());
sl@0
  2286
			TUint numChars=6;	// Default length
sl@0
  2287
			if (abb)
sl@0
  2288
				{
sl@0
  2289
				ch=aFmt.Get();
sl@0
  2290
				if (ch>='0' && ch<='6')
sl@0
  2291
					{
sl@0
  2292
					numChars=ch;
sl@0
  2293
					numChars-='0';
sl@0
  2294
					}
sl@0
  2295
				}
sl@0
  2296
			if (aDes.Length()>(TInt)(aDes.MaxLength()-numChars))
sl@0
  2297
			    User::Leave(KErrOverflow);
sl@0
  2298
			aDes.Append(digits.Left(numChars));
sl@0
  2299
			}	
sl@0
  2300
			break;
sl@0
  2301
		case 'D': // day in date
sl@0
  2302
			if (abb)
sl@0
  2303
				da=ETrue;
sl@0
  2304
			if (!fix)
sl@0
  2305
				break;
sl@0
  2306
			else
sl@0
  2307
				{
sl@0
  2308
doDay:
sl@0
  2309
				aDes.AppendFormat((da||abb) ? _L("%d"):_L("%02d"),&overflowLeave,dateTime.Day()+1);
sl@0
  2310
				if (suff)
sl@0
  2311
doSuffix:
sl@0
  2312
                    {
sl@0
  2313
                    TDateSuffix day(dateTime.Day());
sl@0
  2314
					aDes.AppendFormat(_L("%S"),&overflowLeave,&day);
sl@0
  2315
                    }
sl@0
  2316
				break;
sl@0
  2317
				}
sl@0
  2318
		case 'E': // Day name
sl@0
  2319
            {
sl@0
  2320
			TDay day=DayNoInWeek();
sl@0
  2321
			if (abb)
sl@0
  2322
				{
sl@0
  2323
	            TDayNameAbb nameAbb(day);
sl@0
  2324
				aDes.AppendFormat(_L("%S"),&overflowLeave,&nameAbb);
sl@0
  2325
				}
sl@0
  2326
			else
sl@0
  2327
				{
sl@0
  2328
	            TDayName name(day);
sl@0
  2329
				aDes.AppendFormat(_L("%S"),&overflowLeave,&name);
sl@0
  2330
				}
sl@0
  2331
			break;
sl@0
  2332
            }
sl@0
  2333
		case 'F': // => user wants day,month,year order fixed
sl@0
  2334
			fix=ETrue;
sl@0
  2335
			break;
sl@0
  2336
		case 'H': // hour in 24 hour time format
sl@0
  2337
do24:
sl@0
  2338
			aDes.AppendFormat((abb) ? _L("%d"):_L("%02d"),&overflowLeave,dateTime.Hour());
sl@0
  2339
			break;
sl@0
  2340
		case 'I': // hour in 12 hour time format
sl@0
  2341
do12:
sl@0
  2342
			{
sl@0
  2343
			TInt hour=dateTime.Hour();
sl@0
  2344
			if (hour==0)
sl@0
  2345
				hour=12;
sl@0
  2346
			else if (hour>12)
sl@0
  2347
				hour-=12;
sl@0
  2348
			aDes.AppendFormat(_L("%d"),&overflowLeave,hour);
sl@0
  2349
			break;
sl@0
  2350
			}
sl@0
  2351
		case 'J': //default time format for hour
sl@0
  2352
			if (aLocale.TimeFormat()==ETime12)
sl@0
  2353
				goto do12;
sl@0
  2354
			else
sl@0
  2355
				goto do24;
sl@0
  2356
		case 'M': // month as a number (default value)
sl@0
  2357
			if (abb)
sl@0
  2358
				ma=ETrue;
sl@0
  2359
			if (fix)
sl@0
  2360
				goto doMonth;
sl@0
  2361
			break;
sl@0
  2362
		case 'N': // month as a name
sl@0
  2363
			mnam=ETrue;
sl@0
  2364
			if (abb)
sl@0
  2365
				ma=ETrue;
sl@0
  2366
			if (!fix)
sl@0
  2367
				break;
sl@0
  2368
			else
sl@0
  2369
				{
sl@0
  2370
doMonth:
sl@0
  2371
				if (mnam)
sl@0
  2372
					{
sl@0
  2373
					TMonth month=dateTime.Month();
sl@0
  2374
					if (ma || abb)
sl@0
  2375
						{
sl@0
  2376
		                TMonthNameAbb nameAbb(month);
sl@0
  2377
						aDes.AppendFormat(_L("%S"),&overflowLeave,&nameAbb);
sl@0
  2378
						}
sl@0
  2379
					else
sl@0
  2380
						{
sl@0
  2381
	                    TMonthName name(month);
sl@0
  2382
						aDes.AppendFormat(_L("%S"),&overflowLeave,&name);
sl@0
  2383
						}
sl@0
  2384
					}
sl@0
  2385
				else
sl@0
  2386
					aDes.AppendFormat((ma||abb) ? _L("%d"):_L("%02d"),&overflowLeave,dateTime.Month()+1);
sl@0
  2387
				break;
sl@0
  2388
				}
sl@0
  2389
		case 'S': // seconds
sl@0
  2390
			aDes.AppendFormat((abb) ? _L("%d"):_L("%02d"),&overflowLeave,dateTime.Second());
sl@0
  2391
			break;	
sl@0
  2392
		case 'T': // minutes	
sl@0
  2393
			aDes.AppendFormat((abb) ? _L("%d"):_L("%02d"),&overflowLeave,dateTime.Minute());
sl@0
  2394
			break;
sl@0
  2395
		case 'W': // week no in year
sl@0
  2396
			aDes.AppendFormat((abb) ? _L("%d"):_L("%02d"),&overflowLeave,WeekNoInYear());
sl@0
  2397
			break;
sl@0
  2398
		case 'X': // => wants day suffix
sl@0
  2399
			if (fix)
sl@0
  2400
				goto doSuffix;
sl@0
  2401
			else
sl@0
  2402
				{
sl@0
  2403
				suff=ETrue;
sl@0
  2404
				break;
sl@0
  2405
				}
sl@0
  2406
		case 'Y': // year
sl@0
  2407
			if (abb)
sl@0
  2408
				ya=ETrue;
sl@0
  2409
			if (!fix)
sl@0
  2410
				break;
sl@0
  2411
			else
sl@0
  2412
				{
sl@0
  2413
doYear:
sl@0
  2414
				if (ya || abb)
sl@0
  2415
					aDes.AppendFormat(_L("%02d"),&overflowLeave,((dateTime.Year())%100));
sl@0
  2416
				else
sl@0
  2417
					aDes.AppendFormat(_L("%04d"),&overflowLeave,dateTime.Year());
sl@0
  2418
				break;
sl@0
  2419
				}
sl@0
  2420
		case 'Z': // day no in year
sl@0
  2421
			aDes.AppendFormat((abb) ? _L("%d"):_L("%03d"),&overflowLeave,DayNoInYear());
sl@0
  2422
			break;
sl@0
  2423
		default:
sl@0
  2424
doAppend:
sl@0
  2425
			if (aDes.Length()==aDes.MaxLength())
sl@0
  2426
				User::Leave(KErrOverflow);
sl@0
  2427
			aDes.Append(ch);
sl@0
  2428
			break;
sl@0
  2429
			}
sl@0
  2430
		}
sl@0
  2431
	}
sl@0
  2432
sl@0
  2433
sl@0
  2434
sl@0
  2435
sl@0
  2436
EXPORT_C TTime Time::NullTTime()
sl@0
  2437
/**
sl@0
  2438
Gets a TTime with a null value.
sl@0
  2439
sl@0
  2440
@return TTime object with a null value.
sl@0
  2441
*/
sl@0
  2442
	{
sl@0
  2443
	return UI64LIT(0x8000000000000000);
sl@0
  2444
	}
sl@0
  2445
sl@0
  2446
EXPORT_C TTime Time::MaxTTime()
sl@0
  2447
/**
sl@0
  2448
Gets the maximum time value which can be held in a TTime object.
sl@0
  2449
sl@0
  2450
@return The maximum TTime value.
sl@0
  2451
*/
sl@0
  2452
	{
sl@0
  2453
	return I64LIT(0x7fffffffffffffff);
sl@0
  2454
	}
sl@0
  2455
sl@0
  2456
EXPORT_C TTime Time::MinTTime()
sl@0
  2457
/**
sl@0
  2458
Gets the minimum time value which can be held in a TTime object.
sl@0
  2459
sl@0
  2460
@return The minimum TTime value.
sl@0
  2461
*/
sl@0
  2462
	{
sl@0
  2463
	return UI64LIT(0x8000000000000001);
sl@0
  2464
	}
sl@0
  2465
sl@0
  2466
EXPORT_C TInt Time::DaysInMonth(TInt aYear,TMonth aMonth)
sl@0
  2467
/**
sl@0
  2468
Gets the number of days in a month.
sl@0
  2469
sl@0
  2470
@param aYear  The year. Must be specified because of leap years.
sl@0
  2471
@param aMonth Month, from EJanuary to EDecember.
sl@0
  2472
sl@0
  2473
@return The number of days in the month.
sl@0
  2474
*/
sl@0
  2475
	{
sl@0
  2476
sl@0
  2477
    __ASSERT_DEBUG(aMonth<=EDecember && aMonth>=EJanuary,::Panic(ETTimeValueOutOfRange));
sl@0
  2478
    return(mTab[IsLeapYear(aYear)][aMonth]);
sl@0
  2479
	}
sl@0
  2480
sl@0
  2481
EXPORT_C TBool Time::IsLeapYear(TInt aYear)
sl@0
  2482
//
sl@0
  2483
// up to and including 1600 leap years were every 4 years,since then leap years are every 4 years unless
sl@0
  2484
// the year falls on a century which is not divisible by 4 (ie 1900 wasnt,2000 will be)
sl@0
  2485
// for simplicity define year 0 as a leap year
sl@0
  2486
//
sl@0
  2487
/**
sl@0
  2488
Tests whether a year is a leap year.
sl@0
  2489
sl@0
  2490
@param aYear The year of interest.
sl@0
  2491
sl@0
  2492
@return True if leap year, False if not.
sl@0
  2493
*/
sl@0
  2494
	{
sl@0
  2495
sl@0
  2496
	if (aYear>1600)
sl@0
  2497
    	return(!(aYear%4) && (aYear%100 || !(aYear%400)));
sl@0
  2498
	return(!(aYear%4));
sl@0
  2499
	}
sl@0
  2500
sl@0
  2501
EXPORT_C TInt Time::LeapYearsUpTo(TInt aYear)
sl@0
  2502
//
sl@0
  2503
// from 0AD to present year according to the rule above
sl@0
  2504
//
sl@0
  2505
/**
sl@0
  2506
Gets the number of leap years between 0 AD nominal Gregorian and the specified 
sl@0
  2507
year - inclusive.
sl@0
  2508
sl@0
  2509
@param aYear The final year in the range to search. If negative, the function 
sl@0
  2510
             will return a negative number of leap years.
sl@0
  2511
sl@0
  2512
@return The number of leap years between 0 AD nominal Gregorian and aYear.
sl@0
  2513
*/
sl@0
  2514
	{
sl@0
  2515
sl@0
  2516
	if (aYear<=0)
sl@0
  2517
		return(aYear/4);
sl@0
  2518
	if (aYear<=1600)
sl@0
  2519
		return(1+((aYear-1)/4));
sl@0
  2520
	TInt num=401; // 1600/4+1
sl@0
  2521
	aYear-=1601;
sl@0
  2522
	TInt century=aYear/100;
sl@0
  2523
	num+=(aYear/4-century+century/4);
sl@0
  2524
	return(num);
sl@0
  2525
	}
sl@0
  2526