I have a csv with entries
unique_id,creation_time
52,2021-08-09 18:55:43
53,2021-08-09 19:55:43
54,2021-08-09 20:55:43
After doing a fread(fread('mycsv.csv')
), I get a DT ->
unique_id creation_time
1 52 2021-08-09 18:55:43
2 53 2021-08-09 19:55:43
3 54 2021-08-09 20:55:43
When I do a str on the DT obtained, I see -
❯ str(dt)
Classes ‘data.table’ and 'data.frame': <> obs. of 2 variables:
$ unique_id : int 1 2 3
$ creation_time : POSIXct, format: "2021-08-09 18:55:43" "2021-08-09 19:55:43" "2021-08-09 20:55:43"
My first question is this -
How did fread know that my creation_time
col is a timestamp. Why did it automatically convert it to a POSIXct ?
❯ dt[unique_id == 52][1]$creation_time
[1] "2021-08-09 18:55:43 UTC"
By default, this is converted to UTC. When I try to convert the tz to 'Asia/Kolkata' (this is taken from Sys.timezone()
), nothing happens.
❯ Sys.timezone()
[1] "Asia/Kolkata"
❯ as.POSIXct(dt[unique_id == 52][1]$creation_time, tz='Asia/Kolkata')
[1] "2021-08-09 18:55:43 UTC"
It remains in UTC. However if I convert the timestamp back to a character type and then do a POSIXct with tz, it works
❯ as.POSIXct(as.character(dt[unique_id == 52][1]$creation_time), tz='Asia/Kolkata')
[1] "2021-08-09 18:55:43 IST"
Does POSIXct timezone conversion not happen on a POSIXct type ? Should it always be done on a character type ?
How does one change a timezone of an existing POSIXct variable ?