Commit 76344f7b authored by Christopher Dykes's avatar Christopher Dykes Committed by Facebook Github Bot 9

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
parent 602d3f04
...@@ -271,6 +271,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -271,6 +271,7 @@ nobase_follyinclude_HEADERS = \
portability/Syscall.h \ portability/Syscall.h \
portability/SysTime.h \ portability/SysTime.h \
portability/SysUio.h \ portability/SysUio.h \
portability/Time.h \
Preprocessor.h \ Preprocessor.h \
ProducerConsumerQueue.h \ ProducerConsumerQueue.h \
Random.h \ Random.h \
...@@ -397,6 +398,7 @@ libfolly_la_SOURCES = \ ...@@ -397,6 +398,7 @@ libfolly_la_SOURCES = \
MemoryMapping.cpp \ MemoryMapping.cpp \
portability/Environment.cpp \ portability/Environment.cpp \
portability/SysTime.cpp \ portability/SysTime.cpp \
portability/Time.cpp \
Random.cpp \ Random.cpp \
SafeAssert.cpp \ SafeAssert.cpp \
SharedMutex.cpp \ SharedMutex.cpp \
......
/*
* 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 <folly/portability/Time.h>
#ifdef _WIN32
#include <iomanip>
#include <sstream>
#include <Windows.h>
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<char*>(s + input.tellg());
}
}
#endif
/*
* 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 <time.h>
#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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment