├── .gitignore ├── example.c ├── CMakeLists.txt ├── README.md ├── LICENSE ├── appveyor.yml └── sys └── time.h /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | build 3 | out -------------------------------------------------------------------------------- /example.c: -------------------------------------------------------------------------------- 1 | #ifdef NDEBUG 2 | #undef NDEBUG 3 | #endif 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | assert(sizeof(struct timeval) > 0); 12 | 13 | struct timeval tv; 14 | gettimeofday(&tv, NULL); 15 | 16 | printf("gettimeofday : %ld %ld", tv.tv_sec, tv.tv_usec); 17 | } 18 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(sys_time_h LANGUAGES C) 4 | 5 | add_executable(example example.c sys/time.h) 6 | 7 | target_include_directories(example PRIVATE ${CMAKE_SOURCE_DIR}) 8 | if(MSVC) 9 | target_compile_options(example PRIVATE /W4 /WX) 10 | else(MSVC) 11 | target_compile_options(example PRIVATE -Wall -Wextra -Wpedantic -Werror) 12 | endif(MSVC) 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build status](https://ci.appveyor.com/api/projects/status/v1pltjps1li83cof?svg=true)](https://ci.appveyor.com/project/SSE4/sys-time-h) 2 | 3 | header-only Windows implementation of the `` header. 4 | 5 | tested on the following compilers: 6 | - Visual Studio 7 | - Clang for Windows (clang-cl) 8 | - GCC (MinGW) 9 | 10 | defines the following structures: 11 | - timeval 12 | 13 | implements the following functions: 14 | - gettimeofday 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 win32ports 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | build: false 2 | 3 | environment: 4 | matrix: 5 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 6 | CMAKE_SH: "CMAKE_SH-NOTFOUND" 7 | CMAKE_GENERATOR: "MinGW Makefiles" 8 | MINGW_HOME: "C:\\MinGW\\bin" 9 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 10 | CMAKE_SH: "CMAKE_SH-NOTFOUND" 11 | CMAKE_GENERATOR: "MinGW Makefiles" 12 | MINGW_HOME: "C:\\mingw-w64\\x86_64-6.3.0-posix-seh-rt_v5-rev1\\mingw64\\bin" 13 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 14 | CMAKE_SH: "CMAKE_SH-NOTFOUND" 15 | CMAKE_GENERATOR: "MinGW Makefiles" 16 | MINGW_HOME: "C:\\mingw-w64\\x86_64-7.2.0-posix-seh-rt_v5-rev1\\mingw64\\bin" 17 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 18 | CMAKE_SH: "CMAKE_SH-NOTFOUND" 19 | CMAKE_GENERATOR: "MinGW Makefiles" 20 | MINGW_HOME: "C:\\mingw-w64\\x86_64-7.3.0-posix-seh-rt_v5-rev0\\mingw64\\bin" 21 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 22 | CMAKE_SH: "CMAKE_SH-NOTFOUND" 23 | CMAKE_GENERATOR: "MinGW Makefiles" 24 | MINGW_HOME: "C:\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin" 25 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 26 | CMAKE_GENERATOR: "Ninja" 27 | CC: "C:\\Program Files\\LLVM\\bin\\clang-cl.exe" 28 | VCVARS: "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat" 29 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 30 | CMAKE_GENERATOR: "Visual Studio 12 2013" 31 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 32 | CMAKE_GENERATOR: "Visual Studio 12 2013 Win64" 33 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 34 | CMAKE_GENERATOR: "Visual Studio 14 2015" 35 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015 36 | CMAKE_GENERATOR: "Visual Studio 14 2015 Win64" 37 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 38 | CMAKE_GENERATOR: "Visual Studio 15 2017" 39 | - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 40 | CMAKE_GENERATOR: "Visual Studio 15 2017 Win64" 41 | 42 | install: 43 | - mkdir C:\ninja 44 | - appveyor DownloadFile https://github.com/ninja-build/ninja/releases/download/v1.9.0/ninja-win.zip -FileName ninja.zip 45 | - 7z x ninja.zip -oC:\ninja > nul 46 | - set PATH=C:\ninja;%PATH% 47 | 48 | test_script: 49 | - python build.py 50 | -------------------------------------------------------------------------------- /sys/time.h: -------------------------------------------------------------------------------- 1 | /* 2 | MIT License 3 | Copyright (c) 2019 win32ports 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 18 | SOFTWARE. 19 | */ 20 | 21 | #pragma once 22 | 23 | #ifndef __SYS_TIME_H_D9C1BEC0_4C79_4302_8B29_322C4497B27F__ 24 | #define __SYS_TIME_H_D9C1BEC0_4C79_4302_8B29_322C4497B27F__ 25 | 26 | #ifndef _WIN32 27 | 28 | #pragma message("this sys/time.h implementation is for Windows only!") 29 | 30 | #else /* _WIN32 */ 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif /* __cplusplus */ 35 | 36 | #if !defined(_WINSOCK2API_) && !defined(_WINSOCKAPI_) 37 | #include /* timeval */ 38 | #endif /* !defined(_WINSOCK2API_) && !defined(_WINSOCKAPI_) */ 39 | 40 | #include 41 | 42 | struct timezone 43 | { 44 | int tz_minuteswest; 45 | int tz_dsttime; 46 | }; 47 | 48 | static int gettimeofday(struct timeval *tp, struct timezone *tzp) 49 | { 50 | typedef void (__stdcall * pfnGetSystemTimePreciseAsFileTime)(LPFILETIME lpSystemTimeAsFileTime); 51 | HMODULE hKernel32 = NULL; 52 | pfnGetSystemTimePreciseAsFileTime fnGetSystemTimePreciseAsFileTime = NULL; 53 | FILETIME time; 54 | hKernel32 = GetModuleHandleW(L"kernel32.dll"); 55 | #if defined(__GNUC__) && (__GNUC__ == 8) 56 | #pragma GCC diagnostic push 57 | #pragma GCC diagnostic ignored "-Wcast-function-type" 58 | #endif 59 | if (hKernel32) 60 | fnGetSystemTimePreciseAsFileTime = (pfnGetSystemTimePreciseAsFileTime) GetProcAddress(hKernel32, "GetSystemTimePreciseAsFileTime"); 61 | #if defined(__GNUC__) && (__GNUC__ == 8) 62 | #pragma GCC diagnostic pop 63 | #endif 64 | 65 | if (fnGetSystemTimePreciseAsFileTime) 66 | fnGetSystemTimePreciseAsFileTime(&time); 67 | else 68 | GetSystemTimeAsFileTime(&time); 69 | 70 | uint64_t time64 = ((uint64_t)time.dwHighDateTime << 32) | time.dwLowDateTime; 71 | time64 = (time64 / 10 - 11644473600ULL * 1000000ULL); 72 | 73 | if (tp) 74 | { 75 | tp->tv_sec = (long) (time64 / 1000000ULL); 76 | tp->tv_usec = (long) (time64 % 1000000ULL); 77 | } 78 | if (tzp) 79 | { 80 | /* The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL. */ 81 | TIME_ZONE_INFORMATION tzi; 82 | GetTimeZoneInformation(&tzi); 83 | 84 | tzp->tz_minuteswest = tzi.Bias; 85 | tzp->tz_dsttime = 0; 86 | } 87 | 88 | /* The gettimeofday() function returns 0 and no value is reserved to indicate an error. */ 89 | return 0; 90 | } 91 | 92 | #ifdef __cplusplus 93 | } 94 | #endif /* __cplusplus */ 95 | 96 | #endif /* _WIN32 */ 97 | 98 | #endif /* __SYS_TIME_H_D9C1BEC0_4C79_4302_8B29_322C4497B27F__ */ 99 | --------------------------------------------------------------------------------