Added struct tm and implemented a gettimeofday stub.

Note that gettimeofday calls uptime() and has no idea what the time was when
the system booted.
This commit is contained in:
Jonas 'Sortie' Termansen 2012-05-28 22:51:20 +02:00
parent 341bd73cb0
commit 8ae9f6bd79
2 changed files with 25 additions and 9 deletions

View File

@ -1,6 +1,6 @@
/****************************************************************************** /******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
This file is part of LibMaxsi. This file is part of LibMaxsi.
@ -20,7 +20,7 @@
time.h time.h
Time declarations. Time declarations.
******************************************************************************/ *******************************************************************************/
#ifndef _TIME_H #ifndef _TIME_H
#define _TIME_H 1 #define _TIME_H 1
@ -32,6 +32,18 @@ __BEGIN_DECLS
@include(clock_t.h) @include(clock_t.h)
@include(time_t.h) @include(time_t.h)
struct tm
{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_isdst;
};
clock_t clock(void); clock_t clock(void);
time_t time(time_t* t); time_t time(time_t* t);
char* ctime(const time_t* timep); char* ctime(const time_t* timep);

View File

@ -1,6 +1,6 @@
/****************************************************************************** /******************************************************************************
COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011. COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2011, 2012.
This file is part of LibMaxsi. This file is part of LibMaxsi.
@ -20,7 +20,7 @@
time.cpp time.cpp
Useful time functions. Useful time functions.
******************************************************************************/ *******************************************************************************/
#include <libmaxsi/platform.h> #include <libmaxsi/platform.h>
#include <libmaxsi/string.h> #include <libmaxsi/string.h>
@ -49,18 +49,22 @@ namespace Maxsi
extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/) extern "C" int gettimeofday(struct timeval* tp, void* /*tzp*/)
{ {
tp->tv_sec = 0; uintmax_t sinceboot;
tp->tv_usec = 0; uptime(&sinceboot);
tp->tv_sec = sinceboot / 1000000ULL;
tp->tv_usec = sinceboot % 1000000ULL;
return 0; return 0;
} }
extern "C" time_t time(time_t* t) extern "C" time_t time(time_t* t)
{ {
*t = 0; struct timeval tv;
return 0; gettimeofday(&tv, NULL);
time_t result = tv.tv_sec;
return t ? *t = result : result;
} }
extern "C" char* ctime(const time_t* timep) extern "C" char* ctime(const time_t* /*timep*/)
{ {
return (char*) "ctime(3) is not implemented"; return (char*) "ctime(3) is not implemented";
} }