From 76344f7be3c36b0b0fdf66dafd9e5898d60f368e Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Tue, 1 Mar 2016 16:10:58 -0800 Subject: [PATCH] Create the portability header for time.h Summary: Although MSVC does have time.h, and does have equivelents of these functions, those have slightly different semantics so we have to wrap them. Reviewed By: yfeldblum Differential Revision: D2983613 fb-gh-sync-id: 676cd524ffa834a4250a2acc76aa1200eefe62cc shipit-source-id: 676cd524ffa834a4250a2acc76aa1200eefe62cc --- folly/Makefile.am | 2 + folly/portability/Time.cpp | 83 ++++++++++++++++++++++++++++++++++++++ folly/portability/Time.h | 34 ++++++++++++++++ 3 files changed, 119 insertions(+) create mode 100755 folly/portability/Time.cpp create mode 100755 folly/portability/Time.h diff --git a/folly/Makefile.am b/folly/Makefile.am index e910a2cb..0dc1d29e 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -271,6 +271,7 @@ nobase_follyinclude_HEADERS = \ portability/Syscall.h \ portability/SysTime.h \ portability/SysUio.h \ + portability/Time.h \ Preprocessor.h \ ProducerConsumerQueue.h \ Random.h \ @@ -397,6 +398,7 @@ libfolly_la_SOURCES = \ MemoryMapping.cpp \ portability/Environment.cpp \ portability/SysTime.cpp \ + portability/Time.cpp \ Random.cpp \ SafeAssert.cpp \ SharedMutex.cpp \ diff --git a/folly/portability/Time.cpp b/folly/portability/Time.cpp new file mode 100755 index 00000000..0a06abdb --- /dev/null +++ b/folly/portability/Time.cpp @@ -0,0 +1,83 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#ifdef _WIN32 +#include +#include + +#include + +extern "C" { +char* asctime_r(const tm* tm, char* buf) { + char tmpBuf[64]; + if (asctime_s(tmpBuf, tm)) { + return nullptr; + } + // Nothing we can do if the buff is to small :( + return strcpy(buf, tmpBuf); +} + +char* ctime_r(const time_t* t, char* buf) { + char tmpBuf[64]; + if (ctime_s(tmpBuf, t)) { + return nullptr; + } + // Nothing we can do if the buff is to small :( + return strcpy(buf, tmpBuf); +} + +tm* gmtime_r(const time_t* t, tm* res) { + if (!gmtime_s(res, t)) { + return res; + } + return nullptr; +} + +tm* localtime_r(const time_t* t, tm* o) { + if (!localtime_s(o, t)) { + return o; + } + return nullptr; +} + +int nanosleep(const struct timespec* request, struct timespec* remain) { + Sleep((DWORD)((request->tv_sec * 1000) + (request->tv_nsec / 1000000)); + remain->tv_nsec = 0; + remain->tv_sec = 0; + return 0; +} + +char* strptime(const char* __restrict s, + const char* __restrict f, + struct tm* __restrict tm) { + // Isn't the C++ standard lib nice? std::get_time is defined such that its + // format parameters are the exact same as strptime. Of course, we have to + // create a string stream first, and imbue it with the current C locale, and + // we also have to make sure we return the right things if it fails, or + // if it succeeds, but this is still far simpler an implementation than any + // of the versions in any of the C standard libraries. + std::istringstream input(s); + input.imbue(std::locale(setlocale(LC_ALL, nullptr))); + input >> std::get_time(tm, f); + if (input.fail()) { + return nullptr; + } + return const_cast(s + input.tellg()); +} +} +#endif diff --git a/folly/portability/Time.h b/folly/portability/Time.h new file mode 100755 index 00000000..0a35b23f --- /dev/null +++ b/folly/portability/Time.h @@ -0,0 +1,34 @@ +/* + * Copyright 2016 Facebook, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +#ifdef _WIN32 +#define TM_YEAR_BASE (1900) + +extern "C" { +char* asctime_r(const tm* tm, char* buf); +char* ctime_r(const time_t* t, char* buf); +tm* gmtime_r(const time_t* t, tm* res); +tm* localtime_r(const time_t* t, tm* o); +int nanosleep(const struct timespec* request, struct timespec* remain); +char* strptime(const char* __restrict buf, + const char* __restrict fmt, + struct tm* __restrict tm); +} +#endif -- 2.34.1