I have been trying a few things with Racket these past few days. I am new to Racket, and am loving it. Thank you to all who have made it such a success.
I had a question about converting sql-timestamp
to seconds from epoch. I could not find that in the docs. Can someone please point me in the right direction? When I query data from Postgres, I have a column timestamptz
that gets converted to sql-timestamp
. I believe this is not a jsexpr?
. I would like to convert it to seconds from epoch before I send it to the client. Thank you very much.
I convert sql-timestamp
s to moment
s (from gregor-lib
), then use its ->posix
function to convert the moments to unix timestamps. An example: https://github.com/Bogdanp/koyo/blob/176db1253caeb92c50cef9a8f1adcc53028e4bb4/koyo-lib/koyo/database.rkt#L185-L193
If you don’t want to use gregor, you can convert the sql-timestamp
to the built-in date
data type and then use date->seconds
Thank you very much, Bogdan. I will try the gregor
route. Does koyo/database do that for you automatically? If so, I will look into using that.
Is there a function to convert sql-timestamp
to built in date
data type? To manually construct the built-in date
date type seems difficult since it requires week-day
, year-day
etc.
Another route I just found, which is similar to your usage of gregor-lib
is to use the find-seconds
function in the time
library.
Thank you for your help.
Yes, koyo/database
does provide that functionality, although I notice I forgot to document it. It’s not going to go away, though, so it’s safe to use if you decide to.
Thank you. Can you please let me know how I find the function in koyo/database that does it (since it is not documented, I am not sure how to look for it). Do I have to look at the source of koyo/database? Thank you.
in general, the names of fields are not there at runtime
Oh, sorry, it’s the one I linked to, sql-timestamp->moment
, which you can require from koyo/database
. Provided https://github.com/Bogdanp/koyo/blob/176db1253caeb92c50cef9a8f1adcc53028e4bb4/koyo-lib/koyo/database.rkt#L137\|here. That function lets you turn a sql timestamp into a moment value and gregor provides ->posix
that can convert a moment into a unix timestamp.
Awesome! Thank you so much. And, sorry for not seeing the earlier post more carefully.
Trying to write a function which reads all lines of a file to a Vector in typed/racket. What do I miss?
(or: is there a more idiomatic way to read a file line-by-line to a vector?)
Two things:
- ~If you want
lines
to be a vector of strings, you should not have parentheses aroundlines
. Otherwise, it would definelines
to be a function .~ I didn’t read carefully. My bad for
forms usually need an additional annotation to make it work.
Here’s a working code:
#lang typed/racket
(define (lines) : (Vectorof String)
(with-input-from-file "input3"
(λ ()
(for/vector : (Vectorof String) ([line (in-lines)])
line))))
Found another way ;)