Add fromWinTime and deprecate unixTimeToWinTime/winTimeToUnixTime (#7641)
This commit is contained in:
parent
7e15d5134b
commit
7d034d7b6a
3 changed files with 40 additions and 22 deletions
|
|
@ -188,7 +188,7 @@ proc getLastModificationTime*(file: string): times.Time {.rtl, extern: "nos$1".}
|
||||||
var f: WIN32_FIND_DATA
|
var f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(file, f)
|
var h = findFirstFile(file, f)
|
||||||
if h == -1'i32: raiseOSError(osLastError())
|
if h == -1'i32: raiseOSError(osLastError())
|
||||||
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftLastWriteTime)).int64)
|
result = fromWinTime(rdFileTime(f.ftLastWriteTime))
|
||||||
findClose(h)
|
findClose(h)
|
||||||
|
|
||||||
proc getLastAccessTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
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 f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(file, f)
|
var h = findFirstFile(file, f)
|
||||||
if h == -1'i32: raiseOSError(osLastError())
|
if h == -1'i32: raiseOSError(osLastError())
|
||||||
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftLastAccessTime)).int64)
|
result = fromWinTime(rdFileTime(f.ftLastAccessTime))
|
||||||
findClose(h)
|
findClose(h)
|
||||||
|
|
||||||
proc getCreationTime*(file: string): times.Time {.rtl, extern: "nos$1".} =
|
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 f: WIN32_FIND_DATA
|
||||||
var h = findFirstFile(file, f)
|
var h = findFirstFile(file, f)
|
||||||
if h == -1'i32: raiseOSError(osLastError())
|
if h == -1'i32: raiseOSError(osLastError())
|
||||||
result = fromUnix(winTimeToUnixTime(rdFileTime(f.ftCreationTime)).int64)
|
result = fromWinTime(rdFileTime(f.ftCreationTime))
|
||||||
findClose(h)
|
findClose(h)
|
||||||
|
|
||||||
proc fileNewer*(a, b: string): bool {.rtl, extern: "nos$1".} =
|
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,
|
## 'rawInfo' is either a 'TBY_HANDLE_FILE_INFORMATION' structure on Windows,
|
||||||
## or a 'Stat' structure on posix
|
## or a 'Stat' structure on posix
|
||||||
when defined(Windows):
|
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)
|
template merge(a, b): untyped = a or (b shl 32)
|
||||||
formalInfo.id.device = rawInfo.dwVolumeSerialNumber
|
formalInfo.id.device = rawInfo.dwVolumeSerialNumber
|
||||||
formalInfo.id.file = merge(rawInfo.nFileIndexLow, rawInfo.nFileIndexHigh)
|
formalInfo.id.file = merge(rawInfo.nFileIndexLow, rawInfo.nFileIndexHigh)
|
||||||
formalInfo.size = merge(rawInfo.nFileSizeLow, rawInfo.nFileSizeHigh)
|
formalInfo.size = merge(rawInfo.nFileSizeLow, rawInfo.nFileSizeHigh)
|
||||||
formalInfo.linkCount = rawInfo.nNumberOfLinks
|
formalInfo.linkCount = rawInfo.nNumberOfLinks
|
||||||
formalInfo.lastAccessTime = toTime(rawInfo.ftLastAccessTime)
|
formalInfo.lastAccessTime = fromWinTime(rdFileTime(rawInfo.ftLastAccessTime))
|
||||||
formalInfo.lastWriteTime = toTime(rawInfo.ftLastWriteTime)
|
formalInfo.lastWriteTime = fromWinTime(rdFileTime(rawInfo.ftLastWriteTime))
|
||||||
formalInfo.creationTime = toTime(rawInfo.ftCreationTime)
|
formalInfo.creationTime = fromWinTime(rdFileTime(rawInfo.ftCreationTime))
|
||||||
|
|
||||||
# Retrieve basic permissions
|
# Retrieve basic permissions
|
||||||
if (rawInfo.dwFileAttributes and FILE_ATTRIBUTE_READONLY) != 0'i32:
|
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``).
|
## Convert ``t`` to a unix timestamp (seconds since ``1970-01-01T00:00:00Z``).
|
||||||
t.seconds
|
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 =
|
proc toWinTime*(t: Time): int64 =
|
||||||
## Convert ``t`` to a Windows file time (100-nanosecond intervals since ``1601-01-01T00:00:00Z``).
|
## 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 =
|
proc isLeapYear*(year: int): bool =
|
||||||
## Returns true if ``year`` is a leap year.
|
## Returns true if ``year`` is a leap year.
|
||||||
|
|
@ -860,10 +871,7 @@ proc getTime*(): Time {.tags: [TimeEffect], benign.} =
|
||||||
elif defined(windows):
|
elif defined(windows):
|
||||||
var f: FILETIME
|
var f: FILETIME
|
||||||
getSystemTimeAsFileTime(f)
|
getSystemTimeAsFileTime(f)
|
||||||
let nanosSinceEpoch = (rdFileTime(f) - epochDiff) * 100
|
result = fromWinTime(rdFileTime(f))
|
||||||
let seconds = convert(Nanoseconds, Seconds, nanosSinceEpoch)
|
|
||||||
let nanos = (nanosSinceEpoch mod convert(Seconds, Nanoseconds, 1)).int
|
|
||||||
result = initTime(seconds, nanos)
|
|
||||||
|
|
||||||
proc now*(): DateTime {.tags: [TimeEffect], benign.} =
|
proc now*(): DateTime {.tags: [TimeEffect], benign.} =
|
||||||
## Get the current time as a ``DateTime`` in the local timezone.
|
## Get the current time as a ``DateTime`` in the local timezone.
|
||||||
|
|
@ -1720,14 +1728,6 @@ when not defined(JS):
|
||||||
var
|
var
|
||||||
clocksPerSec {.importc: "CLOCKS_PER_SEC", nodecl.}: int
|
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):
|
when not defined(useNimRtl):
|
||||||
proc cpuTime*(): float {.rtl, extern: "nt$1", tags: [TimeEffect].} =
|
proc cpuTime*(): float {.rtl, extern: "nt$1", tags: [TimeEffect].} =
|
||||||
## gets time spent that the CPU spent to run the current process in
|
## gets time spent that the CPU spent to run the current process in
|
||||||
|
|
@ -1769,6 +1769,19 @@ when defined(JS):
|
||||||
|
|
||||||
# Deprecated procs
|
# 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,
|
proc initInterval*(seconds, minutes, hours, days, months,
|
||||||
years: int = 0): TimeInterval {.deprecated.} =
|
years: int = 0): TimeInterval {.deprecated.} =
|
||||||
## **Deprecated since v0.18.0:** use ``initTimeInterval`` instead.
|
## **Deprecated since v0.18.0:** use ``initTimeInterval`` instead.
|
||||||
|
|
|
||||||
|
|
@ -410,3 +410,10 @@ suite "ttimes":
|
||||||
let day = 24.hours
|
let day = 24.hours
|
||||||
let tomorrow = now + day
|
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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue