Julian way to do strptime?

I’m converting an R script that uses strptime to extact datetime data from strings. Is there a more Julian way of doing this than calling Base.Libc.strptime?

I’m not very familiar with either string or date handling in Julia.

there is, what format specifiers occur in those uses of striptime? (show me a few examples, it is easier to explain how to do it in Julia with a few specific uses).


Base.Libc.strptime.("SMAP_L4_SM_gph_%Y%m%dT%H%M%S", filenames)

Are all of the strings are prefixed with “SMAP_L4_SM_gph_” ?

(would 2018-May-17 at 22:18:05 be given as “SMAP_L4_SM_gph_20180517T221805”)

using Dates

str = "SMAP_L4_SM_gph_20180517T221805"

# Define the format, `S` and `M` need to be escaped because they
# are format identifiers
df = dateformat"\S\MAP_L4_\S\M_gph_yyyymmddTHHMMSS"

DateTime(str, df)
# 2018-05-17T22:18:05

first, lets convert a string that only contains the date+time, given in that same pattern (yyyymmddTHHMMSS) where the “T” serves to separate the date part from the time part.

using Dates

myformat = dateformat"yyyymmddTHHMMSS"

str = "20180517T221805"

mydatetime = DateTime(str, muformat)

with that this happens

julia> mydatetime = DateTime(str, myformat)

julia> Date(mydatetime), Time(mydatetime)
(2018-05-17, 22:18:05)

now lets suppose that all your date+time strings are prefixed by some (nonempty) character sequence that ends with ‘_’ (the [last] underscore in the string comes before the first digit of the year). This way you can have a slightly more general converter.

using Dates

myformat = dateformat"yyyymmddTHHMMSS"

strip_prefixed_chars(filenamestr) = split(filenamestr, '_')[end]

with that we can do this

julia> str = "SMAP_L4_SM_gph_20180517T221805"

julia> mydatetime =  DateTime(strip_prefixed_chars(str), myformat)

Ok dateformat string macro is the thing! so obvious I missed it.


But what if the string has a day-of-year? I couldn’t find a format that allows parsing, e.g.

(many satellite products use variations of this to name their files)