1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

Replace all remaining C code with V in the compiler and vlib (hoorah!)

This commit is contained in:
Alexander Medvednikov
2019-06-27 19:02:47 +02:00
parent 554f083543
commit 6824e6e7db
19 changed files with 391 additions and 685 deletions

View File

@@ -1,17 +0,0 @@
// 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 time
struct Info {
pub:
year int
month int
day int
hour int
minute int
second int
yday int
wday int
}

View File

@@ -4,104 +4,277 @@
module time
import rand
#include <time.h>
struct Time {
sec i64
nsec i32
mono i64
pub:
year int
month int
day int
hour int
minute int
second int
uni int // TODO it's safe to use "unix" now
}
fn (a Time) + (b Time) Time {
sec := a.sec + b.sec
nsec := a.nsec + b.nsec
return Time{
sec: sec + i64(nsec)/i64(1000000000)
nsec: nsec % i32(1000000000)
fn C.localtime(int) *C.tm
fn remove_me_when_c_bug_is_fixed() { // TODO
}
struct C.tm {
tm_year int
tm_mon int
tm_mday int
tm_hour int
tm_min int
tm_sec int
}
pub fn now() Time {
t := C.time(0)
mut now := &C.tm{!}
now = C.localtime(&t)
return convert_ctime(now)
}
pub fn random() Time {
return Time {
year: rand.next(2) + 201
month: rand.next(12) + 1
day: rand.next(30) + 1
hour: rand.next(24)
minute: rand.next(60)
second: rand.next(60)
}
}
fn (a Time) - (b Time) Time {
sec := a.sec - b.sec - i64(1)
nsec := a.nsec - b.nsec + i32(1000000000)
t := Time{
sec: sec + i64(nsec)/i64(1000000000)
nsec: nsec % i32(1000000000)
}
if a.mono <= i64(0) || b.mono <= i64(0) {
return t
}
mono := a.mono - b.mono
if mono > i64(0) && t.sec >= i64(0) && t.nsec >= i32(0) {
return t
}
if mono < i64(0) && t.sec <= i64(0) && t.nsec <= i32(0) {
return t
}
if mono == i64(0) {
return Time{}
}
return Time{
sec: mono / i64(1000000000)
nsec: i32(mono%i64(1000000000))
}
pub fn unix(u int) Time {
mut t := &C.tm{!}
t = C.localtime(&u)
return convert_ctime(t)
}
fn (t Time) days() f64 {
return f64(t.sec)/86400.0
}
fn (t Time) hours() f64 {
return f64(t.sec)/3600.0
}
fn (t Time) minutes() f64 {
return f64(t.sec)/60.0
}
fn (t Time) seconds() f64 {
return f64(t.sec) + f64(t.nsec)/1000000000.0
}
fn (t Time) milliseconds() f64 {
return 1000.0*f64(t.sec) + f64(t.nsec)/1000000.0
}
fn (t Time) microseconds() f64 {
return 1000000.0*f64(t.sec) + f64(t.nsec)/1000.0
}
fn (t Time) nanoseconds() f64 {
return 1000000000.0*f64(t.sec) + f64(t.nsec)
}
fn (t Time) str() string {
if t.sec == i64(0) {
if t.nsec == i32(0) {
return '0s'
}
if t.nsec < i32(1000) && t.nsec > i32(-1000) {
return '${t.nsec}ns'
}
if t.nsec < i32(1000000) && t.nsec > i32(-1000000) {
return '${f64(t.nsec)/1000.0:.1f}µs'
}
if t.nsec < i32(1000000000) && t.nsec > i32(-1000000000) {
return '${f64(t.nsec)/1000000.0:.1f}ms'
}
pub fn convert_ctime(t tm) Time {
return Time {
year: t.tm_year + 1900
month: t.tm_mon + 1
day: t.tm_mday
hour: t.tm_hour
minute: t.tm_min
second: t.tm_sec
}
if t.sec < i64(60) && t.sec > i64(-60) {
return '${f64(t.sec)+f64(t.nsec)/1000000000.0:.1f}s'
}
if t.sec < i64(3600) && t.sec > i64(-3600) {
return '${f64(t.sec)/60.0:.1f}m'
}
if t.sec < i64(86400) && t.sec > i64(-86400) {
return '${f64(t.sec)/3600.0:.1f}h'
}
if t.sec < i64(864000) && t.sec > i64(-864000) {
return '${f64(t.sec)/86400.0:.1f}d'
}
return '${f64(t.sec)/86400.0:.0f}d'
// uni = uni;
}
fn (t Time) format_ss() string {
return '${t.year}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}:${t.second:02d}'
}
pub fn (t Time) format() string {
return '${t.year}-${t.month:02d}-${t.day:02d} ${t.hour:02d}:${t.minute:02d}'
}
const (
Months = 'JanFebMarAprMayJunJulAugSepOctNovDec'
Days = 'MonTueWedThuFriSatSun'
)
pub fn (t Time) smonth() string {
i := t.month - 1
return Months.substr(i * 3, (i + 1) * 3)
}
// 21:04
pub fn (t Time) hhmm() string {
return '${t.hour:02d}:${t.minute:02d}'
}
/*
fn (t Time) hhmm_tmp() string {
return '${t.hour:02d}:${t.minute:02d}'
}
*/
// 9:04pm
pub fn (t Time) hhmm12() string {
mut am := 'am'
mut hour = t.hour
if t.hour > 11 {
am = 'pm'
}
if t.hour > 12 {
hour = hour - 12
}
if t.hour == 0 {
hour = 12
}
return '$hour:${t.minute:02d} $am'
}
// 21:04:03
fn (t Time) hhmmss() string {
return '${t.hour:02d}:${t.minute:02d}:${t.second:02d}'
}
// 2012-01-05
fn (t Time) ymmdd() string {
return '${t.year}-${t.month:02d}-${t.day:02d}'
}
// Jul 3
fn (t Time) md() string {
// jl := t.smonth()
s := '${t.smonth()} $t.day'
return s
}
pub fn (t Time) clean() string {
nowe := time.now()
// if amtime {
// hm = t.Format("3:04 pm")
// }
// Today
if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day {
return t.hhmm()
}
// This week
// if time.Since(t) < 24*7*time.Hour {
// return t.Weekday().String()[:3] + " " + hm
// }
// This year
if t.year == nowe.year {
return '${t.smonth()} ${t.day} ${t.hhmm()}'
}
return t.format()
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
}
fn (t Time) clean12() string {
nowe := time.now()
// if amtime {
// hm = t.Format("3:04 pm")
// }
// Today
if t.month == nowe.month && t.year == nowe.year && t.day == nowe.day {
return t.hhmm12()
}
// This week
// if time.Since(t) < 24*7*time.Hour {
// return t.Weekday().String()[:3] + " " + hm
// }
// This year
if t.year == nowe.year {
return '${t.smonth()} ${t.day} ${t.hhmm12()}'
}
return t.format()
// return fmt.Sprintf("%4d/%02d/%02d", t.Year(), t.Month(), t.Day()) + " " + hm
}
/*
// in ms
fn ticks() double {
return 0
}
*/
// `parse` parses time in the following format: "2018-01-27 12:48:34"
pub fn parse(s string) Time {
// println('parse="$s"')
pos := s.index(' ')
if pos <= 0 {
println('bad time format')
return now()
}
symd := s.left(pos)
ymd := symd.split('-')
if ymd.len != 3 {
println('bad time format')
return now()
}
shms := s.right(pos)
hms := shms.split(':')
hour := hms[0]
minute := hms[1]
second := hms[2]
// //////////
return new_time(Time {
year: ymd[0].int()
month: ymd[1].int()
day: ymd[2].int()
hour: hour.int()
minute: minute.int()
second: second.int()
})
}
fn new_time(t Time) Time {
return{t | uni: t.calc_unix()}
}
fn (t &Time) calc_unix() int {
if t.uni != 0 {
return t.uni
}
tt := C.tm{
tm_sec : t.second
tm_min : t.minute
tm_hour : t.hour
tm_mday : t.day
tm_mon : t.month-1
tm_year : t.year - 1900
}
return C.mktime(&tt)
}
// TODO add(d time.Duration)
pub fn (t Time) add_seconds(seconds int) Time {
return unix(t.uni + seconds)
}
// TODO use time.Duration instead of seconds
fn since(t Time) int {
return 0
}
pub fn (t Time) relative() string {
now := time.now()
secs := now.uni - t.uni
if secs <= 30 {
// right now or in the future
// TODO handle time in the future
return 'now'
}
if secs < 60 {
return '1m'
}
if secs < 3600 {
return '${secs/60}m'
}
if secs < 3600 * 24 {
return '${secs/3600}h'
}
if secs < 3600 * 24 * 5 {
return '${secs/3600/24}d'
}
if secs > 3600 * 24 * 10000 {
return ''
}
return t.md()
}
fn day_of_week(y, m, d int) int {
// TODO please no
//# return (d += m < 3 ? y-- : y - 2, 23*m/9 + d + 4 + y/4- y/100 + y/400)%7;
return 0
}
pub fn (t Time) day_of_week() int {
return day_of_week(t.year, t.month, t.day)
}
// weekday_str() returns the current day in string (upto 3 characters)
pub fn (t Time) weekday_str() string {
i := t.day_of_week() - 1
return Days.substr(i * 3, (i + 1) * 3)
}

View File

@@ -4,84 +4,16 @@
module time
pub fn now() Time {
# struct timespec t = {0, 0};
# struct timespec m = {0, 0};
# clock_gettime(CLOCK_REALTIME, &t);
# clock_gettime(CLOCK_MONOTONIC_RAW, &m);
res := Time{}
# res.sec = t.tv_sec;
# res.nsec = t.tv_nsec;
# res.mono = 1000000000*m.tv_sec + m.tv_nsec;
return res
// in ms
pub fn ticks() f64 {
return f64(0)
}
pub fn sleep(t Time) {
if t.sec > i64(0) {
C.sleep(t.sec)
}
if t.nsec > i32(0) {
C.usleep((t.nsec+i32(999))/i32(1000))
}
pub fn sleep(seconds int) {
C.sleep(seconds)
}
fn (t Time) local() Info {
info := Info{}
# struct tm tm;
# localtime_r(&t.sec, &tm);
# info.year = tm.tm_year + 1900;
# info.month = tm.tm_mon + 1;
# info.day = tm.tm_mday;
# info.hour = tm.tm_hour;
# info.minute = tm.tm_min;
# info.second = tm.tm_sec;
# info.yday = tm.tm_yday;
# info.wday = tm.tm_wday;
return info
pub fn sleep_ms(seconds int) {
C.usleep(seconds * 1000)
}
fn (t Time) utc() Info {
info := Info{}
# struct tm tm;
# gmtime_r(&t.sec, &tm);
# info.year = tm.tm_year + 1900;
# info.month = tm.tm_mon + 1;
# info.day = tm.tm_mday;
# info.hour = tm.tm_hour;
# info.minute = tm.tm_min;
# info.second = tm.tm_sec;
# info.yday = tm.tm_yday;
# info.wday = tm.tm_wday;
return info
}
fn (t Time) format(fmt string) string {
res := ''
cfmt := fmt.cstr()
# char buf[1024];
# struct tm tm;
# localtime_r(&t.sec, &tm);
# strftime(buf, 1024, cfmt, &tm);
# res = tos2(buf);
return res
}
pub fn parse(s, fmt string) ?Time {
t := Time{}
cs := s.cstr()
cfmt := fmt.cstr()
ok := 0
# struct tm tm;
# memset(&tm, 0, sizeof(struct tm));
# ok = strptime(cs, cfmt, &tm);
if ok == 0 {
return error('time.parse: invalid time string')
}
# t.sec = mktime(&tm);
if t.sec < i64(0) {
return error('time.parse: invalid time string')
}
return t
}

View File

@@ -4,66 +4,30 @@
module time
// TODO: nanosecond and monotonic
pub fn now() Time {
# time_t t = time(0);
//#flag -framework CoreServices
//#include <CoreServices/CoreServices.h>
//#include <mach/mach_time.h>
res := Time{}
# res.sec = t;
return res
// in ms
pub fn ticks() f64 {
panic('not implemented')
/*
t := i64(C.mach_absolute_time())
# Nanoseconds elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *) &t );
# return (double)(* (uint64_t *) &elapsedNano) / 1000000;
*/
return f64(0)
}
pub fn sleep(t Time) {
if t.sec > i64(0) {
C.sleep(t.sec)
}
if t.nsec > i32(0) {
C.usleep((t.nsec+i32(999))/i32(1000))
}
pub fn sleep(seconds int) {
C.sleep(seconds)
}
// TODO: Thread safety
fn (t Time) local() Info {
info := Info{}
# struct tm *tm = localtime(&t.sec);
# info.year = tm->tm_year + 1900;
# info.month = tm->tm_mon + 1;
# info.day = tm->tm_mday;
# info.hour = tm->tm_hour;
# info.minute = tm->tm_min;
# info.second = tm->tm_sec;
# info.yday = tm->tm_yday;
# info.wday = tm->tm_wday;
return info
pub fn usleep(seconds int) {
C.usleep(seconds)
}
// TODO: Thread safety
fn (t Time) utc() Info {
info := Info{}
# struct tm *tm = gmtime(&t.sec);
# info.year = tm->tm_year + 1900;
# info.month = tm->tm_mon + 1;
# info.day = tm->tm_mday;
# info.hour = tm->tm_hour;
# info.minute = tm->tm_min;
# info.second = tm->tm_sec;
# info.yday = tm->tm_yday;
# info.wday = tm->tm_wday;
return info
pub fn sleep_ms(seconds int) {
C.usleep(seconds * 1000)
}
// TODO: Thread safety
fn (t Time) format(fmt string) string {
res := ''
cfmt := fmt.cstr()
# char buf[1024];
# struct tm *tm = localtime(&t.sec);
# strftime(buf, 1024, cfmt, tm);
# res = tos2(buf);
return res
}
// TODO: Not implemented yet
pub fn parse(s string) ?Time {
return Time{}
}

View File

@@ -4,63 +4,21 @@
module time
// TODO: nanosecond and monotonic
pub fn now() Time {
# time_t t = time(0);
res := Time{}
# res.sec = t;
return res
// in ms
fn ticks() double {
return C.GetTickCount()
}
pub fn sleep(t Time) {
if t.sec > i64(0) || t.nsec > i32(0) {
C.Sleep(i64(1000)*t.sec+(i64(t.nsec)+i64(999999))/i64(1000000))
}
fn sleep(seconds int) {
C._sleep(seconds * 1000)
}
// TODO: Thread safety
fn (t Time) local() Info {
info := Info{}
# struct tm *tm = localtime(&t.sec);
# info.year = tm->tm_year + 1900;
# info.month = tm->tm_mon + 1;
# info.day = tm->tm_mday;
# info.hour = tm->tm_hour;
# info.minute = tm->tm_min;
# info.second = tm->tm_sec;
# info.yday = tm->tm_yday;
# info.wday = tm->tm_wday;
return info
fn usleep(seconds int) {
panic('usleep not impl')
// C._usleep(seconds)
}
// TODO: Thread safety
fn (t Time) utc() Info {
info := Info{}
# struct tm *tm = gmtime(&t.sec);
# info.year = tm->tm_year + 1900;
# info.month = tm->tm_mon + 1;
# info.day = tm->tm_mday;
# info.hour = tm->tm_hour;
# info.minute = tm->tm_min;
# info.second = tm->tm_sec;
# info.yday = tm->tm_yday;
# info.wday = tm->tm_wday;
return info
fn sleep_ms(n int) {
C.Sleep(n)
}
// TODO: Thread safety
fn (t Time) format(fmt string) string {
res := ''
cfmt := fmt.cstr()
# char buf[1024];
# struct tm *tm = localtime(&t.sec);
# strftime(buf, 1024, cfmt, tm);
# res = tos2(buf);
return res
}
// TODO: Not implemented yet
pub fn parse(s string) ?Time {
return Time{}
}

View File

@@ -1,66 +0,0 @@
// 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 time
pub fn make(i Info) ?Time {
t := Time{}
# struct tm tm;
# tm.tm_year = i.year - 1900;
# tm.tm_mon = i.month - 1;
# tm.tm_mday = i.day;
# tm.tm_hour = i.hour;
# tm.tm_min = i.minute;
# tm.tm_sec = i.second;
# tm.tm_yday = i.yday;
# tm.tm_wday = i.wday;
# tm.tm_isdst = 0;
# t.sec = mktime(&tm);
if t.sec < i64(0) {
return error('time.make: invalid time infomation')
}
return t
}
pub fn days(n int) Time {
return Time{
sec: 86400*n
}
}
pub fn hours(n int) Time {
return Time{
sec: 3600*n
}
}
pub fn minutes(n int) Time {
return Time{
sec: 60*n
}
}
pub fn seconds(n int) Time {
return Time{
sec: n
}
}
pub fn milliseconds(n int) Time {
return Time{
nsec: 1000000*n
}
}
pub fn microseconds(n int) Time {
return Time{
nsec: 1000*n
}
}
pub fn nanoseconds(n int) Time {
return Time{
nsec: n
}
}