time: move time.random() to time.misc module

avoids importing rand to programs that do not need pseudo random generation).
pull/3358/head
Delyan Angelov 2020-01-07 13:58:24 +02:00 committed by Alexander Medvednikov
parent a0c8ad7398
commit 6d30697d9b
4 changed files with 62 additions and 50 deletions

View File

@ -1,21 +1,18 @@
/*
// TODO: Fix this.
import time
import time.misc as tmisc
// using a manual temporary intermediate variable should always work:
fn test_call_fn_that_requires_reference_with_function_that_returns_a_struct_manual(){
t1 := time.random()
fn test_call_fn_that_requires_reference_with_function_that_returns_a_struct_manual() {
t1 := tmisc.random()
t2 := t1.calc_unix()
println('tmp: $t2')
println('res: $t2')
assert true
}
/*
// TODO: Fix this.
// v should produce temporary intermediate variables in chained calls:
fn test_call_fn_that_requires_reference_with_function_that_returns_a_struct_chained(){
res := (time.random().calc_unix())
res := (tmisc.random().calc_unix())
println('res: $res')
assert true
}
*/
fn test_dummy(){}

View File

@ -0,0 +1,12 @@
module misc
import rand
import time
const (
start_time_unix = time.now().unix
)
// random will return a random time.Time in *the past*
pub fn random() time.Time {
return time.unix(rand.next(start_time_unix))
}

View File

@ -0,0 +1,17 @@
import time.misc as tmisc
import rand
fn test_random() {
// guarantee CI test stability, by seeding the random number generator with a known seed
rand.seed(0)
t1 := tmisc.random()
t2 := tmisc.random()
t3 := tmisc.random()
t4 := tmisc.random()
assert t1.unix != t2.unix
assert t1.unix != t3.unix
assert t1.unix != t4.unix
assert t2.unix != t3.unix
assert t2.unix != t4.unix
assert t3.unix != t4.unix
}

View File

@ -3,8 +3,6 @@
// that can be found in the LICENSE file.
module time
import rand
const (
days_string = 'MonTueWedThuFriSatSun'
month_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
@ -32,7 +30,7 @@ pub:
hour int
minute int
second int
unix int
unix int
}
pub enum FormatTime {
@ -65,12 +63,7 @@ pub enum FormatDelimiter {
fn C.localtime(int) &C.tm
fn remove_me_when_c_bug_is_fixed() {
// TODO
}
pub struct C.time_t {
}
pub struct C.time_t {}
fn C.time(int) C.time_t
@ -82,12 +75,6 @@ pub fn now() Time {
return convert_ctime(now)
}
pub fn random() Time {
now_unix := now().unix
rand_unix := rand.next(now_unix)
return time.unix(rand_unix)
}
// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM:SS in
// 24h notation
// @param
@ -224,34 +211,33 @@ pub fn parse_iso(s string) Time {
if fields.len < 5 {
return Time{}
}
pos := months_string.index(fields[2]) or { return Time{} }
mm := pos/3 + 1
tmstr := malloc(s.len*2)
count := int(C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str,
fields[3].str, mm, fields[1].str, fields[4].str))
pos := months_string.index(fields[2]) or {
return Time{}
}
mm := pos / 3 + 1
tmstr := malloc(s.len * 2)
count := int(C.sprintf(charptr(tmstr), '%s-%02d-%s %s'.str, fields[3].str, mm, fields[1].str, fields[4].str))
return parse(tos(tmstr, count))
}
pub fn new_time(t Time) Time {
return Time{
year: t.year,
month: t.month,
day: t.day,
hour: t.hour,
minute: t.minute,
second: t.second,
year: t.year
month: t.month
day: t.day
hour: t.hour
minute: t.minute
second: t.second
unix: t.calc_unix()
}
//TODO: Use the syntax below when it works with reserved keywords like `unix`
// TODO: Use the syntax below when it works with reserved keywords like `unix`
/*
return {
t |
unix:t.calc_unix()
}
*/
}
pub fn (t &Time) calc_unix() int {
@ -340,8 +326,7 @@ pub fn ticks() i64 {
$if windows {
return C.GetTickCount()
} $else {
ts := C.timeval{
}
ts := C.timeval{}
C.gettimeofday(&ts, 0)
return i64(ts.tv_sec * u64(1000) + (ts.tv_usec / u64(1000)))
}
@ -361,19 +346,20 @@ pub fn sleep(seconds int) {
}
}
pub fn usleep(n int) {
pub fn sleep_ms(milliseconds int) {
$if windows {
// C._usleep(n)
C.Sleep(milliseconds)
} $else {
C.usleep(n)
C.usleep(milliseconds * 1000)
}
}
pub fn sleep_ms(n int) {
pub fn usleep(microseconds int) {
$if windows {
C.Sleep(n)
milliseconds := microseconds / 1000
C.Sleep(milliseconds)
} $else {
C.usleep(n * 1000)
C.usleep(microseconds)
}
}
@ -416,7 +402,8 @@ pub fn (t Time) get_fmt_time_str(fmt_time FormatTime) string {
'${t.hour:02d}:${t.minute:02d}:${t.second:02d}'
}
else {
'unknown enumeration $fmt_time'}}
'unknown enumeration $fmt_time'}
}
}
// get_fmt_date_str returns a string for t in a given date format
@ -496,4 +483,3 @@ pub fn (t Time) get_fmt_str(fmt_dlmtr FormatDelimiter, fmt_time FormatTime, fmt_
}
}
}