os/ossrv/genericopenlibs/openenvcore/libc/src/timefuncs.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description:  This is a project specific include file for building the 
sl@0
    15
*                clock related functions as part of libc library.
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include <e32base.h>
sl@0
    21
#include <e32cons.h>
sl@0
    22
#include <tz.h>
sl@0
    23
#include <tzconverter.h>
sl@0
    24
#include "tzlocalizer.h" 
sl@0
    25
#include "tzlocalizationdatatypes.h"
sl@0
    26
#include "vtzrules.h"
sl@0
    27
#include "utf.h" 
sl@0
    28
#include "timefuncs.h"
sl@0
    29
#include "lposix.h"
sl@0
    30
#include "sysif.h"
sl@0
    31
#include "libc_wsd_defs.h"
sl@0
    32
sl@0
    33
// This represents the base EpochTime in microseconds, 
sl@0
    34
// which happens to be January 1, 1970 (midnight UTC/GMT), 
sl@0
    35
// not counting leap seconds. This effectively represents 
sl@0
    36
// the number of microseconds in 1970 years.    
sl@0
    37
static const TInt64 KEpochTime = 62168256000000000;
sl@0
    38
sl@0
    39
sl@0
    40
sl@0
    41
#ifndef EMULATOR
sl@0
    42
struct tm cachetm;
sl@0
    43
time_t prevchange;
sl@0
    44
time_t nextchange;
sl@0
    45
#else //EMULATOR
sl@0
    46
sl@0
    47
sl@0
    48
sl@0
    49
GET_GLOBAL_VAR_FROM_TLS(cachetm, struct tm)
sl@0
    50
#define cachetm (*GET_WSD_VAR_NAME(cachetm, g)())
sl@0
    51
GET_GLOBAL_VAR_FROM_TLS(prevchange, time_t)
sl@0
    52
#define prevchange (*GET_WSD_VAR_NAME(prevchange, g)())
sl@0
    53
GET_GLOBAL_VAR_FROM_TLS(nextchange, time_t)
sl@0
    54
#define nextchange (*GET_WSD_VAR_NAME(nextchange, g)())
sl@0
    55
#endif //EMULATOR
sl@0
    56
sl@0
    57
#if !defined(__SERIES60_30__) 
sl@0
    58
TTzTimeReference FindChangeBracket(TTime& aTime ,TTime& aUTime, CVTzActualisedRules& aRule,TBool& aDstOn)
sl@0
    59
    {
sl@0
    60
    TInt cnt = aRule.Count();
sl@0
    61
    TInt lowestOffset = -1, curOffset=-1;
sl@0
    62
    TBool isInit = EFalse;
sl@0
    63
    TTime CurTime(aTime);
sl@0
    64
    TTime Previous(-1), Next(-1);
sl@0
    65
    TTzTimeReference ref(ETzWallTimeReference);
sl@0
    66
    
sl@0
    67
//datetime to be created from the UTC time.    
sl@0
    68
    TDateTime dateTime = aUTime.DateTime();
sl@0
    69
    
sl@0
    70
// Initially set aDstOn to False
sl@0
    71
    aDstOn = EFalse;
sl@0
    72
    
sl@0
    73
// Loop to find the lowest time offset applicable in the year.    
sl@0
    74
    for(TInt idx=0; idx<cnt; idx++)
sl@0
    75
        {
sl@0
    76
        TVTzActualisedRule& Actual = aRule[idx];  
sl@0
    77
        if (Actual.iTimeOfChange.DateTime().Year() == dateTime.Year())
sl@0
    78
            {
sl@0
    79
            if (!isInit)
sl@0
    80
                {
sl@0
    81
                lowestOffset = Actual.iNewOffset;
sl@0
    82
                isInit = ETrue;    
sl@0
    83
                }
sl@0
    84
sl@0
    85
            if (isInit && Actual.iNewOffset < lowestOffset)
sl@0
    86
                {
sl@0
    87
                lowestOffset = Actual.iNewOffset;
sl@0
    88
                }   
sl@0
    89
            }
sl@0
    90
        }
sl@0
    91
//    Loop to find the current offset, previous time of change and the next time of change.
sl@0
    92
    for(TInt idx=0; idx<cnt; idx++)
sl@0
    93
        {
sl@0
    94
        TVTzActualisedRule& Actual = aRule[idx];
sl@0
    95
        if(Actual.iTimeReference == ETzUtcTimeReference && CurTime != aUTime)
sl@0
    96
            {
sl@0
    97
            CurTime = aUTime;
sl@0
    98
            ref = ETzUtcTimeReference;
sl@0
    99
            }
sl@0
   100
        //  Rules are arranged in increasing order of iTimeofChange
sl@0
   101
        if(CurTime >= Actual.iTimeOfChange)
sl@0
   102
            {
sl@0
   103
            Previous = Actual.iTimeOfChange;
sl@0
   104
            curOffset = Actual.iNewOffset;
sl@0
   105
            }
sl@0
   106
        else
sl@0
   107
            {
sl@0
   108
            Next = Actual.iTimeOfChange;
sl@0
   109
            break;
sl@0
   110
            }
sl@0
   111
       }
sl@0
   112
/*
sl@0
   113
 * If the Next and Previous were not set due to some reason,
sl@0
   114
 * Set them to one year after and one year before the time passed.
sl@0
   115
 */     
sl@0
   116
    if(Next == -1)
sl@0
   117
        {
sl@0
   118
        /* Next year */
sl@0
   119
        Next = CurTime + (TTimeIntervalSeconds)(365*86400);
sl@0
   120
        }
sl@0
   121
    if (Previous == -1)
sl@0
   122
        {
sl@0
   123
        /* Prev Year */
sl@0
   124
        Previous = CurTime - (TTimeIntervalSeconds)(365*86400);
sl@0
   125
        }
sl@0
   126
    aTime = Previous;
sl@0
   127
    aUTime = Next;
sl@0
   128
    if(lowestOffset != -1 && curOffset > lowestOffset)
sl@0
   129
        {
sl@0
   130
        aDstOn = ETrue;
sl@0
   131
        }
sl@0
   132
    return ref;
sl@0
   133
    }
sl@0
   134
#endif
sl@0
   135
sl@0
   136
void UpdateDstAndTznameL(RTz &aServer,CTzId& aId,struct tm* aTmStruct, TTime aTime, TTime aUTime, TDateTime aDate, TTzTimeReference aReference)
sl@0
   137
    {
sl@0
   138
#if !defined(__SERIES60_30__) 
sl@0
   139
    CTzRules* zonerules = aServer.GetTimeZoneRulesL(aId, aTime, aTime, aReference);
sl@0
   140
    CleanupStack::PushL(zonerules);
sl@0
   141
    CVTzActualisedRules* actual = CVTzActualisedRules::NewL(aDate.Year()-1, aDate.Year()+1);
sl@0
   142
    CleanupStack::PushL(actual);
sl@0
   143
    zonerules->GetActualisedRulesL(*actual);
sl@0
   144
    TTime PrevTime(aTime), NextTime(aUTime);
sl@0
   145
    TBool IsDstOn;
sl@0
   146
    /* Send local TTimes */
sl@0
   147
    if(FindChangeBracket(PrevTime, NextTime, *actual, IsDstOn)== ETzWallTimeReference)
sl@0
   148
        {
sl@0
   149
        aServer.ConvertToUniversalTime(PrevTime,aId);
sl@0
   150
        aServer.ConvertToUniversalTime(NextTime,aId);
sl@0
   151
        }
sl@0
   152
    prevchange = (PrevTime.Int64() - KEpochTime)/1000000;
sl@0
   153
    nextchange = (NextTime.Int64() - KEpochTime)/1000000;
sl@0
   154
sl@0
   155
    aTmStruct->tm_isdst = IsDstOn;
sl@0
   156
    CleanupStack::PopAndDestroy(actual);
sl@0
   157
    CleanupStack::PopAndDestroy(zonerules);
sl@0
   158
#else // __SERIES60_30__ defined
sl@0
   159
    aServer;
sl@0
   160
    aTime;
sl@0
   161
    aUTime;
sl@0
   162
    aDate;
sl@0
   163
    aReference;
sl@0
   164
#endif
sl@0
   165
/*  //This part has been commented out because of performance issue. Also this requires capability.
sl@0
   166
    if(RProcess().HasCapability(ECapabilityReadUserData,ECapabilityWriteUserData,NULL))
sl@0
   167
        {
sl@0
   168
        //          tm_zone updated only in this case. Otherwise always has UTC.
sl@0
   169
        CTzLocalizer *localizer = CTzLocalizer::NewL();
sl@0
   170
        CleanupStack::PushL(localizer);
sl@0
   171
        CTzLocalizedTimeZone* timezone = localizer->GetLocalizedTimeZoneL(aId.TimeZoneNumericID());
sl@0
   172
        CleanupStack::PushL(timezone);
sl@0
   173
        TPtrC zoneNameDesc;
sl@0
   174
        if (aTmStruct->tm_isdst)
sl@0
   175
            {
sl@0
   176
            zoneNameDesc.Set(timezone->ShortDaylightName());
sl@0
   177
            }
sl@0
   178
        else            
sl@0
   179
            {
sl@0
   180
            zoneNameDesc.Set(timezone->ShortStandardName());
sl@0
   181
            }
sl@0
   182
        //TODO: Value should not be hardcoded here. Get it from the struct state again.
sl@0
   183
        TPtr8 ptr((unsigned char*)tzname[0],zoneNameDesc.Size());
sl@0
   184
        CnvUtfConverter::ConvertFromUnicodeToUtf8(ptr,zoneNameDesc);
sl@0
   185
        CleanupStack::PopAndDestroy(timezone);  
sl@0
   186
        CleanupStack::PopAndDestroy(localizer);
sl@0
   187
        } 
sl@0
   188
    
sl@0
   189
    aTmStruct->tm_zone = tzname[0]; */
sl@0
   190
    
sl@0
   191
    aTmStruct->tm_zone = "WILDABBR";
sl@0
   192
    }  
sl@0
   193
sl@0
   194
void ConvertUtcToLocalTimeL(const time_t* aUTCTime, struct tm* const atmStruct)
sl@0
   195
    {
sl@0
   196
    if(!aUTCTime)
sl@0
   197
        {
sl@0
   198
        User::Leave(KErrArgument);
sl@0
   199
        }
sl@0
   200
    TTime   time(KEpochTime + (*aUTCTime) * (TInt64)1000000);
sl@0
   201
    TTime   Utime(time);
sl@0
   202
sl@0
   203
    // Cache Logic.
sl@0
   204
    TTimeIntervalSeconds UtcOffset = User::UTCOffset();
sl@0
   205
    if( cachetm.tm_gmtoff == UtcOffset.Int() && prevchange <= *aUTCTime && *aUTCTime<= nextchange)
sl@0
   206
        {
sl@0
   207
        time += UtcOffset;         // Convert to local time.
sl@0
   208
        TDateTime tdt = time.DateTime();
sl@0
   209
        cachetm.tm_sec = tdt.Second();
sl@0
   210
        cachetm.tm_min = tdt.Minute();
sl@0
   211
        cachetm.tm_hour = tdt.Hour();    
sl@0
   212
        cachetm.tm_mday = tdt.Day() + 1;
sl@0
   213
        cachetm.tm_mon = tdt.Month();
sl@0
   214
        cachetm.tm_year = tdt.Year() - 1900;    // This is as per the tm structure format.  
sl@0
   215
        cachetm.tm_wday = (time.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. 
sl@0
   216
        cachetm.tm_yday = time.DayNoInYear()-1; // Note: first day of the year is considered as 1, so
sl@0
   217
        *atmStruct = cachetm;
sl@0
   218
        return;
sl@0
   219
        }
sl@0
   220
sl@0
   221
    TDateTime tdt = time.DateTime();
sl@0
   222
    
sl@0
   223
    //enable the cache
sl@0
   224
	TInt status = KErrNone;
sl@0
   225
	RTz& rtzServer = Backend()->TZServer(status);
sl@0
   226
	if(status != KErrNone)
sl@0
   227
		User::Leave(status);
sl@0
   228
sl@0
   229
    CTzConverter* ctzConverter = CTzConverter::NewL(rtzServer);
sl@0
   230
    
sl@0
   231
    CleanupStack::PushL(ctzConverter);
sl@0
   232
    if(ctzConverter->ConvertToLocalTime(time) == KErrNone)      
sl@0
   233
        {
sl@0
   234
        tdt = time.DateTime();
sl@0
   235
        atmStruct->tm_sec = tdt.Second();
sl@0
   236
        atmStruct->tm_min = tdt.Minute();
sl@0
   237
        atmStruct->tm_hour = tdt.Hour();
sl@0
   238
        atmStruct->tm_mday = tdt.Day() + 1;
sl@0
   239
        atmStruct->tm_mon = tdt.Month();
sl@0
   240
        atmStruct->tm_year = tdt.Year() - 1900;    // This is as per the tm structure format.  
sl@0
   241
        atmStruct->tm_wday = (time.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. 
sl@0
   242
        atmStruct->tm_yday = time.DayNoInYear()-1; // Note: first day of the year is considered as 1, so 
sl@0
   243
sl@0
   244
        atmStruct->tm_gmtoff = (time.Int64() - Utime.Int64())/1000000;
sl@0
   245
        
sl@0
   246
        TInt timeZoneId = ctzConverter->CurrentTzId();
sl@0
   247
        CTzId* zoneid = CTzId::NewL((TUint)timeZoneId);
sl@0
   248
        CleanupStack::PushL(zoneid);
sl@0
   249
        
sl@0
   250
        atmStruct->tm_isdst  = -1;
sl@0
   251
        UpdateDstAndTznameL(rtzServer, *zoneid, atmStruct, time, Utime, tdt, ETzWallTimeReference);
sl@0
   252
        CleanupStack::PopAndDestroy(zoneid);
sl@0
   253
        }   
sl@0
   254
    
sl@0
   255
sl@0
   256
    CleanupStack::PopAndDestroy(1);
sl@0
   257
    cachetm = *atmStruct;
sl@0
   258
    }
sl@0
   259
sl@0
   260
void ConvertLocalToUtcTimeL(time_t* aUTCTime, struct tm* const aTmStruct)
sl@0
   261
    {
sl@0
   262
    if(!aTmStruct || aTmStruct->tm_year < 0 || aTmStruct->tm_mon < 0 || aTmStruct->tm_mday < 1 || aTmStruct->tm_hour < 0 || aTmStruct->tm_min < 0 || aTmStruct->tm_sec < 0 )
sl@0
   263
        {
sl@0
   264
        User::Leave(KErrArgument);
sl@0
   265
        }
sl@0
   266
sl@0
   267
    TDateTime tdt(aTmStruct->tm_year + 1900, (TMonth)aTmStruct->tm_mon, aTmStruct->tm_mday-1, 
sl@0
   268
                    aTmStruct->tm_hour, aTmStruct->tm_min, aTmStruct->tm_sec, 0);
sl@0
   269
    TTime   time(tdt);
sl@0
   270
    TTime   oldTime(time);
sl@0
   271
sl@0
   272
//  Cache Logic 
sl@0
   273
    TTimeIntervalSeconds UtcOffset = User::UTCOffset();
sl@0
   274
    if(cachetm.tm_gmtoff == UtcOffset.Int() && cachetm.tm_year == aTmStruct->tm_year && cachetm.tm_mon == aTmStruct->tm_mon && cachetm.tm_mday == aTmStruct->tm_mday)
sl@0
   275
        {
sl@0
   276
        aTmStruct->tm_gmtoff = cachetm.tm_gmtoff;
sl@0
   277
        aTmStruct->tm_isdst = cachetm.tm_isdst;
sl@0
   278
        aTmStruct->tm_zone = cachetm.tm_zone;    
sl@0
   279
        aTmStruct->tm_wday = (time.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. 
sl@0
   280
        aTmStruct->tm_yday = time.DayNoInYear()-1; // Note: first day of the year is considered as 1, so
sl@0
   281
        time -= UtcOffset;                 // Converting to UTC time.
sl@0
   282
        TInt64 utcTimeMicroSec = time.Int64();
sl@0
   283
        TInt64 epochRefTime = utcTimeMicroSec - KEpochTime;
sl@0
   284
        (*aUTCTime) = (TInt) (epochRefTime / 1000000);
sl@0
   285
        return;
sl@0
   286
        }
sl@0
   287
sl@0
   288
	TInt status = KErrNone;
sl@0
   289
	RTz& rtzServer = Backend()->TZServer(status);
sl@0
   290
	if(status != KErrNone)
sl@0
   291
		User::Leave(status);
sl@0
   292
sl@0
   293
    CTzConverter* ctzConverter = CTzConverter::NewL(rtzServer);      
sl@0
   294
    CleanupStack::PushL(ctzConverter);
sl@0
   295
 /* Following fields are updated if successful:
sl@0
   296
  * tm_wday
sl@0
   297
  * tm_yday
sl@0
   298
  * tm_isdst
sl@0
   299
  * tm_gmtoffset
sl@0
   300
  * tm_zone ( Conditional on capabilities available)
sl@0
   301
  */
sl@0
   302
    if(ctzConverter->ConvertToUniversalTime(time) == KErrNone)
sl@0
   303
        {
sl@0
   304
        //conversion successful
sl@0
   305
        TInt64 utcTimeMicroSec = time.Int64();
sl@0
   306
        TInt64 epochRefTime = utcTimeMicroSec - KEpochTime;
sl@0
   307
        (*aUTCTime) = (TInt) (epochRefTime / 1000000);
sl@0
   308
        aTmStruct->tm_wday = (oldTime.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. 
sl@0
   309
        aTmStruct->tm_yday = oldTime.DayNoInYear()-1; // Note: first day of the year is considered as 1, so
sl@0
   310
        aTmStruct->tm_gmtoff = (oldTime.Int64() - utcTimeMicroSec)/1000000;
sl@0
   311
        
sl@0
   312
        TInt timeZoneId = ctzConverter->CurrentTzId();
sl@0
   313
        CTzId* zoneid = CTzId::NewL((TUint)timeZoneId);
sl@0
   314
        CleanupStack::PushL(zoneid);
sl@0
   315
sl@0
   316
        aTmStruct->tm_isdst  = -1;
sl@0
   317
        UpdateDstAndTznameL(rtzServer, *zoneid, aTmStruct, oldTime, time, tdt, ETzUtcTimeReference);
sl@0
   318
        CleanupStack::PopAndDestroy(zoneid);
sl@0
   319
        }
sl@0
   320
    cachetm = *aTmStruct;
sl@0
   321
    CleanupStack::PopAndDestroy(1);
sl@0
   322
    }
sl@0
   323
sl@0
   324
// converts a UTC time to local time, or, local time to UTC time.  
sl@0
   325
TInt ConvertTime(const TInt atimeConverFlg, time_t *aUTCTime, struct tm *const atmStruct)
sl@0
   326
    {
sl@0
   327
    TInt err = 0;
sl@0
   328
sl@0
   329
    CActiveScheduler* oldAs = CActiveScheduler::Current();
sl@0
   330
    RHeap* oldHeap = User::SwitchHeap(Backend()->Heap());
sl@0
   331
    CActiveScheduler* newAs = new CActiveScheduler();
sl@0
   332
    User::SwitchHeap(oldHeap);
sl@0
   333
    if(!newAs)
sl@0
   334
        {
sl@0
   335
        errno = ENOMEM;
sl@0
   336
        return ENOMEM;
sl@0
   337
        }
sl@0
   338
    if(oldAs)
sl@0
   339
        {
sl@0
   340
        (void)CActiveScheduler::Replace(newAs);
sl@0
   341
        }
sl@0
   342
    else
sl@0
   343
        {
sl@0
   344
        CActiveScheduler::Install(newAs);
sl@0
   345
        }
sl@0
   346
    
sl@0
   347
    oldHeap = User::SwitchHeap(Backend()->Heap());
sl@0
   348
    // Cleanup stack needed
sl@0
   349
    CTrapCleanup* cleanup=CTrapCleanup::New();
sl@0
   350
sl@0
   351
    if (cleanup)
sl@0
   352
        {
sl@0
   353
        if(EUtcToLocal == atimeConverFlg)
sl@0
   354
            {
sl@0
   355
            TRAP(err, ConvertUtcToLocalTimeL(aUTCTime, atmStruct));
sl@0
   356
            }
sl@0
   357
        else if(ELocalToUtc == atimeConverFlg)
sl@0
   358
            {
sl@0
   359
            TRAP(err, ConvertLocalToUtcTimeL(aUTCTime, atmStruct));
sl@0
   360
            }
sl@0
   361
        delete cleanup;
sl@0
   362
        }
sl@0
   363
    else
sl@0
   364
        {
sl@0
   365
        err = ENOMEM;
sl@0
   366
        }
sl@0
   367
    if(oldAs)
sl@0
   368
        {
sl@0
   369
        (void)CActiveScheduler::Replace(oldAs);
sl@0
   370
        }
sl@0
   371
sl@0
   372
sl@0
   373
    delete newAs;
sl@0
   374
    User::SwitchHeap(oldHeap);
sl@0
   375
    return MapError(err,errno);
sl@0
   376
    }
sl@0
   377