Skip to content

Commit

Permalink
std/time: add location support
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Nov 21, 2024
1 parent 886c527 commit b2a8f9b
Show file tree
Hide file tree
Showing 11 changed files with 1,872 additions and 14 deletions.
41 changes: 41 additions & 0 deletions std/time/go.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

// The Jule implementation of Unix time conversions, TZ-handling and some other
// algoritmhs is based on the original Go code.
//
// The Jule implementation will not follows Go code.
// It only uses Go implementation as base implementation.
// API may be work/implemented different.
//
// The original Go code came with this notice:
// ====================================================
// Copyright (c) 2009 The Go Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// ====================================================
8 changes: 8 additions & 0 deletions std/time/sys.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// 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.

// Copies of os.Seek.* enum fields to avoid importing "std/os":
const seekStart = 0
const seekCurrent = 1
const seekEnd = 2
48 changes: 48 additions & 0 deletions std/time/sys_unix.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.

use integ "std/jule/integrated"
use "std/sys"

fn open(name: str): (uintptr, ok: bool) {
sName := integ::StrToBytes(name)
fd := unsafe { sys::Open(&sName[0], sys::O_RDONLY, 0) }
if fd == -1 {
ret 0, false
}
ret uintptr(fd), true
}

fn read(fd: uintptr, mut buf: []byte): (n: int, ok: bool) {
if len(buf) == 0 {
// If the caller wanted a zero byte read, return immediately
// without trying to read.
ret 0, true
}
n = unsafe { sys::Read(int(fd), &buf[0], uint(len(buf))) }
ok = n != -1
ret
}

fn preadn(fd: uintptr, mut buf: []byte, off: int): (ok: bool) {
mut whence := seekStart
if off < 0 {
whence = seekEnd
}
if sys::Seek(int(fd), off, whence) == -1 {
ret false
}
for len(buf) > 0 {
m := unsafe { sys::Read(int(fd), &buf[0], uint(len(buf))) }
if m <= 0 {
ret false
}
buf = buf[m:]
}
ret true
}

fn closefd(fd: uintptr) {
sys::Close(int(fd))
}
48 changes: 48 additions & 0 deletions std/time/sys_windows.jule
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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.

use integ "std/jule/integrated"
use "std/sys"

fn open(name: str): (uintptr, ok: bool) {
sName := integ::UTF16FromStr(name)
fd := unsafe { sys::Wopen(&sName[0], sys::O_RDONLY, 0) }
if fd == -1 {
ret 0, false
}
ret uintptr(fd), true
}

fn read(fd: uintptr, mut buf: []byte): (n: int, ok: bool) {
if len(buf) == 0 {
// If the caller wanted a zero byte read, return immediately
// without trying to read.
ret 0, true
}
n = unsafe { sys::Read(int(fd), &buf[0], uint(len(buf))) }
ok = n != -1
ret
}

fn preadn(fd: uintptr, mut buf: []byte, off: int): (ok: bool) {
mut whence := seekStart
if off < 0 {
whence = seekEnd
}
if sys::Seek(int(fd), off, whence) == -1 {
ret false
}
for len(buf) > 0 {
m := unsafe { sys::Read(int(fd), &buf[0], uint(len(buf))) }
if m <= 0 {
ret false
}
buf = buf[m:]
}
ret true
}

fn closefd(fd: uintptr) {
sys::Close(int(fd))
}
Loading

0 comments on commit b2a8f9b

Please sign in to comment.