installationservices/switestfw/test/sntpclient/util.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "util.h"
       
    20 #include <centralrepository.h>
       
    21 
       
    22 _LIT(KDaylightSavingsMinFormat, "%F%Y0224:");
       
    23 _LIT(KDaylightSavingsMaxFormat, "%F%Y0924:");
       
    24 
       
    25 void Util::SetAppropriateTimezoneL()
       
    26 	{
       
    27 	TUid repUid = {0x1020e4d3};
       
    28 	CRepository* rep = CRepository::NewLC(repUid);
       
    29 	
       
    30 	// Set the date format to European
       
    31 	User::LeaveIfError(rep->StartTransaction(CRepository::EConcurrentReadWriteTransaction));
       
    32 	User::LeaveIfError(rep->Set(101, EDateEuropean)); // 101 is the date format reg entry
       
    33 	TUint32 keys(0);
       
    34 	User::LeaveIfError(rep->CommitTransaction(keys));
       
    35 	
       
    36 	CleanupStack::PopAndDestroy(rep);
       
    37 	
       
    38 	TExtendedLocale locale;
       
    39 	locale.LoadSystemSettings();
       
    40 	locale.GetLocale()->SetDateFormat(EDateEuropean);
       
    41 	User::LeaveIfError(locale.SaveSystemSettings());
       
    42 	}
       
    43 
       
    44 TBool Util::DaylightSavingsAppliesL(const TTime& utc)
       
    45 	{
       
    46 	
       
    47 	// This algorithm needs the first day of the week to be monday
       
    48 	
       
    49 	TDay oldStart;
       
    50 	
       
    51 	TLocale set;
       
    52 	oldStart = set.StartOfWeek();
       
    53 	set.SetStartOfWeek(EMonday);
       
    54 	set.Set();
       
    55 	
       
    56 	TBuf<9> min;
       
    57 	TBuf<9> max;
       
    58 	
       
    59 	utc.FormatL(min, KDaylightSavingsMinFormat);
       
    60 	utc.FormatL(max, KDaylightSavingsMaxFormat);
       
    61 	
       
    62 	// Get times representing the first/last possible day of this 
       
    63 	// year that daylight savings time change could change on
       
    64 	
       
    65 	TTime timeMin;
       
    66 	User::LeaveIfError(timeMin.Set(min));
       
    67 	TTime timeMax;
       
    68 	User::LeaveIfError(timeMax.Set(max));
       
    69 
       
    70 	// Find the last sunday in the respective months
       
    71 	
       
    72 	TTimeIntervalDays addMin(6 - timeMin.DayNoInWeek());
       
    73 	TTimeIntervalDays addMax(6 - timeMax.DayNoInWeek());
       
    74 	
       
    75 	timeMin += addMin;
       
    76 	timeMax += addMax;
       
    77 	
       
    78 	// The change happens at 1AM.
       
    79 	TTimeIntervalHours hour(1);
       
    80 	timeMin += hour;
       
    81 	timeMax += hour;
       
    82 	
       
    83 	// Now we know which day the change occurs on.
       
    84 	// Compare it to what the UTC is.
       
    85 
       
    86 	TBool result = ((timeMin <= utc) && (timeMax > utc));
       
    87 	
       
    88 	// reset the first week day
       
    89 	set.SetStartOfWeek(oldStart);
       
    90 	set.Set();
       
    91 	
       
    92 	return result;
       
    93 	
       
    94 	}