Ben
Ben

Reputation: 1522

Convert nasty time format from tera term

I got this time format

Fri Aug 28 11:34:57,915 2020

from a tera term data logging. I'm trying to find a possibility to convert it to a more handy time format. So far, I couldn't find a proper package (e.g. chron, lubridate and POSIXct) or something similar. But so far, I never had such an issue by converting time formats so I hope others already know something. Otherwise I have to re-arrange it somehow manually.

Upvotes: 2

Views: 928

Answers (2)

pieterbons
pieterbons

Reputation: 1724

If the format will always be the same, you can try to extract the necessary date elements using a regular expression (this is necessary to access the year information at the end) and then convert to the proper date format:

test <- "Fri Aug 28 11:34:57,915 2020"

test2 <- gsub(x = test, 
     pattern = "([A-Za-z]+) ([A-Za-z]+) ([0-9]+) ([0-9]+):([0-9]+):([0-9]+),[0-9]+ ([0-9]+)",
     replacement = "\\2 \\3 \\7 \\4:\\5:\\6")

date <- as.POSIXct(test2, format = "%b %d %Y %H:%M:%S")
date

Upvotes: 1

Allan Cameron
Allan Cameron

Reputation: 174448

You could try:

strptime("Fri Aug 28 11:34:57,915 2020", format = "%a %b %d %H:%M:%S")
#> [1] "2020-08-28 11:34:57 BST"

Or, if you want to ensure that you capture the year correctly, you will first need to get rid of the comma with the trailing milliseconds:

f <- function(x) strptime(gsub(",\\d+", "", x), format = "%a %b %d %H:%M:%S %Y")

f(c("Fri Aug 28 11:34:57,915 2020", "Mon Feb 05 12:21:05,321 2018"))
#> [1] "2020-08-28 11:34:57 BST" "2018-02-05 12:21:05 GMT"

Upvotes: 3

Related Questions