Merge branch 'feature/time' into develop

This commit is contained in:
Simon Brooke 2019-02-07 15:32:15 +00:00
commit 93c40b7d27
5 changed files with 44 additions and 11 deletions

View file

@ -31,7 +31,7 @@
#include "peano.h"
#include "print.h"
#include "repl.h"
#include "time.h"
#include "psse_time.h"
// extern char *optarg; /* defined in unistd.h */

View file

@ -378,9 +378,8 @@ void collect_meta( struct cons_pointer stream, char *url ) {
meta =
add_meta_integer( meta, L"size",
( intmax_t ) statbuf.st_size );
/*
meta = add_meta_time( meta, L"modified", &statbuf.st_mtime );
*/
}
break;
case CFTYPE_CURL:

View file

@ -24,7 +24,7 @@
#include "map.h"
#include "stack.h"
#include "print.h"
#include "time.h"
#include "psse_time.h"
#include "vectorspace.h"
/**
@ -140,6 +140,27 @@ void print_vso( URL_FILE * output, struct cons_pointer pointer) {
}
}
/**
* stolen from https://stackoverflow.com/questions/11656241/how-to-print-uint128-t-number-using-gcc
*/
void print_128bit( URL_FILE * output, __int128_t n ) {
if ( n == 0 ) {
fwprintf( stderr, L"0" );
} else {
char str[40] = { 0 }; // log10(1 << 128) + '\0'
char *s = str + sizeof( str ) - 1; // start at the end
while ( n != 0 ) {
if ( s == str )
return; // never happens
*--s = "0123456789"[n % 10]; // save last digit
n /= 10; // drop it
}
url_fwprintf( output, L"%s", s );
}
}
/**
* Print the cons-space object indicated by `pointer` to the stream indicated
* by `output`.
@ -257,7 +278,11 @@ struct cons_pointer print( URL_FILE * output, struct cons_pointer pointer ) {
url_fputwc( L'>', output);
break;
case TIMETV:
url_fwprintf( output, L"<Time: " );
print_string( output, time_to_string( pointer));
url_fputws( L"; ", output);
print_128bit( output, pointer2cell(pointer).payload.time.value);
url_fputwc( L'>', output);
break;
case TRUETV:
url_fwprintf( output, L"t" );

View file

@ -1,5 +1,5 @@
/*
* time.h
* psse_time.c
*
* Bare bones of PSSE time. See issue #16.
*
@ -8,6 +8,7 @@
*/
#include <stdlib.h>
#include <string.h>
#include <time.h>
/*
* wide characters
@ -18,7 +19,7 @@
#include "conspage.h"
#include "consspaceobject.h"
#include "integer.h"
#include "time.h"
#include "psse_time.h"
#define _GNU_SOURCE
#define seconds_per_year 31557600L
@ -90,9 +91,17 @@ struct cons_pointer lisp_time( struct stack_frame *frame, struct cons_pointer fr
* This is temporary, for bootstrapping.
*/
struct cons_pointer time_to_string( struct cons_pointer pointer) {
struct cons_pointer result = NIL;
long int t = lisp_time_to_unix_time(pointer);
return c_string_to_lisp_string( t == 0 ?
L"Not yet implemented: cannot print times outside UNIX time\n" :
ctime(&t));
if ( t != 0) {
char * bytes = ctime(&t);
int l = strlen(bytes) + 1;
wchar_t buffer[ l];
mbstowcs( buffer, bytes, l);
result = c_string_to_lisp_string( buffer);
}
return result;
}

View file

@ -1,5 +1,5 @@
/*
* time.h
* psse_time.h
*
* Bare bones of PSSE time. See issue #16.
*