From 50a1267b7d028f18df39523fd90399f0751180a4 Mon Sep 17 00:00:00 2001 From: Ruslan Mustakov Date: Wed, 30 Nov 2016 20:28:17 +0700 Subject: Fix TimeInfo to Time conversion. Fixes #5065. --- lib/pure/times.nim | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'lib') diff --git a/lib/pure/times.nim b/lib/pure/times.nim index 1767a37be..e5b4f383f 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -436,6 +436,13 @@ when not defined(JS): TimeInfoPtr = ptr StructTM Clock {.importc: "clock_t".} = distinct int + when defined(windows): + proc timegm(t: StructTM): Time {. + importc: "_mkgmtime", header: "", tags: [].} + else: + proc timegm(t: StructTM): Time {. + importc: "timegm", header: "", tags: [].} + proc localtime(timer: ptr Time): TimeInfoPtr {. importc: "localtime", header: "", tags: [].} proc gmtime(timer: ptr Time): TimeInfoPtr {. @@ -516,19 +523,17 @@ when not defined(JS): proc timeInfoToTime(timeInfo: TimeInfo): Time = var cTimeInfo = timeInfo # for C++ we have to make a copy, - # because the header of mktime is broken in my version of libc - result = mktime(timeInfoToTM(cTimeInfo)) - # mktime is defined to interpret the input as local time. As timeInfoToTM - # does ignore the timezone, we need to adjust this here. - result = Time(TimeImpl(result) - getTimezone() + timeInfo.timezone) + # because the header of timegm is broken in my version of libc + result = timegm(timeInfoToTM(cTimeInfo)) + # As timeInfoToTM does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) + timeInfo.timezone) proc toTime(timeInfo: TimeInfo): Time = var cTimeInfo = timeInfo # for C++ we have to make a copy, - # because the header of mktime is broken in my version of libc - result = mktime(timeInfoToTM(cTimeInfo)) - # mktime is defined to interpret the input as local time. As timeInfoToTM - # does ignore the timezone, we need to adjust this here. - result = Time(TimeImpl(result) - getTimezone() + timeInfo.timezone) + # because the header of timegm is broken in my version of libc + result = timegm(timeInfoToTM(cTimeInfo)) + # As timeInfoToTM does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) + timeInfo.timezone) const epochDiff = 116444736000000000'i64 -- cgit 1.4.1-2-gfad0 From b453a8e616f489e077f8edcd95623b07484e7b8b Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Fri, 2 Dec 2016 22:24:42 +0100 Subject: check waitpid() return value before setting exitStatus This fixes a race with parallelBuild on DragonFly BSD. --- lib/pure/osproc.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index 76bd2dfe1..f597bf773 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -965,7 +965,7 @@ elif not defined(useNimRtl): var ret : int var status : cint = 1 ret = waitpid(p.id, status, WNOHANG) - if WIFEXITED(status): + if ret == int(p.id) and WIFEXITED(status): p.exitStatus = status if ret == 0: return true # Can't establish status. Assume running. result = ret == int(p.id) -- cgit 1.4.1-2-gfad0 From 95188edf6fe63ee3fe8341b1b5c2e7b92beb3415 Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Sat, 3 Dec 2016 14:07:08 +0100 Subject: make sure first call to running() after process exit returns false --- lib/pure/osproc.nim | 14 ++++++++++---- tests/osproc/texitcode.nim | 5 +++++ 2 files changed, 15 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/pure/osproc.nim b/lib/pure/osproc.nim index f597bf773..1d43bb321 100644 --- a/lib/pure/osproc.nim +++ b/lib/pure/osproc.nim @@ -965,10 +965,16 @@ elif not defined(useNimRtl): var ret : int var status : cint = 1 ret = waitpid(p.id, status, WNOHANG) - if ret == int(p.id) and WIFEXITED(status): - p.exitStatus = status - if ret == 0: return true # Can't establish status. Assume running. - result = ret == int(p.id) + if ret == int(p.id): + if WIFEXITED(status): + p.exitStatus = status + return false + else: + return true + elif ret == 0: + return true # Can't establish status. Assume running. + else: + return false proc terminate(p: Process) = if kill(p.id, SIGTERM) != 0'i32: diff --git a/tests/osproc/texitcode.nim b/tests/osproc/texitcode.nim index 1e83658c2..4eaab6da2 100644 --- a/tests/osproc/texitcode.nim +++ b/tests/osproc/texitcode.nim @@ -16,3 +16,8 @@ var running = true while running: running = running(p) doAssert(waitForExit(p) == QuitFailure) + +# make sure that first call to running() after process exit returns false +p = startProcess(filename, dir) +os.sleep(500) +doAssert(not running(p)) -- cgit 1.4.1-2-gfad0 From dc8ee72eb6069c181621a3dc4ad97f8cced9db4b Mon Sep 17 00:00:00 2001 From: Dmitry Polienko Date: Tue, 6 Dec 2016 14:16:00 +0700 Subject: Revert @endragor's fix for Windows _mkgmtime is not supported by mingw because of the older msvcrt versions. Fortunately, mktime implementation in Windows is just broken enough to make the initial implementation work... As far as I can tell, this works with both *nix-like platforms and Win, and fixes #4690 --- lib/pure/times.nim | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) (limited to 'lib') diff --git a/lib/pure/times.nim b/lib/pure/times.nim index e5b4f383f..cf4e7dde6 100644 --- a/lib/pure/times.nim +++ b/lib/pure/times.nim @@ -436,10 +436,8 @@ when not defined(JS): TimeInfoPtr = ptr StructTM Clock {.importc: "clock_t".} = distinct int - when defined(windows): - proc timegm(t: StructTM): Time {. - importc: "_mkgmtime", header: "", tags: [].} - else: + when not defined(windows): + # This is not ANSI C, but common enough proc timegm(t: StructTM): Time {. importc: "timegm", header: "", tags: [].} @@ -522,18 +520,22 @@ when not defined(JS): # the conversion is not expensive proc timeInfoToTime(timeInfo: TimeInfo): Time = - var cTimeInfo = timeInfo # for C++ we have to make a copy, - # because the header of timegm is broken in my version of libc - result = timegm(timeInfoToTM(cTimeInfo)) - # As timeInfoToTM does ignore the timezone, we need to adjust this here. - result = Time(TimeImpl(result) + timeInfo.timezone) + toTime(timeInfo) proc toTime(timeInfo: TimeInfo): Time = - var cTimeInfo = timeInfo # for C++ we have to make a copy, - # because the header of timegm is broken in my version of libc - result = timegm(timeInfoToTM(cTimeInfo)) - # As timeInfoToTM does ignore the timezone, we need to adjust this here. - result = Time(TimeImpl(result) + timeInfo.timezone) + var cTimeInfo = timeInfo # for C++ we have to make a copy + # because the header of mktime is broken in my version of libc + + when defined(windows): + # On Windows `mktime` is broken enough to make this work. + result = mktime(timeInfoToTM(cTimeInfo)) + # mktime is defined to interpret the input as local time. As timeInfoToTM + # does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) - getTimezone() + timeInfo.timezone) + else: + result = timegm(timeInfoToTM(cTimeInfo)) + # As timeInfoToTM does ignore the timezone, we need to adjust this here. + result = Time(TimeImpl(result) + timeInfo.timezone) const epochDiff = 116444736000000000'i64 -- cgit 1.4.1-2-gfad0