读/写csv文件时的日期问题

时间:2018-02-10 08:55:18

标签: r datetime

将在IST时区中具有datetime列的lubridate文件加载到RStudio时,会将其转换为UTC语言环境(-5:30)。因此,我的子集比较检查没有给出确切的结果。例如

csv文件中的原始字符串:

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"

使用> lub_date[1] > '2017-06-01 05:58:00' [1] TRUE > lub_date[1] > '2017-06-01 05:59:59' [1] FALSE

加载
tz

字符串应该被读作IST日期。默认情况下为UTC

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

因此添加了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

{{1}}

现在的问题是,当我将此数据保存为csv文件并再次读取时,数据会被损坏。

{{1}}

1 个答案:

答案 0 :(得分:0)

我的想法是以write/maintain file/database格式UTC日期/时间。以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"