loads of things

This commit is contained in:
Greg Gauthier 2025-04-09 19:57:45 +01:00
parent 6dc7f49357
commit f7474e911b
11 changed files with 250 additions and 0 deletions

7
src/divs.a68 Normal file
View File

@ -0,0 +1,7 @@
BEGIN
INT diva := 7 OVER 3;
INT divb := 7 MOD 3;
print(diva);
print(divb)
END

41
src/mktime.a68 Normal file
View File

@ -0,0 +1,41 @@
BEGIN
INT days per year = 365,
days per leap year = 366,
secs per day = 24 * 3600,
secs per hour = 3600,
secs per min = 60;
[1:12] INT days in month := (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
PROC to epoch secs = ([] INT t) LONG REAL:
BEGIN
INT year = t[1], month = t[2], day = t[3],
hour = t[4], min = t[5], sek = t[6];
# Ignoring dow (t[7]) and dst (t[8]) for simplicity #
# Years since 1970 #
INT years = year - 1970;
INT leap years = years OVER 4 - years OVER 100 + years OVER 400; # Leap rule #
LONG INT days = years * days per year + leap years;
# Adjust February for current year #
IF year MOD 4 = 0 AND (year MOD 100 /= 0 OR year MOD 400 = 0)
THEN days in month[2] := 29 FI;
# Days in prior months this year #
FOR m FROM 1 TO month - 1 DO
days +:= days in month[m]
OD;
days +:= day - 1; # Current day (0-based from month start) #
# Total seconds #
LONG REAL total = days * secs per day + hour * secs per hour + min * secs per min + sek;
total
END;
# Test it #
[] INT lt = local time;
print(("array: ", lt, newline));
LONG REAL secs = to epoch secs(lt);
print(("seconds: ", secs, newline))
END

36
src/mktime2.a68 Normal file
View File

@ -0,0 +1,36 @@
BEGIN
LONG INT days per year = 365,
days per leap year = 366,
secs per day = 24 * 3600,
secs per hour = 3600,
secs per min = 60;
[1:12] LONG INT days in month := (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
PROC to epoch secs = ([] INT t) LONG REAL:
BEGIN
LONG INT year = t[1], month = t[2], day = t[3],
hour = t[4], min = t[5], sek = t[6];
LONG INT years = year - 1970;
LONG INT leap years = years OVER 4 - years OVER 100 + years OVER 400;
LONG INT days = years * days per year + leap years;
IF year MOD 4 = 0 AND (year MOD 100 /= 0 OR year MOD 400 = 0)
THEN days in month[2] := 29
FI;
FOR m FROM 1 TO month - 1 DO
days +:= days in month[m]
OD;
days +:= day - 1;
LONG REAL total = days * secs per day + hour * secs per hour + min * secs per min + sek;
total
END;
[] INT lt = local time;
print(("array: ", lt, newline));
LONG REAL secs = to epoch secs(lt);
print(("seconds: ", secs, newline))
END

45
src/mktime3.a68 Normal file
View File

@ -0,0 +1,45 @@
# ------------------------------------------
mktime:
Mimics the behavior of unix "mktime". Takes the Algol 68 "local time" array as
input, and outputs the number of seconds since Jan 1, 1970. Algol "Local time"
produces the following output: [YEAR, MONTH, DAY, HOUR, MINS, SECS]
------------------------------------------ #
BEGIN
INT days per year = 365,
secs per day = 24 * 3600,
secs per hour = 3600,
secs per min = 60;
[1:12] INT days in month := (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
PROC to epoch secs = ([] INT t) LONG REAL:
BEGIN
# Parses up the output from "local time" #
INT year = t[1], month = t[2], day = t[3],
hour = t[4], min = t[5], sek = t[6];
INT years = year - 1970; # uses unix epoch year #
# Don't forget the leap years! #
INT leap years = years OVER 4 - years OVER 100 + years OVER 400;
INT days := years * days per year + leap years;
IF year MOD 4 = 0 AND (year MOD 100 /= 0 OR year MOD 400 = 0)
THEN days in month[2] := 29 FI;
INT month days := 0;
FOR m FROM 1 TO month - 1 DO
month days := month days + days in month[m]
OD;
days := days + month days + (day - 1);
LONG REAL total = days * secs per day + hour * secs per hour + min * secs per min + sek;
total
END;
[] INT lt = local time;
print(("array: ", lt, newline));
LONG REAL secs = to epoch secs(lt);
print(("seconds: ", secs, newline))
END

26
src/postgres-test-4.a68 Normal file
View File

@ -0,0 +1,26 @@
BEGIN
FILE db;
STRING result; # Renamed for clarity #
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
INT status = pq connect db(db, conninfo, result);
IF status = 0 THEN
print(("Connected!", newline));
INT qstatus = pq exec(db, "SELECT version()");
IF qstatus = 0 THEN
INT nrows = pq ntuples(db);
print(("Rows: ", nrows, newline));
INT fetch = pq get value(db, 1, 1);
IF fetch = 0 THEN
print(("Version: ", result, newline))
ELSE
print(("Fetch failed: ", fetch, newline))
FI
ELSE
print(("Query failed: ", qstatus, newline))
FI;
INT finish status = pq finish(db);
print(("Finish: ", finish status, newline))
ELSE
print(("Failed: ", result, newline))
FI
END

22
src/postgres-test.a68 Normal file
View File

@ -0,0 +1,22 @@
BEGIN
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
REF FILE db = pq connect db(conninfo);
IF db ISNT REF FILE(NIL)
THEN
BEGIN
REF pqresult res; # Declare at block start #
print(("Connected!", newline));
res := pq exec(db, "SELECT version()");
IF res ISNT REF pqresult(NIL) THEN
STRING ver = pq get value(res, 0, 0);
print(("Postgres version: ", ver, newline));
pq clear(res)
ELSE
print(("Query failed", newline))
FI;
pq finish(db)
END
ELSE
print(("Connection failed", newline))
FI
END

14
src/postgres-test2.a68 Normal file
View File

@ -0,0 +1,14 @@
BEGIN
FILE db;
STRING err;
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
INT status = pq connect db(db, conninfo, err);
print(("Status: ", status, newline));
IF status = 0 THEN
print(("Connected!", newline));
INT finish status = pq finish(db);
print(("Finish status: ", finish status, newline))
ELSE
print(("Failed: ", err, newline))
FI
END

17
src/postgres-test3.a68 Normal file
View File

@ -0,0 +1,17 @@
BEGIN
FILE db;
STRING err;
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
INT status = pq connect db(db, conninfo, err);
IF status = 0 THEN
print(("Connected!", newline));
printf (($"protocol="g (0)x"server="g (0)x"socket="g (0)x"pid="g (0)l$,
pq protocol version (db), pq server version (db), pq socket (db),
pq back end pid (db)));
print(("Exec: ", pq exec(db, "SELECT version()"), newline));
INT finish status = pq finish(db);
print(("Finish: ", finish status, newline))
ELSE
print(("Failed: ", err, newline))
FI
END

14
src/postgres-test5.a68 Normal file
View File

@ -0,0 +1,14 @@
BEGIN
FILE db; STRING result;
STRING conninfo = "dbname=testdb user=gmgauthier password=thx1138gg host=localhost port=5432";
INT status = pq connect db(db, conninfo, result);
IF status = 0 THEN
INT qstatus = pq exec(db, "SELECT data FROM test");
IF qstatus = 0 THEN
print(("Rows: ", pq ntuples(db), newline));
IF pq get value(db, 1, 1) = 0 THEN print(("Data: ", result, newline)) FI
FI;
INT finish = pq finish(db);
print(("Finish: ", finish, newline))
ELSE print(("Failed: ", result, newline)) FI
END

21
src/simple-time.a68 Normal file
View File

@ -0,0 +1,21 @@
BEGIN
PROC print time = VOID:
BEGIN
[] INT lt = local time;
print(("time: ", lt[1], "-", lt[2], "-", lt[3], " ",
lt[4], ":", lt[5], ":", lt[6],
", dow: ", lt[7], ", dst: ", lt[8], newline))
END;
PROC delay = (REAL secs) VOID:
BEGIN
REAL strt = seconds;
WHILE seconds - strt < secs DO SKIP OD
END;
FOR i FROM 1 TO 5 DO
print(("run ", i, ": ", newline));
print time;
delay(1.0) # Wait 1 second between prints #
OD
END

7
src/times.a68 Normal file
View File

@ -0,0 +1,7 @@
BEGIN
[] INT lt = local time;
print(lt);
LONG REAL secs = local time; # Explicit LONG REAL #
print(("seconds: ", secs, newline))
END