1/* 2 * C11 <time.h> implementation 3 * 4 * (C) Copyright yohhoy 2012. 5 * Copyright 2022 Yonggang Luo 6 * Distributed under the Boost Software License, Version 1.0. 7 * 8 * Permission is hereby granted, free of charge, to any person or organization 9 * obtaining a copy of the software and accompanying documentation covered by 10 * this license (the "Software") to use, reproduce, display, distribute, 11 * execute, and transmit the Software, and to prepare [[derivative work]]s of the 12 * Software, and to permit third-parties to whom the Software is furnished to 13 * do so, all subject to the following: 14 * 15 * The copyright notices in the Software and this entire statement, including 16 * the above license grant, this restriction and the following disclaimer, 17 * must be included in all copies of the Software, in whole or in part, and 18 * all derivative works of the Software, unless such copies or derivative 19 * works are solely in the form of machine-executable object code generated by 20 * a source language processor. 21 * 22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 25 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 26 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 28 * DEALINGS IN THE SOFTWARE. 29 */ 30 31#include "c11/time.h" 32 33#ifndef HAVE_TIMESPEC_GET 34 35#if defined(_WIN32) && !defined(__CYGWIN__) 36 37#ifndef WIN32_LEAN_AND_MEAN 38#define WIN32_LEAN_AND_MEAN 1 39#endif 40#include <windows.h> 41 42int 43timespec_get(struct timespec *ts, int base) 44{ 45/* difference between 1970 and 1601 */ 46#define _TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS 116444736000000000ull 47/* 1 tick is 100 nanoseconds */ 48#define _TIMESPEC_IMPL_TICKS_PER_SECONDS 10000000ull 49 if (!ts) 50 return 0; 51 if (base == TIME_UTC) { 52 FILETIME ft; 53 ULARGE_INTEGER date; 54 LONGLONG ticks; 55 56 GetSystemTimeAsFileTime(&ft); 57 date.HighPart = ft.dwHighDateTime; 58 date.LowPart = ft.dwLowDateTime; 59 ticks = (LONGLONG)(date.QuadPart - _TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS); 60 ts->tv_sec = ticks / _TIMESPEC_IMPL_TICKS_PER_SECONDS; 61 ts->tv_nsec = (ticks % _TIMESPEC_IMPL_TICKS_PER_SECONDS) * 100; 62 return base; 63 } 64 return 0; 65#undef _TIMESPEC_IMPL_UNIX_EPOCH_IN_TICKS 66#undef _TIMESPEC_IMPL_TICKS_PER_SECONDS 67} 68 69#else 70 71int 72timespec_get(struct timespec *ts, int base) 73{ 74 if (!ts) 75 return 0; 76 if (base == TIME_UTC) { 77 clock_gettime(CLOCK_REALTIME, ts); 78 return base; 79 } 80 return 0; 81} 82#endif 83 84#endif /* !HAVE_TIMESPEC_GET */ 85