os/ossrv/genericopenlibs/openenvcore/libc/src/gettimeofday.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// TIME.CPP
sl@0
    15
// Mapping between EPOC32 time and libc time
sl@0
    16
// The basic philosophy is to work in time_t units (TInt) which
sl@0
    17
// will be essentially TTimeIntervalSeconds from the start of Unix time.
sl@0
    18
// To stay compliant with the C-Standard (with reference to C99 draft), we  
sl@0
    19
// set the meaning of time_t to be Universal time.
sl@0
    20
// 
sl@0
    21
//
sl@0
    22
sl@0
    23
#include <e32std.h>
sl@0
    24
#include <sys/time.h> // struct timeval, timezone
sl@0
    25
sl@0
    26
#define UNIX_BASE   TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000))    // 00:00, Jan 1st 1970
sl@0
    27
sl@0
    28
// external interface for the C library
sl@0
    29
sl@0
    30
extern "C" {
sl@0
    31
sl@0
    32
/*
sl@0
    33
Intended Usage:	The gettimeofday function obtains the current UTC time, which is 
sl@0
    34
				expressed as seconds and microseconds since 00:00:00 Coordinated Universal Time (UTC), 
sl@0
    35
				January 1, 1970, and stores it in a timeval structure.
sl@0
    36
				Please note that tz_minuteswest includes daytime saving. The struct member tz_dsttime is no 
sl@0
    37
				longer supported by Symbian OS and therefore set to Zero.
sl@0
    38
sl@0
    39
*/
sl@0
    40
EXPORT_C 
sl@0
    41
int gettimeofday (struct timeval *tp, struct timezone *tzp)
sl@0
    42
    {
sl@0
    43
    if (tp)
sl@0
    44
	{
sl@0
    45
	TTime t;
sl@0
    46
	t.UniversalTime();
sl@0
    47
	
sl@0
    48
	TTimeIntervalSeconds sec;
sl@0
    49
	TInt err = t.SecondsFrom(UNIX_BASE, sec);
sl@0
    50
	if (err)
sl@0
    51
	    return -1;
sl@0
    52
	else
sl@0
    53
	    tp->tv_sec = sec.Int();
sl@0
    54
	t -= sec;
sl@0
    55
	TTimeIntervalMicroSeconds usec = t.MicroSecondsFrom(UNIX_BASE);
sl@0
    56
	TInt64 hackyfix = usec.Int64();	// because GetTInt() isn't declared const
sl@0
    57
	tp->tv_usec = I64INT(hackyfix);
sl@0
    58
	}
sl@0
    59
    if (tzp)
sl@0
    60
	{
sl@0
    61
	    tzp->tz_minuteswest = (User::UTCOffset().Int())/60;
sl@0
    62
	    tzp->tz_dsttime = 0;
sl@0
    63
	}
sl@0
    64
    return 0;
sl@0
    65
    }
sl@0
    66
sl@0
    67
} // extern "C"