mirror of
https://github.com/Nexoniarz/Finch.git
synced 2026-11-03 00:44:27 +00:00
import json import path import time import proc -- found in std/ after the program's own files, so a program's own json.fch always wins. path: join, base, dir, ext, stem, parts, clean, is_abs. time: now, millis, sleep, date, local, format, days_in_month (clock_gettime, strftime through C import). proc: run, output (an error with the command's output when it fails), env, pid, cwd. json: parse (errors with line and column), stringify, pretty, new_*, get/at/set/push/has/names and str_or/int_or/float_or/bool_or; \u escapes with surrogate pairs. The C importer now leaves a plain (non-const) char * parameter a ptr[char], a buffer C writes into; only const char * is text going in. str(ptr) and str([]u8) are in the self-hosted compiler. boot.sh: 40 passed, 0 failed.
88 lines
2.5 KiB
Text
88 lines
2.5 KiB
Text
// Time: the clock, sleeping, and dates.
|
|
//
|
|
// import time
|
|
// start := time.millis() milliseconds from a clock that only goes forward (to measure with)
|
|
// time.sleep(250) wait a quarter of a second
|
|
// time.now() seconds since 1970-01-01 00:00 UTC
|
|
// d := time.date(time.now()) the parts: d.year, d.month, d.day, d.hour, d.minute, d.second
|
|
// time.format(time.now(), "%Y-%m-%d %H:%M") as text, in the computer's time zone
|
|
import "time.h"
|
|
|
|
struct Date {
|
|
int year
|
|
int month // 1 to 12
|
|
int day // 1 to 31
|
|
int hour
|
|
int minute
|
|
int second
|
|
int weekday // 0 is Sunday
|
|
int yearday // 1 to 366
|
|
}
|
|
|
|
// seconds since 1970-01-01 00:00 UTC
|
|
fn now() -> int {
|
|
return int(time(null))
|
|
}
|
|
|
|
// milliseconds on a clock that only goes forward: for measuring how long something took
|
|
fn millis() -> int {
|
|
timespec ts
|
|
clock_gettime(CLOCK_MONOTONIC, addr(ts))
|
|
return int(ts.tv_sec) * 1000 + int(ts.tv_nsec) / 1000000
|
|
}
|
|
|
|
fn sleep(int ms) {
|
|
timespec ts
|
|
ts.tv_sec = ms / 1000
|
|
ts.tv_nsec = (ms % 1000) * 1000000
|
|
nanosleep(addr(ts), null)
|
|
}
|
|
|
|
fn parts(tm t) -> Date {
|
|
return Date(year: int(t.tm_year) + 1900, month: int(t.tm_mon) + 1, day: int(t.tm_mday), hour: int(t.tm_hour),
|
|
minute: int(t.tm_min), second: int(t.tm_sec), weekday: int(t.tm_wday), yearday: int(t.tm_yday) + 1)
|
|
}
|
|
|
|
// the date and time of a moment, in UTC
|
|
fn date(int unix) -> Date {
|
|
tm t
|
|
i64 stamp = unix
|
|
gmtime_r(addr(stamp), addr(t))
|
|
return parts(t)
|
|
}
|
|
|
|
// the date and time of a moment, in the computer's time zone
|
|
fn local(int unix) -> Date {
|
|
tm t
|
|
i64 stamp = unix
|
|
localtime_r(addr(stamp), addr(t))
|
|
return parts(t)
|
|
}
|
|
|
|
// a moment as text, in the computer's time zone: %Y year, %m month, %d day, %H hour, %M minute, %S second
|
|
// (the rest of C's strftime works too: %A weekday name, %B month name, ...)
|
|
fn format(int unix, str pattern) -> str {
|
|
tm t
|
|
i64 stamp = unix
|
|
localtime_r(addr(stamp), addr(t))
|
|
[]char buf
|
|
for i in 0..256 {
|
|
buf.push(char(0))
|
|
}
|
|
strftime(buf.ptr, 256, pattern, addr(t))
|
|
return str(buf.ptr)
|
|
}
|
|
|
|
// the number of days in a month (month is 1 to 12)
|
|
fn days_in_month(int year, int month) -> int {
|
|
if month == 2 {
|
|
if year % 4 == 0 && (year % 100 != 0 || year % 400 == 0) {
|
|
return 29
|
|
}
|
|
return 28
|
|
}
|
|
if month == 4 || month == 6 || month == 9 || month == 11 {
|
|
return 30
|
|
}
|
|
return 31
|
|
}
|