curiouslearn
2021-1-20 13:15:32

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.


curiouslearn
2021-1-20 13:17:26

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.


popa.bogdanp
2021-1-20 13:40:29

I convert sql-timestamps to moments (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


popa.bogdanp
2021-1-20 13:43:15

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


curiouslearn
2021-1-20 13:47:37

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.


curiouslearn
2021-1-20 13:49:25

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.


curiouslearn
2021-1-20 13:50:27

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.


curiouslearn
2021-1-20 13:50:33

Thank you for your help.


popa.bogdanp
2021-1-20 14:17:23

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.


curiouslearn
2021-1-20 14:26:01

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.


samth
2021-1-20 14:45:20

in general, the names of fields are not there at runtime


popa.bogdanp
2021-1-20 16:04:11

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.


curiouslearn
2021-1-20 18:18:01

Awesome! Thank you so much. And, sorry for not seeing the earlier post more carefully.


pihentagy
2021-1-20 21:21:48

Trying to write a function which reads all lines of a file to a Vector in typed/racket. What do I miss?


pihentagy
2021-1-20 21:23:49

pihentagy
2021-1-20 21:34:08

(or: is there a more idiomatic way to read a file line-by-line to a vector?)


sorawee
2021-1-20 21:48:29

Two things:

  1. ~If you want lines to be a vector of strings, you should not have parentheses around lines. Otherwise, it would define lines to be a function .~ I didn’t read carefully. My bad
  2. 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))))


pihentagy
2021-1-20 22:17:48

Found another way ;)