Post Scarcity
A prototype for a post scarcity programming environment
Loading...
Searching...
No Matches
psse_time.c
Go to the documentation of this file.
1/*
2 * psse_time.c
3 *
4 * Bare bones of PSSE time. See issue #16.
5 *
6 * (c) 2019 Simon Brooke <simon@journeyman.cc>
7 * Licensed under GPL version 2.0, or, at your option, any later version.
8 */
9
10#include <stdlib.h>
11#include <string.h>
12#include <time.h>
13/*
14 * wide characters
15 */
16#include <wchar.h>
17#include <wctype.h>
18
19#include "memory/conspage.h"
21#include "arith/integer.h"
22#include "time/psse_time.h"
23#define _GNU_SOURCE
24
25#define seconds_per_year 31557600L
26
27/**
28 * PSSE Lisp epoch is 14 Bn years, or 441,806,400,000,000,000 seconds, before
29 * the UNIX epoch; the value in microseconds will break the C reader.
30 */
31unsigned __int128 epoch_offset =
32 ( ( __int128 ) ( seconds_per_year * 1000000000L ) *
33 ( __int128 ) ( 14L * 1000000000L ) );
34
35/**
36 * Return the UNIX time value which represents this time, if it falls within
37 * the period representable in UNIX time, or zero otherwise.
38 */
40 long int result = 0;
41
42 if ( timep( t ) ) {
43 unsigned __int128 value = pointer2cell( t ).payload.time.value;
44
45 if ( value > epoch_offset ) { // \todo && value < UNIX time rollover
46 result = ( ( value - epoch_offset ) / 1000000000 );
47 }
48 }
49
50 return result;
51}
52
53unsigned __int128 unix_time_to_lisp_time( time_t t ) {
54 unsigned __int128 result = epoch_offset + ( t * 1000000000 );
55
56 return result;
57}
58
59struct cons_pointer make_time( struct cons_pointer integer_or_nil ) {
60 struct cons_pointer pointer = allocate_cell( TIMETV );
61 struct cons_space_object *cell = &pointer2cell( pointer );
62
63 if ( integerp( integer_or_nil ) ) {
64 cell->payload.time.value =
65 pointer2cell( integer_or_nil ).payload.integer.value;
66 } else {
67 cell->payload.time.value = unix_time_to_lisp_time( time( NULL ) );
68 }
69
70 return pointer;
71}
72
73/**
74 * Function; return a time representation of the first argument in the frame;
75 * further arguments are ignored.
76 *
77 * * (time integer_or_nil)
78 *
79 * @param frame my stack_frame.
80 * @param frame_pointer a pointer to my stack_frame.
81 * @param env my environment.
82 * @return a lisp time; if `integer_or_nil` is an integer, return a time which
83 * is that number of microseconds after the notional big bang; else the current
84 * time.
85 */
86struct cons_pointer lisp_time( struct stack_frame *frame,
87 struct cons_pointer frame_pointer,
88 struct cons_pointer env ) {
89 return make_time( frame->arg[0] );
90}
91
92/**
93 * This is temporary, for bootstrapping.
94 */
95struct cons_pointer time_to_string( struct cons_pointer pointer ) {
96 struct cons_pointer result = NIL;
97 long int t = lisp_time_to_unix_time( pointer );
98
99 if ( t != 0 ) {
100 char *bytes = ctime( &t );
101 int l = strlen( bytes ) + 1;
102 wchar_t buffer[l];
103
104 mbstowcs( buffer, bytes, l );
105 result = c_string_to_lisp_string( buffer );
106 }
107
108 return result;
109}
struct cons_pointer allocate_cell(uint32_t tag)
Allocates a cell with the specified tag.
Definition conspage.c:222
union cons_space_object::@3 payload
#define NIL
a cons pointer which points to the special NIL cell
#define timep(conspoint)
true if conspoint points to a time cell, else false.
#define TIMETV
The string TIME, considered as an unsigned int.
struct cons_pointer c_string_to_lisp_string(wchar_t *string)
Return a lisp string representation of this wide character string.
#define pointer2cell(pointer)
given a cons_pointer as argument, return the cell.
#define integerp(conspoint)
true if conspoint points to an integer cell, else false
An indirect pointer to a cons cell.
an object in cons space.
A stack frame.
unsigned __int128 unix_time_to_lisp_time(time_t t)
Definition psse_time.c:53
struct cons_pointer time_to_string(struct cons_pointer pointer)
This is temporary, for bootstrapping.
Definition psse_time.c:95
long int lisp_time_to_unix_time(struct cons_pointer t)
Return the UNIX time value which represents this time, if it falls within the period representable in...
Definition psse_time.c:39
struct cons_pointer lisp_time(struct stack_frame *frame, struct cons_pointer frame_pointer, struct cons_pointer env)
Function; return a time representation of the first argument in the frame; further arguments are igno...
Definition psse_time.c:86
struct cons_pointer make_time(struct cons_pointer integer_or_nil)
Definition psse_time.c:59
unsigned __int128 epoch_offset
PSSE Lisp epoch is 14 Bn years, or 441,806,400,000,000,000 seconds, before the UNIX epoch; the value ...
Definition psse_time.c:31
#define seconds_per_year
Definition psse_time.c:25