2
library("anytime")

This works well:

anytime("08/24/2014 01:28:00")
anytime("2014/08/24 01:28:00")

[1] "2014-08-24 00:28:00 NZST"

This doesn't:

anytime("24/08/2014 01:28:00")
anytime("2014/24/08 01:28:00")

[1] NA

What is the reason for this and what would be the options (using anytime package ONLY)?

Edgar Santos
  • 3,426
  • 2
  • 17
  • 29

1 Answers1

3

We can add the formats with addFormats in anytime if that formats are not in the default list of formats by using

getFormats()
#[1] "%Y-%m-%d %H:%M:%S%f"    "%Y/%m/%d %H:%M:%S%f"    "%Y%m%d %H%M%S%f"       
#[4] "%Y%m%d %H:%M:%S%f"      "%m/%d/%Y %H:%M:%S%f"    "%m-%d-%Y %H:%M:%S%f"   
#[7] "%Y-%b-%d %H:%M:%S%f"    "%Y/%b/%d %H:%M:%S%f"    "%Y%b%d %H%M%S%F"       
#[10] "%Y%b%d %H:%M:%S%F"      "%b/%d/%Y %H:%M:%S%f"    "%b-%d-%Y %H:%M:%S%f"   
#[13] "%d.%b.%Y %H:%M:%S%f"    "%Y-%B-%d %H:%M:%S%f"    "%Y/%B/%d %H:%M:%S%f"   
#[16] "%Y%B%d %H%M%S%f"        "%Y%B%d %H:%M:%S%f"      "%B/%d/%Y %H:%M:%S%f"   
#[19] "%B-%d-%Y %H:%M:%S%f"    "%d.%B.%Y %H:%M:%S%f"    "%a %b %d %H:%M:%S%F %Y"
#[22] "%Y-%m-%d"               "%Y%m%d"                 "%m/%d/%Y"              
#[25] "%m-%d-%Y"               "%Y-%b-%d"               "%Y%b%d"                
#[28] "%b/%d/%Y"               "%b-%d-%Y"               "%Y-%B-%d"              
#[31] "%Y%B%d"                 "%B/%d/%Y"               "%B-%d-%Y"   

If we check the format that gives NA, it is not in the getFormats list

c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S") %in% getFormats()
#[1] FALSE FALSE

So, we can add the formats with addFormats and apply the anytime

anytime::addFormats(c("%d/%m/%Y %H:%M:%S", "%Y/%d/%m %H:%M:%S")) 
anytime("24/08/2014 01:28:00")
#[1] "2014-08-24 01:28:00 IST"
anytime("2014/24/08 01:28:00")
#[1] "2014-08-24 01:28:00 IST"

Update

In the latest version of anytime, there are 41 formats in getFormats()

length(getFormats())
#[1] 41

But, the format specified in the OP's post is still not included and would have to follow the addFormats route

akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks. If multiple formats can be simultaneously defined, what happens when the argument is ambiguous? Say anytime("03/01/2014 01:28:00") – Edgar Santos Apr 02 '17 at 08:06
  • 1
    @ed_sans this is already in the formats i.e. `anytime("03/01/2014 01:28:00") #[1] "2014-03-01 01:28:00 IST"` I think it is very difficult for the machine to process if it is ambiguous without specifying the format. You will have the same problem when you are using `parse_date_time` from `lubridate`. Only option is to make sure that you as an user knows exactly whether it is month followed by day or day followed by month to correctly insert the format. – akrun Apr 02 '17 at 08:08
  • What I mean is how the function decides what format to use when matching several e.g. "03/01/2014" = 1st of March or 3th of January? – Edgar Santos Apr 02 '17 at 08:11
  • @ed_sans I updated my earlier comment. Based on the list of `getFormats`, it is only having `%m` followed by %d` as default. So it will read as March 01 – akrun Apr 02 '17 at 08:14
  • @SymbolixAU Thanks, I have `anytime_0.0.2` – akrun Apr 02 '17 at 08:43