2

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.

Ben
  • 1,432
  • 4
  • 20
  • 43
  • 1
    I know this is an old post, and answered, but curious if you have access to the teraterm script to make edits there? If so, you can make the call to _[gettime time_str ](https://ttssh2.osdn.jp/manual/4/en/macro/command/gettime.html)_ with a custom format string argument to produce a time-stamp in the format you prefer. No post-processing necessary. – ryyker Jun 08 '22 at 14:59

2 Answers2

3

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"
Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 1
    Upvote but to get rid of the comma doesn't mean to get rid of the decimals, use format string `%OS`. (And `sub(",", ".", x)`). – Rui Barradas Sep 07 '20 at 09:40
  • Sure, there was also strptime. When I execute your code above, I receive NA. That's weird as it is just one line which I do not modify at all.. do I have to load something, or so? – Ben Sep 07 '20 at 09:40
  • 1
    @Ben Is your locale an English language locale? Mine isn't so I have to `Sys.setlocale("LC_TIME", "en_US.UTF-8")` and everything works. – Rui Barradas Sep 07 '20 at 09:43
  • 1
    @Ben the captures in `strptime` are locale dependent, so if your locale is set to somewhere that has different names for weekdays and months to the format you are parsing, you would need to change locale as per @RuiBarradas – Allan Cameron Sep 07 '20 at 09:43
  • Thanks a lot! I guess this detail would have driven me insane through the day.. nevertheless, I have to figure out how it works. When I change it as given above from Rui, I receive: "Warning message: In Sys.setlocale("LC_TIME", "en_US.UTF-8") : OS reports request to set locale to "en_US.UTF-8" cannot be honored " – Ben Sep 07 '20 at 09:45
  • @RuiBarradas I think my regex finds any comma with trailing digits and removes both the comma and the trailing digits from the string? – Allan Cameron Sep 07 '20 at 09:45
  • 1
    Yes, that was my point. Don't remove the trailing digits, replace the comma with period and use format `"%OS"` to keep the decimals digits. – Rui Barradas Sep 07 '20 at 09:47
  • @Ben Are you on [Windows](https://stackoverflow.com/a/8147191/8245406)? – Rui Barradas Sep 07 '20 at 09:49
  • Unfortunately, yes :) I found https://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r but so far, it doesn't work. Have to dive deeper.. – Ben Sep 07 '20 at 09:50
  • great, found on https://stackoverflow.com/questions/17031002/get-weekdays-in-english-in-r the suggestion to use " Sys.setlocale("LC_TIME", "English") " which finally worked while all other didn't. Now the strptime command also works! Thank you all! – Ben Sep 07 '20 at 09:58
1

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
pieterbons
  • 1,604
  • 1
  • 11
  • 14