Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling milliseconds in POSIXct datetimes #53

Open
statquant opened this issue Sep 4, 2024 · 0 comments
Open

Handling milliseconds in POSIXct datetimes #53

statquant opened this issue Sep 4, 2024 · 0 comments

Comments

@statquant
Copy link

Hello, thank you for this package I'm amazed at how fast it is.
I was trying to use it as a drop in replacement for jsonlite::toJSON to write data to be passed to a graphic library.
I noticed that I cannot get the milliseconds to "print" using yyjsonr as depicted in the following:

library(jsonlite)
library(yyjsonr)
library(data.table)

set.seed(1L)
options(digits.secs = 3)
N <- 10
DF <- data.table(
  date = Sys.Date() + seq_len(N),
  time_msec = as.POSIXct(Sys.Date()) + seq_len(N)/1e3,
  values = rnorm(N)
)
DF
#           date               time_msec     values
#         <Date>                  <POSc>      <num>
#  1: 2024-09-05 2024-09-04 00:00:00.000 -0.6264538
#  2: 2024-09-06 2024-09-04 00:00:00.002  0.1836433
#  3: 2024-09-07 2024-09-04 00:00:00.003 -0.8356286
#  4: 2024-09-08 2024-09-04 00:00:00.003  1.5952808
#  5: 2024-09-09 2024-09-04 00:00:00.005  0.3295078
#  6: 2024-09-10 2024-09-04 00:00:00.006 -0.8204684
#  7: 2024-09-11 2024-09-04 00:00:00.006  0.4874291
#  8: 2024-09-12 2024-09-04 00:00:00.007  0.7383247
#  9: 2024-09-13 2024-09-04 00:00:00.009  0.5757814
# 10: 2024-09-14 2024-09-04 00:00:00.009 -0.3053884

# Default jsonlite::toJSON 
jsonlite::toJSON(DF, digits = 4L)
# [{"date":"2024-09-05","time_msec":"2024-09-04 00:00:00.000","values":-0.6265},{"date":"2024-09-06","time_msec":"2024-09-04 00:00:00.002","values":0.1836},{"date":"2024-09-07","time_msec":"2024-09-04 00:00:00.003","values":-0.8356},{"date":"2024-09-08","time_msec":"2024-09-04 00:00:00.003","values":1.5953},{"date":"2024-09-09","time_msec":"2024-09-04 00:00:00.005","values":0.3295},{"date":"2024-09-10","time_msec":"2024-09-04 00:00:00.006","values":-0.8205},{"date":"2024-09-11","time_msec":"2024-09-04 00:00:00.006","values":0.4874},{"date":"2024-09-12","time_msec":"2024-09-04 00:00:00.007","values":0.7383},{"date":"2024-09-13","time_msec":"2024-09-04 00:00:00.009","values":0.5758},{"date":"2024-09-14","time_msec":"2024-09-04 00:00:00.009","values":-0.3054}] 

yyjsonr::write_json_str(DF, opts = list(digits = 4L))
# [1] "[{\"date\":\"2024-09-05\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":-0.6265},{\"date\":\"2024-09-06\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":0.1836},{\"date\":\"2024-09-07\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":-0.8356},{\"date\":\"2024-09-08\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":1.5953},{\"date\":\"2024-09-09\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":0.3295},{\"date\":\"2024-09-10\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":-0.8205},{\"date\":\"2024-09-11\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":0.4874},{\"date\":\"2024-09-12\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":0.7383},{\"date\":\"2024-09-13\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":0.5758},{\"date\":\"2024-09-14\",\"time_msec\":\"2024-09-04 00:00:00\",\"values\":-0.3054}]"

Sorry if that's obvious, is there something I can do to get the millis to show ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant