[英]Date issue while reading/writing csv files
將在IST時區中具有datetime列的.csv
文件加載到RStudio時,會將其轉換為UTC語言環境(-5:30)。 因此,我的子集比較檢查沒有給出確切的結果。 例如。
原始字符串,如csv文件中所示:
> coilid$StartTime[1:5]
[1] "06/01/2017 00:29:10" "06/01/2017 01:19:19" "06/01/2017 02:09:31" "06/01/2017 03:05:08"
[5] "06/01/2017 03:48:51"
使用lubridate
加載:
lub_date = mdy_hms(coilid$StartTime[1:5])
> lub_date
[1] "2017-06-01 00:29:10 UTC" "2017-06-01 01:19:19 UTC" "2017-06-01 02:09:31 UTC" "2017-06-01 03:05:08 UTC"
[5] "2017-06-01 03:48:51 UTC"
字符串應被視為IST日期。 默認為UTC
> lub_date[1] > '2017-06-01 05:58:00'
[1] TRUE
> lub_date[1] > '2017-06-01 05:59:59'
[1] FALSE
因此增加了tz
lub_date_ist = mdy_hms(coilid$StartTime[1:5], tz = 'Asia/Calcutta')
> lub_date_ist
[1] "2017-06-01 00:29:10 IST" "2017-06-01 01:19:19 IST" "2017-06-01 02:09:31 IST" "2017-06-01 03:05:08 IST"
[5] "2017-06-01 03:48:51 IST"
> lub_date_ist[1] > '2017-06-01 00:29:59'
[1] FALSE
> lub_date_ist[1] > '2017-06-01 00:29:00'
[1] TRUE
現在的問題是,當我將它另存為data.table到csv文件中並再次讀取時,數據會損壞。
fwrite(data.table(lub_date_ist), file = paste0(path_loc, 'xxx.csv'))
fread(file = paste0(path_loc, 'xxx.csv'))
V1 V2 V3
1: 2017-05-31T18 59 10Z
2: 2017-05-31T19 49 19Z
3: 2017-05-31T20 39 31Z
4: 2017-05-31T21 35 08Z
5: 2017-05-31T22 18 51Z
想法是以UTC
格式在file/database
中write/maintain
日期/時間。 以locale
格式存儲日期/時間既困難又容易出錯。 特別是在應用DST
時區。
實現它的步驟非常簡單。 即將寫入文件之前,將日期/時間轉換為UTC
格式。 同樣,以UTC
格式讀取它,然后將其轉換為語言環境時區。 with_tz
的使用對於將日期/時間轉換為另一個時區非常重要。
請允許我用OP中的數據進行演示。
library(lubridate)
coild <- read.table(text = 'Sl Time
1 "06/01/2017 00:29:10"
2 "06/01/2017 01:19:19"
3 "06/01/2017 02:09:31"
4 "06/01/2017 03:05:08"
5 "06/01/2017 03:48:51"', header = T, stringsAsFactors = F)
# The time is in IST format
coild$Time <- mdy_hms(coild$Time, tz="Asia/Calcutta")
[1] "2017-06-01 00:29:10 IST" "2017-06-01 01:19:19 IST" "2017-06-01 02:09:31 IST"
[4] "2017-06-01 03:05:08 IST" "2017-06-01 03:48:51 IST"
# Convert it to "UTC" just before writing to file
fwrite(data.table(with_tz(coild$Time, tzone = "UTC")), file = 'xxx.csv')
# Read and convert it date/time in UTC format
DT <- fread(file = 'xxx.csv', sep = " ", colClasses=c("POSIXt"))
# Date is in UTC format
readTime <- parse_date_time(DT$V1, "%Y-%m-%d %H:%M:%S%z")
# Change date/time in IST
with_tz(readTime, tzone = "Asia/Calcutta")
[1] "2017-06-01 00:29:10 IST" "2017-06-01 01:19:19 IST" "2017-06-01 02:09:31 IST"
[4] "2017-06-01 03:05:08 IST" "2017-06-01 03:48:51 IST"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.