Skip to content

Commit

Permalink
std/time: reimplement the Time and AbsTime
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Nov 19, 2024
1 parent fabb30d commit 4853b84
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 262 deletions.
2 changes: 1 addition & 1 deletion std/encoding/csv/reader.jule
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
// {`Multi-line
// field`, `comma is ,`}

use "std/bytes"
use "std/bufio"
use "std/bytes"
use "std/io"
use "std/unicode"
use "std/unicode/utf8"
Expand Down
2 changes: 1 addition & 1 deletion std/jule/sema/comptime.jule
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,7 @@ impl comptimeValue {
e.pushErr(arg.Token, build::LogMsg.ExprNotConst)
ret nil
}
log := checkDataForIntegerIndexing(e.s, d, arg.Token,e.getOwnerRefers())
log := checkDataForIntegerIndexing(e.s, d, arg.Token, e.getOwnerRefers())
if log != build::LogMsg.Empty {
e.pushErr(arg.Token, log)
ret nil
Expand Down
19 changes: 19 additions & 0 deletions std/runtime/time_unix.jule
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@

cpp use "<time.h>"

cpp type clockid_t: int

cpp unsafe fn clock_gettime(id: cpp.clockid_t, mut tp: *cpp.timespec): int

cpp struct timespec {
tv_sec: i64
tv_nsec: i64
}

// Returns system-wide realtime clock in seconds and nanoseconds.
// The nanoseconds is not a second yet, always in range (0, 999999999).
fn timeNow(): (sec: i64, nsec: i64) {
// Identifier for system-wide realtime clock.
// All possible platforms use 0 for this one, as fas as tested.
const CLOCK_REALTIME = 0

mut ts := cpp.timespec{}
if unsafe { cpp.clock_gettime(cpp.clockid_t(CLOCK_REALTIME), &ts) } == -1 {
panic("runtime: timeNow failed")
}
sec, nsec = ts.tv_sec, ts.tv_nsec
ret
}
34 changes: 34 additions & 0 deletions std/runtime/time_windows.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2024 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.

cpp fn GetSystemTimeAsFileTime(mut ft: *cpp.FILETIME)

#typedef
cpp struct FILETIME {
dwLowDateTime: _DWORD
dwHighDateTime: _DWORD
}

#typedef
cpp struct LARGE_INTEGER {
QuadPart: u64
}

// Returns system-wide realtime clock in seconds and nanoseconds.
// The nanoseconds is not a second yet, always in range (0, 999999999).
fn timeNow(): (sec: i64, nsec: i64) {
mut ft := cpp.FILETIME{}
cpp.GetSystemTimeAsFileTime(&ft) // Returns ticks in UTC
// Get the number of seconds since January 1, 1970 12:00am UTC
// FILETIME is in 100-nanosecond intervals since 1601-01-01
mut t := cpp.LARGE_INTEGER{}
t.QuadPart = u64(ft.dwHighDateTime)
t.QuadPart <<= 32
t.QuadPart |= u64(ft.dwLowDateTime)
t.QuadPart -= 116444736000000000 // Convert from 1601 to 1970
t.QuadPart = t.QuadPart / 10 // Convert from 100-ns intervals to nanoseconds
sec = i64(t.QuadPart / 1000000)
nsec = i64(t.QuadPart % 1000000)
ret
}
Loading

0 comments on commit 4853b84

Please sign in to comment.