Skip to content

Instantly share code, notes, and snippets.

@tjmahr
Last active May 26, 2017 01:06
Show Gist options
  • Save tjmahr/7f03662c69f71f43764c to your computer and use it in GitHub Desktop.
Save tjmahr/7f03662c69f71f43764c to your computer and use it in GitHub Desktop.
xml example
library("magrittr")
library("stringr")
library("lubridate")
library("xml2")
get_date <- function(xml_blob) {
# Convert XML clock info into an R list
xml_list <- xml_blob %>%
str_replace_all("\\\\n", "") %>%
read_xml %>%
as_list
# Extract date, convert from UTC to Central Time, save as string
date <- xml_list[["StartTime"]][["DateUtc"]][[1]] %>%
parse_date_time(orders = "%y-%m-%dT") %>%
with_tz("America/Chicago") %>%
format("%Y-%m-%d")
date
}
get_date(
'<?xml version=\"1.0\"?>\\n<Clock xmlns:dt=\"urn:schemas-microsoft-com:datatypes\"><Description dt:dt=\"string\">E-Prime Primary Realtime Clock</Description><StartTime><Timestamp dt:dt=\"int\">0</Timestamp><DateUtc dt:dt=\"string\">2013-01-03T15:33:32Z</DateUtc></StartTime><FrequencyChanges><FrequencyChange><Frequency dt:dt=\"r8\">2992520000</Frequency><Timestamp dt:dt=\"r8\">3.43119378004024E+15</Timestamp><Current dt:dt=\"r8\">0</Current><DateUtc dt:dt=\"string\">2013-01-03T15:33:32Z</DateUtc></FrequencyChange></FrequencyChanges></Clock>\\n'
)
# [1] "2013-01-03"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment