mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 05:50:30 +00:00
Add fromWinTime and deprecate unixTimeToWinTime/winTimeToUnixTime (#7641)
This commit is contained in:
committed by
Andreas Rumpf
parent
7e15d5134b
commit
7d034d7b6a
@@ -188,7 +188,7 @@ proc getLastModificationTime*(file: string): times.Time {.rtl, extern: "nos$1".}
|
||||
var f: WIN32_FIND_DATA
|
||||
var h = findFirstFile(file, f)
|
||||
if h == -1'i32: raiseOSError(osLastError())
|
||||
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftLastWriteTime)).int64)
|
||||
result = fromWinTime(rdFileTime(f.ftLastWriteTime))
|
||||
findClose(h)
|
||||
|
||||
proc getLastAccessTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||
@@ -201,7 +201,7 @@ proc getLastAccessTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||
var f: WIN32_FIND_DATA
|
||||
var h = findFirstFile(file, f)
|
||||
if h == -1'i32: raiseOSError(osLastError())
|
||||
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftLastAccessTime)).int64)
|
||||
result = fromWinTime(rdFileTime(f.ftLastAccessTime))
|
||||
findClose(h)
|
||||
|
||||
proc getCreationTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||
@@ -218,7 +218,7 @@ proc getCreationTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
||||
var f: WIN32_FIND_DATA
|
||||
var h = findFirstFile(file, f)
|
||||
if h == -1'i32: raiseOSError(osLastError())
|
||||
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftCreationTime)).int64)
|
||||
result = fromWinTime(rdFileTime(f.ftCreationTime))
|
||||
findClose(h)
|
||||
|
||||
proc fileNewer*(a, b: string): bool {.rtl, extern: "nos$1".} =
|
||||
@@ -1512,16 +1512,14 @@ template rawToFormalFileInfo(rawInfo, path, formalInfo): untyped =
|
||||
## 'rawInfo' is either a 'TBY_HANDLE_FILE_INFORMATION' structure on Windows,
|
||||
## or a 'Stat' structure on posix
|
||||
when defined(Windows):
|
||||
template toTime(e: FILETIME): untyped {.gensym.} =
|
||||
fromUnix(winTimeToUnixTime(rdFileTime(e)).int64) # local templates default to bind semantics
|
||||
template merge(a, b): untyped = a or (b shl 32)
|
||||
formalInfo.id.device = rawInfo.dwVolumeSerialNumber
|
||||
formalInfo.id.file = merge(rawInfo.nFileIndexLow, rawInfo.nFileIndexHigh)
|
||||
formalInfo.size = merge(rawInfo.nFileSizeLow, rawInfo.nFileSizeHigh)
|
||||
formalInfo.linkCount = rawInfo.nNumberOfLinks
|
||||
formalInfo.lastAccessTime = toTime(rawInfo.ftLastAccessTime)
|
||||
formalInfo.lastWriteTime = toTime(rawInfo.ftLastWriteTime)
|
||||
formalInfo.creationTime = toTime(rawInfo.ftCreationTime)
|
||||
formalInfo.lastAccessTime = fromWinTime(rdFileTime(rawInfo.ftLastAccessTime))
|
||||
formalInfo.lastWriteTime = fromWinTime(rdFileTime(rawInfo.ftLastWriteTime))
|
||||
formalInfo.creationTime = fromWinTime(rdFileTime(rawInfo.ftCreationTime))
|
||||
|
||||
# Retrieve basic permissions
|
||||
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_READONLY) != 0'i32:
|
||||
|
||||
@@ -374,9 +374,20 @@ proc toUnix*(t: Time): int64 {.benign, tags: [], raises: [], noSideEffect.} =
|
||||
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
|
||||
t.seconds
|
||||
|
||||
proc fromWinTime*(win: int64): Time =
|
||||
## Convert a Windows file time (100-nanosecond intervals since ``1601-01-01T00:00:00Z``)
|
||||
## to a ``Time``.
|
||||
let hnsecsSinceEpoch = (win - epochDiff)
|
||||
var seconds = hnsecsSinceEpoch div rateDiff
|
||||
var nanos = ((hnsecsSinceEpoch mod rateDiff) * 100).int
|
||||
if nanos < 0:
|
||||
nanos += convert(Seconds, Nanoseconds, 1)
|
||||
seconds -= 1
|
||||
result = initTime(seconds, nanos)
|
||||
|
||||
proc toWinTime*(t: Time): int64 =
|
||||
## Convert ``t`` to a Windows file time (100-nanosecond intervals since ``1601-01-01T00:00:00Z``).
|
||||
result = t.seconds * rateDiff + epochDiff
|
||||
result = t.seconds * rateDiff + epochDiff + t.nanoseconds div 100
|
||||
|
||||
proc isLeapYear*(year: int): bool =
|
||||
## Returns true if ``year`` is a leap year.
|
||||
@@ -860,10 +871,7 @@ proc getTime*(): Time {.tags: [TimeEffect], benign.} =
|
||||
elif defined(windows):
|
||||
var f: FILETIME
|
||||
getSystemTimeAsFileTime(f)
|
||||
let nanosSinceEpoch = (rdFileTime(f) - epochDiff) * 100
|
||||
let seconds = convert(Nanoseconds, Seconds, nanosSinceEpoch)
|
||||
let nanos = (nanosSinceEpoch mod convert(Seconds, Nanoseconds, 1)).int
|
||||
result = initTime(seconds, nanos)
|
||||
result = fromWinTime(rdFileTime(f))
|
||||
|
||||
proc now*(): DateTime {.tags: [TimeEffect], benign.} =
|
||||
## Get the current time as a ``DateTime`` in the local timezone.
|
||||
@@ -1720,14 +1728,6 @@ when not defined(JS):
|
||||
var
|
||||
clocksPerSec {.importc: "CLOCKS_PER_SEC", nodecl.}: int
|
||||
|
||||
proc unixTimeToWinTime*(time: CTime): int64 =
|
||||
## converts a UNIX `Time` (``time_t``) to a Windows file time
|
||||
result = int64(time) * rateDiff + epochDiff
|
||||
|
||||
proc winTimeToUnixTime*(time: int64): CTime =
|
||||
## converts a Windows time to a UNIX `Time` (``time_t``)
|
||||
result = CTime((time - epochDiff) div rateDiff)
|
||||
|
||||
when not defined(useNimRtl):
|
||||
proc cpuTime*(): float {.rtl, extern: "nt$1", tags: [TimeEffect].} =
|
||||
## gets time spent that the CPU spent to run the current process in
|
||||
@@ -1769,6 +1769,19 @@ when defined(JS):
|
||||
|
||||
# Deprecated procs
|
||||
|
||||
when not defined(JS):
|
||||
proc unixTimeToWinTime*(time: CTime): int64 {.deprecated: "Use toWinTime instead".} =
|
||||
## Converts a UNIX `Time` (``time_t``) to a Windows file time
|
||||
##
|
||||
## **Deprecated:** use ``toWinTime`` instead.
|
||||
result = int64(time) * rateDiff + epochDiff
|
||||
|
||||
proc winTimeToUnixTime*(time: int64): CTime {.deprecated: "Use fromWinTime instead".} =
|
||||
## Converts a Windows time to a UNIX `Time` (``time_t``)
|
||||
##
|
||||
## **Deprecated:** use ``fromWinTime`` instead.
|
||||
result = CTime((time - epochDiff) div rateDiff)
|
||||
|
||||
proc initInterval*(seconds, minutes, hours, days, months,
|
||||
years: int = 0): TimeInterval {.deprecated.} =
|
||||
## **Deprecated since v0.18.0:** use ``initTimeInterval`` instead.
|
||||
|
||||
@@ -409,4 +409,11 @@ suite "ttimes":
|
||||
# Bug with adding a day to a Time
|
||||
let day = 24.hours
|
||||
let tomorrow = now + day
|
||||
check tomorrow - now == initDuration(days = 1)
|
||||
check tomorrow - now == initDuration(days = 1)
|
||||
|
||||
test "fromWinTime/toWinTime":
|
||||
check 0.fromUnix.toWinTime.fromWinTime.toUnix == 0
|
||||
check (-1).fromWinTime.nanoseconds == convert(Seconds, Nanoseconds, 1) - 100
|
||||
check -1.fromWinTime.toWinTime == -1
|
||||
# One nanosecond is discarded due to differences in time resolution
|
||||
check initTime(0, 101).toWinTime.fromWinTime.nanoseconds == 100
|
||||
Reference in New Issue
Block a user