time: gmt offset; macos syscalls

pull/3293/head
Alexander Medvednikov 2019-12-31 19:53:15 +01:00
parent 3c17851200
commit 87cff0386c
9 changed files with 63 additions and 33 deletions

View File

@ -54,7 +54,7 @@ fn new_cgen(out_name_c string) &CGen {
out_path: path
out: out
// buf: strings.new_builder(10000)
lines: make(0, 1000, sizeof(string))
}
return gen
@ -347,6 +347,9 @@ fn os_name_to_ifdef(name string) string {
'haiku' {
return '__haiku__'
}
'linux_or_macos' {
return ''
}
else {
verror('bad os ifdef name "$name"')
}}

View File

@ -27,15 +27,21 @@ fn (p mut Parser) comp_time() {
if name == 'mac' {
p.warn('use `macos` instead of `mac`')
}
if not {
p.genln('#ifndef $ifdef_name')
}
else {
p.genln('#ifdef $ifdef_name')
if name == 'linux_or_macos' {
p.genln('#if defined(__linux) || defined(__APPLE__)')
} else {
p.genln('#ifdef $ifdef_name')
}
}
p.check(.lcbr)
os := os_from_string(name)
if ((!not && os != p.os) || (not && os == p.os)) && !p.scanner.is_fmt && !p.pref.output_cross_c {
if ((!not && os != p.os) || (not && os == p.os)) && !name.contains('_or_') &&
!p.scanner.is_fmt && !p.pref.output_cross_c {
// `$if os {` for a different target, skip everything inside
// to avoid compilation errors (like including <windows.h>
// on non-Windows systems)

View File

@ -31,7 +31,7 @@ enum BuildMode {
const (
supported_platforms = ['windows', 'mac', 'macos', 'linux', 'freebsd', 'openbsd', 'netbsd',
'dragonfly', 'android', 'js', 'solaris', 'haiku']
'dragonfly', 'android', 'js', 'solaris', 'haiku', 'linux_or_macos']
)
enum OS {
@ -1281,6 +1281,9 @@ pub fn os_from_string(os string) OS {
'haiku' {
return .haiku
}
'linux_or_macos' {
return .linux
}
else {
panic('bad os $os')
}}

View File

@ -330,6 +330,7 @@ pub fn (f mut File) close() {
}
f.opened = false
$if linux {
//$if linux_or_macos {
C.syscall(sys_close, f.fd)
return
}

View File

@ -0,0 +1,18 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module os
pub const (
sys_write = 4
sys_open = 5
sys_close = 6
sys_mkdir = 136
sys_creat = 8
sys_open_nocancel = 398
sys_stat64 = 338
)

View File

@ -7,11 +7,20 @@ module os
const (
PROT_READ = 1
PROT_WRITE = 2
MAP_PRIVATE = 0x02
MAP_ANONYMOUS = 0x20
)
pub const (
sys_write = 1
sys_open = 2
sys_close = 3
sys_mkdir = 83
sys_creat = 85
)
/*
// TODO no pub => error
@ -32,7 +41,7 @@ pub fn println(s string) {
fn mmap(start voidptr, len, prot, flags, fd, off int) byteptr {
return syscall6(9, start, len, prot, flags, fd, off) // sys_mmap
}
}
pub fn malloc(n int) byteptr {
println('malloc($n)')
@ -41,7 +50,7 @@ pub fn malloc(n int) byteptr {
}
pub fn free(b byteptr) {
}
*/

View File

@ -6,14 +6,6 @@ pub const (
path_separator = '/'
)
pub const (
sys_write = 1
sys_open = 2
sys_close = 3
sys_mkdir = 83
sys_creat = 85
)
const (
stdin_value = 0
stdout_value = 1
@ -77,6 +69,7 @@ pub fn is_dir(path string) bool {
pub fn open(path string) ?File {
$if linux {
//$if linux_or_macos {
fd := C.syscall(sys_open, path.str, 511)
if fd == -1 {
return error('failed to open file "$path"')
@ -103,8 +96,16 @@ pub fn open(path string) ?File {
// create creates a file at a specified location and returns a writable `File` object.
pub fn create(path string) ?File {
$if linux {
fd := C.syscall(sys_creat, path.str, 511)
//////println('Fd=$fd')
//$if linux_or_macos {
mut fd := 0
println('creat SYS')
$if macos {
fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6)
}
$else {
fd = C.syscall(sys_creat, path.str, 511)
}
println('fd=$fd')
if fd == -1 {
return error('failed to create file "$path"')
}
@ -134,6 +135,7 @@ pub fn (f mut File) write(s string) {
return
}
$if linux {
//$if linux_or_macos {
C.syscall(sys_write, f.fd, s.str, s.len)
return
}
@ -146,6 +148,7 @@ pub fn (f mut File) writeln(s string) {
if !f.opened {
return
}
//$if linux_or_macos {
$if linux {
snl := s + '\n'
C.syscall(sys_write, f.fd, snl.str, snl.len)

View File

@ -79,6 +79,7 @@ struct C.tm {
tm_hour int
tm_min int
tm_sec int
tm_gmtoff int // seconds
}
fn C.time(int) C.time_t
@ -106,7 +107,7 @@ pub fn convert_ctime(t tm) Time {
minute: t.tm_min
second: t.tm_sec
unix: C.mktime(&t)
}
}.add_seconds(t.tm_gmtoff)
}
// format_ss returns a string for t in a given format YYYY-MM-DD HH:MM:SS in

View File

@ -22,14 +22,12 @@ fn test_is_leap_year() {
}
fn test_now_format() {
/*
t := time.now()
u:=t.uni
u:=t.unix
println(u)
println(t.format())
println(time.unix(u).format())
assert t.format() == time.unix(u).format()
*/
}
fn check_days_in_month(month, year, expected int) bool {
@ -99,18 +97,6 @@ fn test_unix() {
assert t6.second == 29
}
fn test_unix2() {
/*
println(t.year)
assert t.year == 2019
assert t.month == 12
assert t.day == 31
assert t.hour == 8
assert t.minute == 9
assert t.second == 53
*/
}
fn test_format_ss() {
assert '11.07.1980 21:23:42' == time_to_test.get_fmt_str(.dot, .hhmmss24, .ddmmyyyy)
}