5

Gnu AWK provides the built in function

strftime()

which can convert a timestamp like 1359210984 into Sat 26. Jan 15:36:24 CET 2013. I couldn't find a function that would do this:

seconds = timefromdate("Sat 26. Jan 15:36:24 CET 2013", "%a %d. %b %H:%M:%S CET %Y")

or

seconds = timefromdate("2013-01-26 15:36:24", "%Y-%m-%d %H:%M:%S")

Whereas seconds then is 1359210984.

So, the date string should be convertable by a format pattern.

I'd like to do this in gawk only.

Edit 1:

I'd like to convert the date only in gawk for further processing of the stream.

Edit 2:

I've clarified my question. It was a bit sloppy in the "would do this" code example.

Community
  • 1
  • 1
try-catch-finally
  • 7,436
  • 6
  • 46
  • 67
  • 1
    I think your best chance is to massage the time string by regexps until it becomes acceptable for `mktime`. – Anton Kovalenko Jan 26 '13 at 14:46
  • @svnpenn: Nothing's wrong with `date -d`, but it doesn't help filtering a logfile's timestamp. ;) @Anton Kovalenko: I treat this as "there's no such built in function, but ...". I can live with your answer, if you like, you might add it as an answer. – try-catch-finally Jan 26 '13 at 16:59
  • Clarified my question with precise examples. (I admit, it was a bit sloppy :). – try-catch-finally Jan 26 '13 at 21:46

2 Answers2

5

The function you're looking for is called mktime(). You should use the gensub() function to manipulate the datespec into the format that can be read by mktime().


To format the second example that you give, consider:

BEGIN {
    t = "2013-01-26 15:36:24"
    f = "\\1 \\2 \\3 \\4 \\5 \\6"

    s = mktime(gensub(/(....)-(..)-(..) (..):(..):(..)/, f, "", t))

    print s
}

Results on my machine:

1359178584

To format the first example that you give, consider:

BEGIN {
    t = "Sat 26. Jan 15:36:24 CET 2013"

    gsub(/\.|:/, FS, t)
    split(t,a)

    Y = a[8]
    M = convert(a[3])
    D = a[2]

    h = a[4]
    m = a[5]
    s = a[6]

    x = mktime(sprintf("%d %d %d %d %d %d", Y, M, D, h, m, s))

    print x
}

function convert(month) {

    return(((index("JanFebMarAprMayJunJulAugSepOctNovDec", month) - 1) / 3) + 1)
}

Results on my machine:

1359178584

For more information, please consult the manual, specifically time functions and string functions. HTH.

Steve
  • 51,466
  • 13
  • 89
  • 103
  • 1
    Great answer. I like your `convert()` solution it's very elegant. Mine looked like `MAP["Jan"]="01"; MAP["Feb"]="02"; ... MAP[match[3]] ...` (`match` = `a` in your case) which worked fine but is a bit long. – try-catch-finally Jan 27 '13 at 09:50
0

You could use the python method strptime from datetime:


from datetime import datetime

date_string = "21 June, 2018"

print("date_string =", date_string)
print("type of date_string =", type(date_string))

date_object = datetime.strptime(date_string, "%d %B, %Y")

print("date_object =", date_object)
print("type of date_object =", type(date_object))

for more info please see here: https://www.programiz.com/python-programming/datetime/strptime

Yannick Pezeu
  • 530
  • 1
  • 7
  • 12