Given a data.table with date-time strings, this function converts those dates-times to type POSIXct with the appropriate time zone. Assumption is that dates are of the form "2016-07-25T22:15:19Z" where T is just a separator and the last letter is a military timezone.
This is a side-effect-free function: it returns a new data.table and the input data.table is unmodified.
parse_date_time(input_df, date_cols, assume_tz = "UTC")
input_df | a data.table with one or more date-time columns you want to convert |
---|---|
date_cols | Character vector of column names to convert. Columns should have string dates of the form "2016-07-25T22:15:19Z". |
assume_tz | Timezone to convert to if parsing fails. Default is UTC |
https://www.timeanddate.com/time/zones/military
https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
# Sample es_search(), chomp_hits(), or chomp_aggs() output: someDT <- data.table::data.table(id = 1:5 , company = c("Apple", "Apple", "Banana", "Banana", "Cucumber") , timestamp = c("2015-03-14T09:26:53B", "2015-03-14T09:26:54B" , "2031-06-28T08:53:07Z", "2031-06-28T08:53:08Z" , "2000-01-01")) # Note that the date field is character right now str(someDT)#> Classes ‘data.table’ and 'data.frame': 5 obs. of 3 variables: #> $ id : int 1 2 3 4 5 #> $ company : chr "Apple" "Apple" "Banana" "Banana" ... #> $ timestamp: chr "2015-03-14T09:26:53B" "2015-03-14T09:26:54B" "2031-06-28T08:53:07Z" "2031-06-28T08:53:08Z" ... #> - attr(*, ".internal.selfref")=<externalptr># Let's fix that! someDT <- parse_date_time(input_df = someDT , date_cols = "timestamp" , assume_tz = "UTC") str(someDT)#> Classes ‘data.table’ and 'data.frame': 5 obs. of 3 variables: #> $ id : int 1 2 3 4 5 #> $ company : chr "Apple" "Apple" "Banana" "Banana" ... #> $ timestamp: POSIXct, format: "2015-03-14 07:26:53" "2015-03-14 07:26:54" ... #> - attr(*, ".internal.selfref")=<externalptr>