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:
sl@0: //
sl@0: 
sl@0: #include <e32def.h>
sl@0: #include <s32strm.h>
sl@0: #include "SCHTIME.H"
sl@0: #include "SCHEXEC.H"
sl@0: 
sl@0: /**
sl@0: UTC/Local Time flag value
sl@0: @internalComponent
sl@0: */
sl@0: const TUint32 KIsUtc = 0x00000001;
sl@0: 
sl@0: /** 
sl@0: Default constructor for TTsTime.
sl@0: This constructor initialises its member data to zero.
sl@0: By default this sets it to be UTC based.
sl@0: */ 
sl@0: EXPORT_C TTsTime::TTsTime():
sl@0: 	iUtcTime(0), 
sl@0: 	iOffset(0), 
sl@0: 	iFlags(KIsUtc)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: /**
sl@0: Copy constructor for TTsTime
sl@0: @param aTTsTime value to duplicate
sl@0: */	
sl@0: EXPORT_C TTsTime::TTsTime(const TTsTime& aTTsTime)
sl@0: 	{
sl@0: 	*this = aTTsTime;
sl@0: 	}
sl@0: 		
sl@0: /** 
sl@0: Constructs a TTsTime with a TTime. 
sl@0: If the iIsUtc is ETrue, then TTsTime is UTC based time, if iIsUtc is EFalse 
sl@0: then TTsTime is local time based. This constructor will update all the member data as appropriate.	
sl@0: @param aTime The TTime value
sl@0: @param aIsUtc is Etrue when aTime is UTC and EFalse when aTime is local time.
sl@0: */	
sl@0: EXPORT_C TTsTime::TTsTime(const TTime& aTime, TBool aIsUtc)
sl@0: 	{
sl@0: 	// if the time passed as argument is UTC based
sl@0: 	if(aIsUtc)
sl@0: 		SetUtcTime(aTime);
sl@0: 		
sl@0: 	// if the time passed is local time	
sl@0: 	else
sl@0: 		SetLocalTime(aTime);
sl@0: 	}
sl@0: 	
sl@0: /**
sl@0: Sets this object to a local time based value updating its parameters as appropriate.
sl@0: @param aLocalTime The local time to be set. 
sl@0: */
sl@0: EXPORT_C void TTsTime::SetLocalTime(const TTime& aLocalTime)
sl@0: 	{
sl@0: 	iOffset = User::UTCOffset(); // sets the current UTC offset to iOffset
sl@0: 	iUtcTime = aLocalTime - iOffset; // converts the given aLocalTime to a UTC time value
sl@0: 	iFlags &= ~KIsUtc;    //set the Bit 0 to zero for Local Time
sl@0: 	}
sl@0: 		
sl@0: /** 
sl@0: Sets this object to a UTC time based value updating its parameters as appropriate.
sl@0: @param aUtcTime The UTC time to be set
sl@0: */
sl@0: EXPORT_C void TTsTime::SetUtcTime(const TTime& aUtcTime)
sl@0: 	{
sl@0: 	iUtcTime = aUtcTime; 
sl@0: 	iOffset = 0; 
sl@0: 	iFlags |= KIsUtc;    //set the Bit 0 to one as TTsTime is UTC
sl@0: 	}
sl@0: 		
sl@0: /** 
sl@0: This function returns a home time value.
sl@0: @return Retrieves time from object in local time
sl@0: */
sl@0: EXPORT_C const TTime TTsTime::GetLocalTime()
sl@0: 	{
sl@0: 	return DetermineLocalTime();
sl@0: 	}
sl@0: 	
sl@0: /** 
sl@0: This function returns a home time value.
sl@0: @return Retrieves time from object in local time
sl@0: */
sl@0: EXPORT_C TTime TTsTime::GetLocalTime() const
sl@0: 	{
sl@0: 	return DetermineLocalTime();
sl@0: 	}	
sl@0: 		
sl@0: /**
sl@0: This function returns a UTC value.
sl@0: @return Returns the UTC time value.
sl@0: */ 
sl@0: EXPORT_C const TTime& TTsTime::GetUtcTime()
sl@0: 	{
sl@0: 	return iUtcTime;
sl@0: 	}
sl@0: 	
sl@0: /**
sl@0: This function returns a UTC value.
sl@0: @return Returns the UTC time value.
sl@0: */ 
sl@0: EXPORT_C const TTime& TTsTime::GetUtcTime() const
sl@0: 	{
sl@0: 	return iUtcTime;
sl@0: 	}
sl@0: 	
sl@0: /** 
sl@0: @return ETrue, if TTsTime object is UTC and EFalse if TTsTime object is local time
sl@0: */
sl@0: EXPORT_C TBool TTsTime::IsUtc() const
sl@0: 	{
sl@0: 	return iFlags & KIsUtc ? ETrue: EFalse;
sl@0: 	}
sl@0: 
sl@0: 	
sl@0: /**
sl@0: This function returns a home time value.
sl@0: If UTC based returns UTC time + User::UTCOffset(). If local time based returns
sl@0: local time of the object.
sl@0: @internalComponent
sl@0: */
sl@0: TTime TTsTime::DetermineLocalTime() const
sl@0: 	{
sl@0: 	TTime localTime;
sl@0: 	if(IsUtc())
sl@0: 		localTime = iUtcTime+User::UTCOffset();
sl@0: 	else
sl@0: 		localTime = iUtcTime+iOffset;
sl@0: 	
sl@0: 	return localTime;
sl@0: 	}
sl@0: 	
sl@0: /** 
sl@0: This class does not explicitly update iOffSet if the system TimeZone/DST offset changes.
sl@0: When called this API will update the object if the system TimeZone/DST offset has changed. 
sl@0: Keeping the offset in objects of this class correct is the responsibility of the 
sl@0: Task Scheduler server.This applies to local time objects only. UTC objects will be unaffected.
sl@0: @internalComponent
sl@0: */	
sl@0: void TTsTime::ProcessOffsetEvent() 
sl@0: 	{
sl@0: 	if(iFlags & KIsUtc)
sl@0: 		return;
sl@0: 			
sl@0: 	TTimeIntervalSeconds kernelOffset = User::UTCOffset();
sl@0: 	
sl@0: 	// check if there was an offset event happened without TTsTime being updated
sl@0: 	// localTime will always be the addition of UTC time and kernel offset
sl@0: 	if (iOffset != kernelOffset)
sl@0: 		{	
sl@0: 		// If TTsTime is home time and there was a time zone change then TTsTime needs to be update.
sl@0: 		// If not then TTsTime remains the same, as UTC times are the same everywhere.
sl@0: 		iUtcTime = (iUtcTime+iOffset) - kernelOffset; 
sl@0: 		iOffset = kernelOffset; 
sl@0: 		}
sl@0: 	}
sl@0: /** 
sl@0: Overloaded assignment operator for TTsTime
sl@0: @param aTsTime time value to copy
sl@0: */
sl@0: EXPORT_C TTsTime& TTsTime::operator=(const TTsTime& aTsTime)
sl@0: 	{
sl@0: 	Mem::Copy(this,&aTsTime,sizeof(*this));
sl@0: 	return *this;	
sl@0: 	}
sl@0: 	
sl@0: /** 
sl@0: Uses the standard template operator>>() to internalize the object from aStream.
sl@0: Additionally this method will ensure that the object is updated in the event that 
sl@0: the system TimeZone/DST offset changed since it was externalised.
sl@0: @internalComponent
sl@0: */
sl@0: void TTsTime::InternalizeL(RReadStream& aStream)  
sl@0: 	{
sl@0: 	TInt32 offset;
sl@0: 	TInt64 utcTime;
sl@0: 	
sl@0: 	// Updates TTsTime if local time based and the system Timezone/DST change occurred.
sl@0: 	ProcessOffsetEvent();
sl@0: 				
sl@0: 	aStream >> utcTime;
sl@0: 	iUtcTime = TTime(utcTime);
sl@0: 	aStream >> offset;
sl@0: 	iOffset = offset;
sl@0: 	aStream >> iFlags;	
sl@0: 	}
sl@0: 	
sl@0: /** 
sl@0: Uses the standard template operator>>() to externalize the object to aStream
sl@0: @internalComponent
sl@0: */
sl@0: void TTsTime::ExternalizeL(RWriteStream& aStream) const
sl@0: 	{
sl@0: 	aStream << iUtcTime.Int64();
sl@0: 	aStream.WriteInt32L(iOffset.Int());
sl@0: 	aStream.WriteUint32L(iFlags);
sl@0: 	}