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