system/Clock, fs/FileInfo: move FILETIME specific code to time/FileTime.hxx

This commit is contained in:
Max Kellermann 2019-05-08 15:47:04 +02:00
parent 96f889276f
commit 5641c4baa6
3 changed files with 75 additions and 38 deletions

View File

@ -24,7 +24,7 @@
#include "system/Error.hxx"
#ifdef _WIN32
#include <fileapi.h>
#include "time/FileTime.hxx"
#else
#include <sys/stat.h>
#endif
@ -33,30 +33,6 @@
#include <stdint.h>
#ifdef _WIN32
static constexpr uint64_t
ConstructUint64(DWORD lo, DWORD hi)
{
return uint64_t(lo) | (uint64_t(hi) << 32);
}
static constexpr time_t
FileTimeToTimeT(FILETIME ft)
{
return (ConstructUint64(ft.dwLowDateTime, ft.dwHighDateTime)
- 116444736000000000) / 10000000;
}
static std::chrono::system_clock::time_point
FileTimeToChrono(FILETIME ft)
{
// TODO: eliminate the time_t roundtrip, preserve sub-second resolution
return std::chrono::system_clock::from_time_t(FileTimeToTimeT(ft));
}
#endif
class FileInfo {
friend bool GetFileInfo(Path path, FileInfo &info,
bool follow_symlinks);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2003-2018 The Music Player Daemon Project
* Copyright 2003-2019 The Music Player Daemon Project
* http://www.musicpd.org
*
* This program is free software; you can redistribute it and/or modify
@ -20,19 +20,9 @@
#include "Clock.hxx"
#ifdef _WIN32
#include <windows.h>
#include "time/FileTime.hxx"
gcc_const
static std::chrono::seconds
DeltaFileTimeS(FILETIME a, FILETIME b)
{
ULARGE_INTEGER a2, b2;
b2.LowPart = b.dwLowDateTime;
b2.HighPart = b.dwHighDateTime;
a2.LowPart = a.dwLowDateTime;
a2.HighPart = a.dwHighDateTime;
return std::chrono::seconds((a2.QuadPart - b2.QuadPart) / 10000000);
}
#include <windows.h>
std::chrono::seconds
GetProcessUptimeS()

71
src/time/FileTime.hxx Normal file
View File

@ -0,0 +1,71 @@
/*
* Copyright 2013-2019 Max Kellermann <max.kellermann@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef FILE_TIME_HXX
#define FILE_TIME_HXX
#include <fileapi.h>
#include <chrono>
#include <stdint.h>
static constexpr uint64_t
ConstructUint64(DWORD lo, DWORD hi)
{
return uint64_t(lo) | (uint64_t(hi) << 32);
}
static constexpr time_t
FileTimeToTimeT(FILETIME ft)
{
return (ConstructUint64(ft.dwLowDateTime, ft.dwHighDateTime)
- 116444736000000000) / 10000000;
}
static inline std::chrono::system_clock::time_point
FileTimeToChrono(FILETIME ft)
{
// TODO: eliminate the time_t roundtrip, preserve sub-second resolution
return std::chrono::system_clock::from_time_t(FileTimeToTimeT(ft));
}
gcc_const
static inline std::chrono::seconds
DeltaFileTimeS(FILETIME a, FILETIME b)
{
ULARGE_INTEGER a2, b2;
b2.LowPart = b.dwLowDateTime;
b2.HighPart = b.dwHighDateTime;
a2.LowPart = a.dwLowDateTime;
a2.HighPart = a.dwHighDateTime;
return std::chrono::seconds((a2.QuadPart - b2.QuadPart) / 10000000);
}
#endif