sl@0: /* sl@0: * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: This is a project specific include file for building the sl@0: * clock related functions as part of libc library. sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "tzlocalizer.h" sl@0: #include "tzlocalizationdatatypes.h" sl@0: #include "vtzrules.h" sl@0: #include "utf.h" sl@0: #include "timefuncs.h" sl@0: #include "lposix.h" sl@0: #include "sysif.h" sl@0: #include "libc_wsd_defs.h" sl@0: sl@0: // This represents the base EpochTime in microseconds, sl@0: // which happens to be January 1, 1970 (midnight UTC/GMT), sl@0: // not counting leap seconds. This effectively represents sl@0: // the number of microseconds in 1970 years. sl@0: static const TInt64 KEpochTime = 62168256000000000; sl@0: sl@0: sl@0: sl@0: #ifndef EMULATOR sl@0: struct tm cachetm; sl@0: time_t prevchange; sl@0: time_t nextchange; sl@0: #else //EMULATOR sl@0: sl@0: sl@0: sl@0: GET_GLOBAL_VAR_FROM_TLS(cachetm, struct tm) sl@0: #define cachetm (*GET_WSD_VAR_NAME(cachetm, g)()) sl@0: GET_GLOBAL_VAR_FROM_TLS(prevchange, time_t) sl@0: #define prevchange (*GET_WSD_VAR_NAME(prevchange, g)()) sl@0: GET_GLOBAL_VAR_FROM_TLS(nextchange, time_t) sl@0: #define nextchange (*GET_WSD_VAR_NAME(nextchange, g)()) sl@0: #endif //EMULATOR sl@0: sl@0: #if !defined(__SERIES60_30__) sl@0: TTzTimeReference FindChangeBracket(TTime& aTime ,TTime& aUTime, CVTzActualisedRules& aRule,TBool& aDstOn) sl@0: { sl@0: TInt cnt = aRule.Count(); sl@0: TInt lowestOffset = -1, curOffset=-1; sl@0: TBool isInit = EFalse; sl@0: TTime CurTime(aTime); sl@0: TTime Previous(-1), Next(-1); sl@0: TTzTimeReference ref(ETzWallTimeReference); sl@0: sl@0: //datetime to be created from the UTC time. sl@0: TDateTime dateTime = aUTime.DateTime(); sl@0: sl@0: // Initially set aDstOn to False sl@0: aDstOn = EFalse; sl@0: sl@0: // Loop to find the lowest time offset applicable in the year. sl@0: for(TInt idx=0; idx= Actual.iTimeOfChange) sl@0: { sl@0: Previous = Actual.iTimeOfChange; sl@0: curOffset = Actual.iNewOffset; sl@0: } sl@0: else sl@0: { sl@0: Next = Actual.iTimeOfChange; sl@0: break; sl@0: } sl@0: } sl@0: /* sl@0: * If the Next and Previous were not set due to some reason, sl@0: * Set them to one year after and one year before the time passed. sl@0: */ sl@0: if(Next == -1) sl@0: { sl@0: /* Next year */ sl@0: Next = CurTime + (TTimeIntervalSeconds)(365*86400); sl@0: } sl@0: if (Previous == -1) sl@0: { sl@0: /* Prev Year */ sl@0: Previous = CurTime - (TTimeIntervalSeconds)(365*86400); sl@0: } sl@0: aTime = Previous; sl@0: aUTime = Next; sl@0: if(lowestOffset != -1 && curOffset > lowestOffset) sl@0: { sl@0: aDstOn = ETrue; sl@0: } sl@0: return ref; sl@0: } sl@0: #endif sl@0: sl@0: void UpdateDstAndTznameL(RTz &aServer,CTzId& aId,struct tm* aTmStruct, TTime aTime, TTime aUTime, TDateTime aDate, TTzTimeReference aReference) sl@0: { sl@0: #if !defined(__SERIES60_30__) sl@0: CTzRules* zonerules = aServer.GetTimeZoneRulesL(aId, aTime, aTime, aReference); sl@0: CleanupStack::PushL(zonerules); sl@0: CVTzActualisedRules* actual = CVTzActualisedRules::NewL(aDate.Year()-1, aDate.Year()+1); sl@0: CleanupStack::PushL(actual); sl@0: zonerules->GetActualisedRulesL(*actual); sl@0: TTime PrevTime(aTime), NextTime(aUTime); sl@0: TBool IsDstOn; sl@0: /* Send local TTimes */ sl@0: if(FindChangeBracket(PrevTime, NextTime, *actual, IsDstOn)== ETzWallTimeReference) sl@0: { sl@0: aServer.ConvertToUniversalTime(PrevTime,aId); sl@0: aServer.ConvertToUniversalTime(NextTime,aId); sl@0: } sl@0: prevchange = (PrevTime.Int64() - KEpochTime)/1000000; sl@0: nextchange = (NextTime.Int64() - KEpochTime)/1000000; sl@0: sl@0: aTmStruct->tm_isdst = IsDstOn; sl@0: CleanupStack::PopAndDestroy(actual); sl@0: CleanupStack::PopAndDestroy(zonerules); sl@0: #else // __SERIES60_30__ defined sl@0: aServer; sl@0: aTime; sl@0: aUTime; sl@0: aDate; sl@0: aReference; sl@0: #endif sl@0: /* //This part has been commented out because of performance issue. Also this requires capability. sl@0: if(RProcess().HasCapability(ECapabilityReadUserData,ECapabilityWriteUserData,NULL)) sl@0: { sl@0: // tm_zone updated only in this case. Otherwise always has UTC. sl@0: CTzLocalizer *localizer = CTzLocalizer::NewL(); sl@0: CleanupStack::PushL(localizer); sl@0: CTzLocalizedTimeZone* timezone = localizer->GetLocalizedTimeZoneL(aId.TimeZoneNumericID()); sl@0: CleanupStack::PushL(timezone); sl@0: TPtrC zoneNameDesc; sl@0: if (aTmStruct->tm_isdst) sl@0: { sl@0: zoneNameDesc.Set(timezone->ShortDaylightName()); sl@0: } sl@0: else sl@0: { sl@0: zoneNameDesc.Set(timezone->ShortStandardName()); sl@0: } sl@0: //TODO: Value should not be hardcoded here. Get it from the struct state again. sl@0: TPtr8 ptr((unsigned char*)tzname[0],zoneNameDesc.Size()); sl@0: CnvUtfConverter::ConvertFromUnicodeToUtf8(ptr,zoneNameDesc); sl@0: CleanupStack::PopAndDestroy(timezone); sl@0: CleanupStack::PopAndDestroy(localizer); sl@0: } sl@0: sl@0: aTmStruct->tm_zone = tzname[0]; */ sl@0: sl@0: aTmStruct->tm_zone = "WILDABBR"; sl@0: } sl@0: sl@0: void ConvertUtcToLocalTimeL(const time_t* aUTCTime, struct tm* const atmStruct) sl@0: { sl@0: if(!aUTCTime) sl@0: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: TTime time(KEpochTime + (*aUTCTime) * (TInt64)1000000); sl@0: TTime Utime(time); sl@0: sl@0: // Cache Logic. sl@0: TTimeIntervalSeconds UtcOffset = User::UTCOffset(); sl@0: if( cachetm.tm_gmtoff == UtcOffset.Int() && prevchange <= *aUTCTime && *aUTCTime<= nextchange) sl@0: { sl@0: time += UtcOffset; // Convert to local time. sl@0: TDateTime tdt = time.DateTime(); sl@0: cachetm.tm_sec = tdt.Second(); sl@0: cachetm.tm_min = tdt.Minute(); sl@0: cachetm.tm_hour = tdt.Hour(); sl@0: cachetm.tm_mday = tdt.Day() + 1; sl@0: cachetm.tm_mon = tdt.Month(); sl@0: cachetm.tm_year = tdt.Year() - 1900; // This is as per the tm structure format. sl@0: cachetm.tm_wday = (time.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. sl@0: cachetm.tm_yday = time.DayNoInYear()-1; // Note: first day of the year is considered as 1, so sl@0: *atmStruct = cachetm; sl@0: return; sl@0: } sl@0: sl@0: TDateTime tdt = time.DateTime(); sl@0: sl@0: //enable the cache sl@0: TInt status = KErrNone; sl@0: RTz& rtzServer = Backend()->TZServer(status); sl@0: if(status != KErrNone) sl@0: User::Leave(status); sl@0: sl@0: CTzConverter* ctzConverter = CTzConverter::NewL(rtzServer); sl@0: sl@0: CleanupStack::PushL(ctzConverter); sl@0: if(ctzConverter->ConvertToLocalTime(time) == KErrNone) sl@0: { sl@0: tdt = time.DateTime(); sl@0: atmStruct->tm_sec = tdt.Second(); sl@0: atmStruct->tm_min = tdt.Minute(); sl@0: atmStruct->tm_hour = tdt.Hour(); sl@0: atmStruct->tm_mday = tdt.Day() + 1; sl@0: atmStruct->tm_mon = tdt.Month(); sl@0: atmStruct->tm_year = tdt.Year() - 1900; // This is as per the tm structure format. sl@0: atmStruct->tm_wday = (time.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. sl@0: atmStruct->tm_yday = time.DayNoInYear()-1; // Note: first day of the year is considered as 1, so sl@0: sl@0: atmStruct->tm_gmtoff = (time.Int64() - Utime.Int64())/1000000; sl@0: sl@0: TInt timeZoneId = ctzConverter->CurrentTzId(); sl@0: CTzId* zoneid = CTzId::NewL((TUint)timeZoneId); sl@0: CleanupStack::PushL(zoneid); sl@0: sl@0: atmStruct->tm_isdst = -1; sl@0: UpdateDstAndTznameL(rtzServer, *zoneid, atmStruct, time, Utime, tdt, ETzWallTimeReference); sl@0: CleanupStack::PopAndDestroy(zoneid); sl@0: } sl@0: sl@0: sl@0: CleanupStack::PopAndDestroy(1); sl@0: cachetm = *atmStruct; sl@0: } sl@0: sl@0: void ConvertLocalToUtcTimeL(time_t* aUTCTime, struct tm* const aTmStruct) sl@0: { sl@0: 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: { sl@0: User::Leave(KErrArgument); sl@0: } sl@0: sl@0: TDateTime tdt(aTmStruct->tm_year + 1900, (TMonth)aTmStruct->tm_mon, aTmStruct->tm_mday-1, sl@0: aTmStruct->tm_hour, aTmStruct->tm_min, aTmStruct->tm_sec, 0); sl@0: TTime time(tdt); sl@0: TTime oldTime(time); sl@0: sl@0: // Cache Logic sl@0: TTimeIntervalSeconds UtcOffset = User::UTCOffset(); sl@0: 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: { sl@0: aTmStruct->tm_gmtoff = cachetm.tm_gmtoff; sl@0: aTmStruct->tm_isdst = cachetm.tm_isdst; sl@0: aTmStruct->tm_zone = cachetm.tm_zone; sl@0: aTmStruct->tm_wday = (time.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. sl@0: aTmStruct->tm_yday = time.DayNoInYear()-1; // Note: first day of the year is considered as 1, so sl@0: time -= UtcOffset; // Converting to UTC time. sl@0: TInt64 utcTimeMicroSec = time.Int64(); sl@0: TInt64 epochRefTime = utcTimeMicroSec - KEpochTime; sl@0: (*aUTCTime) = (TInt) (epochRefTime / 1000000); sl@0: return; sl@0: } sl@0: sl@0: TInt status = KErrNone; sl@0: RTz& rtzServer = Backend()->TZServer(status); sl@0: if(status != KErrNone) sl@0: User::Leave(status); sl@0: sl@0: CTzConverter* ctzConverter = CTzConverter::NewL(rtzServer); sl@0: CleanupStack::PushL(ctzConverter); sl@0: /* Following fields are updated if successful: sl@0: * tm_wday sl@0: * tm_yday sl@0: * tm_isdst sl@0: * tm_gmtoffset sl@0: * tm_zone ( Conditional on capabilities available) sl@0: */ sl@0: if(ctzConverter->ConvertToUniversalTime(time) == KErrNone) sl@0: { sl@0: //conversion successful sl@0: TInt64 utcTimeMicroSec = time.Int64(); sl@0: TInt64 epochRefTime = utcTimeMicroSec - KEpochTime; sl@0: (*aUTCTime) = (TInt) (epochRefTime / 1000000); sl@0: aTmStruct->tm_wday = (oldTime.DayNoInWeek()+1)%7; // Note: first day of the week is considered as Monday, so adding +1. sl@0: aTmStruct->tm_yday = oldTime.DayNoInYear()-1; // Note: first day of the year is considered as 1, so sl@0: aTmStruct->tm_gmtoff = (oldTime.Int64() - utcTimeMicroSec)/1000000; sl@0: sl@0: TInt timeZoneId = ctzConverter->CurrentTzId(); sl@0: CTzId* zoneid = CTzId::NewL((TUint)timeZoneId); sl@0: CleanupStack::PushL(zoneid); sl@0: sl@0: aTmStruct->tm_isdst = -1; sl@0: UpdateDstAndTznameL(rtzServer, *zoneid, aTmStruct, oldTime, time, tdt, ETzUtcTimeReference); sl@0: CleanupStack::PopAndDestroy(zoneid); sl@0: } sl@0: cachetm = *aTmStruct; sl@0: CleanupStack::PopAndDestroy(1); sl@0: } sl@0: sl@0: // converts a UTC time to local time, or, local time to UTC time. sl@0: TInt ConvertTime(const TInt atimeConverFlg, time_t *aUTCTime, struct tm *const atmStruct) sl@0: { sl@0: TInt err = 0; sl@0: sl@0: CActiveScheduler* oldAs = CActiveScheduler::Current(); sl@0: RHeap* oldHeap = User::SwitchHeap(Backend()->Heap()); sl@0: CActiveScheduler* newAs = new CActiveScheduler(); sl@0: User::SwitchHeap(oldHeap); sl@0: if(!newAs) sl@0: { sl@0: errno = ENOMEM; sl@0: return ENOMEM; sl@0: } sl@0: if(oldAs) sl@0: { sl@0: (void)CActiveScheduler::Replace(newAs); sl@0: } sl@0: else sl@0: { sl@0: CActiveScheduler::Install(newAs); sl@0: } sl@0: sl@0: oldHeap = User::SwitchHeap(Backend()->Heap()); sl@0: // Cleanup stack needed sl@0: CTrapCleanup* cleanup=CTrapCleanup::New(); sl@0: sl@0: if (cleanup) sl@0: { sl@0: if(EUtcToLocal == atimeConverFlg) sl@0: { sl@0: TRAP(err, ConvertUtcToLocalTimeL(aUTCTime, atmStruct)); sl@0: } sl@0: else if(ELocalToUtc == atimeConverFlg) sl@0: { sl@0: TRAP(err, ConvertLocalToUtcTimeL(aUTCTime, atmStruct)); sl@0: } sl@0: delete cleanup; sl@0: } sl@0: else sl@0: { sl@0: err = ENOMEM; sl@0: } sl@0: if(oldAs) sl@0: { sl@0: (void)CActiveScheduler::Replace(oldAs); sl@0: } sl@0: sl@0: sl@0: delete newAs; sl@0: User::SwitchHeap(oldHeap); sl@0: return MapError(err,errno); sl@0: } sl@0: