os/security/cryptomgmtlibs/securitytestfw/test/sntpclient/util.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) 2007-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 the License "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: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "util.h"
sl@0
    20
#include <centralrepository.h>
sl@0
    21
sl@0
    22
_LIT(KDaylightSavingsMinFormat, "%F%Y0224:");
sl@0
    23
_LIT(KDaylightSavingsMaxFormat, "%F%Y0924:");
sl@0
    24
sl@0
    25
void Util::SetAppropriateTimezoneL()
sl@0
    26
	{
sl@0
    27
	TUid repUid = {0x1020e4d3};
sl@0
    28
	CRepository* rep = CRepository::NewLC(repUid);
sl@0
    29
	
sl@0
    30
	// Set the date format to European
sl@0
    31
	User::LeaveIfError(rep->StartTransaction(CRepository::EConcurrentReadWriteTransaction));
sl@0
    32
	User::LeaveIfError(rep->Set(101, EDateEuropean)); // 101 is the date format reg entry
sl@0
    33
	TUint32 keys(0);
sl@0
    34
	User::LeaveIfError(rep->CommitTransaction(keys));
sl@0
    35
	
sl@0
    36
	CleanupStack::PopAndDestroy(rep);
sl@0
    37
	
sl@0
    38
	TExtendedLocale locale;
sl@0
    39
	locale.LoadSystemSettings();
sl@0
    40
	locale.GetLocale()->SetDateFormat(EDateEuropean);
sl@0
    41
	User::LeaveIfError(locale.SaveSystemSettings());
sl@0
    42
	}
sl@0
    43
sl@0
    44
TBool Util::DaylightSavingsAppliesL(const TTime& utc)
sl@0
    45
	{
sl@0
    46
	
sl@0
    47
	// This algorithm needs the first day of the week to be monday
sl@0
    48
	
sl@0
    49
	TDay oldStart;
sl@0
    50
	
sl@0
    51
	TLocale set;
sl@0
    52
	oldStart = set.StartOfWeek();
sl@0
    53
	set.SetStartOfWeek(EMonday);
sl@0
    54
	set.Set();
sl@0
    55
	
sl@0
    56
	TBuf<9> min;
sl@0
    57
	TBuf<9> max;
sl@0
    58
	
sl@0
    59
	utc.FormatL(min, KDaylightSavingsMinFormat);
sl@0
    60
	utc.FormatL(max, KDaylightSavingsMaxFormat);
sl@0
    61
	
sl@0
    62
	// Get times representing the first/last possible day of this 
sl@0
    63
	// year that daylight savings time change could change on
sl@0
    64
	
sl@0
    65
	TTime timeMin;
sl@0
    66
	User::LeaveIfError(timeMin.Set(min));
sl@0
    67
	TTime timeMax;
sl@0
    68
	User::LeaveIfError(timeMax.Set(max));
sl@0
    69
sl@0
    70
	// Find the last sunday in the respective months
sl@0
    71
	
sl@0
    72
	TTimeIntervalDays addMin(6 - timeMin.DayNoInWeek());
sl@0
    73
	TTimeIntervalDays addMax(6 - timeMax.DayNoInWeek());
sl@0
    74
	
sl@0
    75
	timeMin += addMin;
sl@0
    76
	timeMax += addMax;
sl@0
    77
	
sl@0
    78
	// The change happens at 1AM.
sl@0
    79
	TTimeIntervalHours hour(1);
sl@0
    80
	timeMin += hour;
sl@0
    81
	timeMax += hour;
sl@0
    82
	
sl@0
    83
	// Now we know which day the change occurs on.
sl@0
    84
	// Compare it to what the UTC is.
sl@0
    85
sl@0
    86
	TBool result = ((timeMin <= utc) && (timeMax > utc));
sl@0
    87
	
sl@0
    88
	// reset the first week day
sl@0
    89
	set.SetStartOfWeek(oldStart);
sl@0
    90
	set.Set();
sl@0
    91
	
sl@0
    92
	return result;
sl@0
    93
	
sl@0
    94
	}