Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
d7e7adb4
Commit
d7e7adb4
authored
3 years ago
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
time/FileTime: add ChronoToFileTime()
parent
45354a42
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
0 deletions
+53
-0
FileTime.hxx
src/time/FileTime.hxx
+49
-0
TestFileTime.cxx
test/time/TestFileTime.cxx
+4
-0
No files found.
src/time/FileTime.hxx
View file @
d7e7adb4
...
@@ -53,6 +53,21 @@ ToInt64(FILETIME ft) noexcept
...
@@ -53,6 +53,21 @@ ToInt64(FILETIME ft) noexcept
return
ToUint64
(
ft
);
return
ToUint64
(
ft
);
}
}
constexpr
FILETIME
ToFileTime
(
uint_least64_t
t
)
noexcept
{
FILETIME
ft
{};
ft
.
dwLowDateTime
=
DWORD
(
t
);
ft
.
dwHighDateTime
=
DWORD
(
t
>>
32
);
return
ft
;
}
constexpr
FILETIME
ToFileTime
(
int_least64_t
t
)
noexcept
{
return
ToFileTime
(
uint_least64_t
(
t
));
}
/* "A file time is a 64-bit value that represents the number of
/* "A file time is a 64-bit value that represents the number of
100-nanosecond intervals"
100-nanosecond intervals"
https://docs.microsoft.com/en-us/windows/win32/sysinfo/file-times */
https://docs.microsoft.com/en-us/windows/win32/sysinfo/file-times */
...
@@ -104,6 +119,40 @@ FileTimeToChrono(FILETIME ft) noexcept
...
@@ -104,6 +119,40 @@ FileTimeToChrono(FILETIME ft) noexcept
return
unix_epoch
+
sys_duration
;
return
unix_epoch
+
sys_duration
;
}
}
constexpr
FILETIME
ToFileTime
(
FileTimeDuration
d
)
noexcept
{
return
ToFileTime
(
d
.
count
());
}
constexpr
FILETIME
UnixEpochDurationToFileTime
(
FileTimeDuration
d
)
noexcept
{
/**
* The number of days between the Windows FILETIME epoch
* (1601-01-01T00:00) and the Unix epoch (1970-01-01T00:00).
*/
constexpr
int_least64_t
windows_unix_days
=
134774
;
constexpr
int_least64_t
windows_unix_hours
=
windows_unix_days
*
24
;
constexpr
FileTimeDuration
windows_unix_delta
{
std
::
chrono
::
hours
{
windows_unix_hours
}};
return
ToFileTime
(
d
+
windows_unix_delta
);
}
inline
FILETIME
ChronoToFileTime
(
std
::
chrono
::
system_clock
::
time_point
tp
)
noexcept
{
/* this is guaranteed to be 0 in C++20 */
const
auto
unix_epoch
=
std
::
chrono
::
system_clock
::
from_time_t
(
0
);
const
auto
since_unix_epoch
=
tp
-
unix_epoch
;
const
auto
ft_since_unix_epoch
=
std
::
chrono
::
duration_cast
<
FileTimeDuration
>
(
since_unix_epoch
);
return
UnixEpochDurationToFileTime
(
ft_since_unix_epoch
);
}
constexpr
std
::
chrono
::
seconds
constexpr
std
::
chrono
::
seconds
DeltaFileTimeS
(
FILETIME
a
,
FILETIME
b
)
noexcept
DeltaFileTimeS
(
FILETIME
a
,
FILETIME
b
)
noexcept
{
{
...
...
This diff is collapsed.
Click to expand it.
test/time/TestFileTime.cxx
View file @
d7e7adb4
...
@@ -49,4 +49,8 @@ TEST(Time, FileTimeToChrono)
...
@@ -49,4 +49,8 @@ TEST(Time, FileTimeToChrono)
ASSERT_EQ
(
stat
(
"."
,
&
st
),
0
);
ASSERT_EQ
(
stat
(
"."
,
&
st
),
0
);
ASSERT_EQ
(
std
::
chrono
::
system_clock
::
to_time_t
(
tp
),
st
.
st_mtime
);
ASSERT_EQ
(
std
::
chrono
::
system_clock
::
to_time_t
(
tp
),
st
.
st_mtime
);
const
auto
ft2
=
ChronoToFileTime
(
std
::
chrono
::
system_clock
::
from_time_t
(
st
.
st_mtime
));
const
auto
tp2
=
FileTimeToChrono
(
ft2
);
ASSERT_EQ
(
std
::
chrono
::
system_clock
::
to_time_t
(
tp2
),
st
.
st_mtime
);
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment