From 0e5999efae2204f680521fea7e6662fe29a3b94e Mon Sep 17 00:00:00 2001 From: "sgjesse@chromium.org" Date: Thu, 26 Mar 2009 09:28:27 +0000 Subject: [PATCH] Move time related functions to POSIX platform file. The functions getrusage, gettimeofday and localtime are in POSIX.1-2001. Review URL: http://codereview.chromium.org/53045 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@1616 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/platform-freebsd.cc | 50 ------------------------------------------- src/platform-linux.cc | 50 ------------------------------------------- src/platform-macos.cc | 50 ------------------------------------------- src/platform-posix.cc | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 150 deletions(-) diff --git a/src/platform-freebsd.cc b/src/platform-freebsd.cc index b6e2558..ee3310b 100644 --- a/src/platform-freebsd.cc +++ b/src/platform-freebsd.cc @@ -83,56 +83,6 @@ void OS::Setup() { } -int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { - struct rusage usage; - - if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; - *secs = usage.ru_utime.tv_sec; - *usecs = usage.ru_utime.tv_usec; - return 0; -} - - -double OS::TimeCurrentMillis() { - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) return 0.0; - return (static_cast(tv.tv_sec) * 1000) + - (static_cast(tv.tv_usec) / 1000); -} - - -int64_t OS::Ticks() { - // FreeBSD's gettimeofday has microsecond resolution. - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) - return 0; - return (static_cast(tv.tv_sec) * 1000000) + tv.tv_usec; -} - - -char* OS::LocalTimezone(double time) { - time_t tv = static_cast(floor(time/msPerSecond)); - struct tm* t = localtime(&tv); - return const_cast(t->tm_zone); -} - - -double OS::DaylightSavingsOffset(double time) { - time_t tv = static_cast(floor(time/msPerSecond)); - struct tm* t = localtime(&tv); - return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; -} - - -double OS::LocalTimeOffset() { - time_t tv = time(NULL); - struct tm* t = localtime(&tv); - // tm_gmtoff includes any daylight savings offset, so subtract it. - return static_cast(t->tm_gmtoff * msPerSecond - - (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); -} - - FILE* OS::FOpen(const char* path, const char* mode) { return fopen(path, mode); } diff --git a/src/platform-linux.cc b/src/platform-linux.cc index 88b28c5..a834667 100644 --- a/src/platform-linux.cc +++ b/src/platform-linux.cc @@ -81,56 +81,6 @@ void OS::Setup() { } -int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { - struct rusage usage; - - if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; - *secs = usage.ru_utime.tv_sec; - *usecs = usage.ru_utime.tv_usec; - return 0; -} - - -double OS::TimeCurrentMillis() { - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) return 0.0; - return (static_cast(tv.tv_sec) * 1000) + - (static_cast(tv.tv_usec) / 1000); -} - - -int64_t OS::Ticks() { - // Linux's gettimeofday has microsecond resolution. - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) - return 0; - return (static_cast(tv.tv_sec) * 1000000) + tv.tv_usec; -} - - -char* OS::LocalTimezone(double time) { - time_t tv = static_cast(floor(time/msPerSecond)); - struct tm* t = localtime(&tv); - return const_cast(t->tm_zone); -} - - -double OS::DaylightSavingsOffset(double time) { - time_t tv = static_cast(floor(time/msPerSecond)); - struct tm* t = localtime(&tv); - return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; -} - - -double OS::LocalTimeOffset() { - time_t tv = time(NULL); - struct tm* t = localtime(&tv); - // tm_gmtoff includes any daylight savings offset, so subtract it. - return static_cast(t->tm_gmtoff * msPerSecond - - (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); -} - - FILE* OS::FOpen(const char* path, const char* mode) { return fopen(path, mode); } diff --git a/src/platform-macos.cc b/src/platform-macos.cc index a9a9cdb..0b40e0d 100644 --- a/src/platform-macos.cc +++ b/src/platform-macos.cc @@ -86,56 +86,6 @@ void OS::Setup() { } -int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { - struct rusage usage; - - if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; - *secs = usage.ru_utime.tv_sec; - *usecs = usage.ru_utime.tv_usec; - return 0; -} - - -double OS::TimeCurrentMillis() { - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) return 0.0; - return (static_cast(tv.tv_sec) * 1000) + - (static_cast(tv.tv_usec) / 1000); -} - - -int64_t OS::Ticks() { - // Mac OS's gettimeofday has microsecond resolution. - struct timeval tv; - if (gettimeofday(&tv, NULL) < 0) - return 0; - return (static_cast(tv.tv_sec) * 1000000) + tv.tv_usec; -} - - -char* OS::LocalTimezone(double time) { - time_t tv = static_cast(floor(time/msPerSecond)); - struct tm* t = localtime(&tv); - return const_cast(t->tm_zone); -} - - -double OS::DaylightSavingsOffset(double time) { - time_t tv = static_cast(floor(time/msPerSecond)); - struct tm* t = localtime(&tv); - return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; -} - - -double OS::LocalTimeOffset() { - time_t tv = time(NULL); - struct tm* t = localtime(&tv); - // tm_gmtoff includes any daylight savings offset, so subtract it. - return static_cast(t->tm_gmtoff * msPerSecond - - (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); -} - - FILE* OS::FOpen(const char* path, const char* mode) { return fopen(path, mode); } diff --git a/src/platform-posix.cc b/src/platform-posix.cc index 6f71315..87087e4 100644 --- a/src/platform-posix.cc +++ b/src/platform-posix.cc @@ -31,8 +31,11 @@ #include #include +#include #include +#include +#include #include #include @@ -48,6 +51,60 @@ namespace v8 { namespace internal { // ---------------------------------------------------------------------------- +// POSIX date/time support. +// + +int OS::GetUserTime(uint32_t* secs, uint32_t* usecs) { + struct rusage usage; + + if (getrusage(RUSAGE_SELF, &usage) < 0) return -1; + *secs = usage.ru_utime.tv_sec; + *usecs = usage.ru_utime.tv_usec; + return 0; +} + + +double OS::TimeCurrentMillis() { + struct timeval tv; + if (gettimeofday(&tv, NULL) < 0) return 0.0; + return (static_cast(tv.tv_sec) * 1000) + + (static_cast(tv.tv_usec) / 1000); +} + + +int64_t OS::Ticks() { + // gettimeofday has microsecond resolution. + struct timeval tv; + if (gettimeofday(&tv, NULL) < 0) + return 0; + return (static_cast(tv.tv_sec) * 1000000) + tv.tv_usec; +} + + +char* OS::LocalTimezone(double time) { + time_t tv = static_cast(floor(time/msPerSecond)); + struct tm* t = localtime(&tv); + return const_cast(t->tm_zone); +} + + +double OS::DaylightSavingsOffset(double time) { + time_t tv = static_cast(floor(time/msPerSecond)); + struct tm* t = localtime(&tv); + return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; +} + + +double OS::LocalTimeOffset() { + time_t tv = time(NULL); + struct tm* t = localtime(&tv); + // tm_gmtoff includes any daylight savings offset, so subtract it. + return static_cast(t->tm_gmtoff * msPerSecond - + (t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); +} + + +// ---------------------------------------------------------------------------- // POSIX socket support. // -- 2.7.4