mktime [05/Jul/2008:00:27:35 +0000]

This is a discussion on mktime [05/Jul/2008:00:27:35 +0000] within the awk forums in Programming Languages category; We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC into a timestamp. How can we deal with this format [05/Jul/2008:00:27:35 +0000] ? If using gensub(/\[(..)\/(...)\/(....) ..) ..) ..)/, "\\3 \\2 \\1 \ \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35". Is there a way converting "Jul" to "07" so that we can blah("\\2") -> 07 ?...

Go Back   Application Development Forum > Programming Languages > awk

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-04-2008, 08:37 PM
dnlchen@gmail.com
Guest
 
Default mktime [05/Jul/2008:00:27:35 +0000]

We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC
into a timestamp.

How can we deal with this format [05/Jul/2008:00:27:35 +0000] ?

If using gensub(/\[(..)\/(...)\/(....)..)..)..)/, "\\3 \\2 \\1 \
\4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35".

Is there a way converting "Jul" to "07" so that we can blah("\\2") ->
07 ?
Reply With Quote
  #2  
Old 07-04-2008, 10:07 PM
Ed Morton
Guest
 
Default Re: mktime [05/Jul/2008:00:27:35 +0000]



On 7/4/2008 7:37 PM, dnlchen@gmail.com wrote:
> We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC
> into a timestamp.
>
> How can we deal with this format [05/Jul/2008:00:27:35 +0000] ?
>
> If using gensub(/\[(..)\/(...)\/(....)..)..)..)/, "\\3 \\2 \\1 \
> \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35".
>
> Is there a way converting "Jul" to "07" so that we can blah("\\2") ->
> 07 ?


Play with this:

function cvttime(t, a) {
split(t,a,"[/:]")
match("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])
a[2] = sprintf("%02d",(RSTART+2)/3)
return( mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]) )
}
BEGIN{
t1="01/Dec/2005:00:04:42"
t2="01/Dec/2005:17:14:12"
print cvttime(t2) - cvttime(t1)
}

Ed.

Reply With Quote
  #3  
Old 07-04-2008, 10:42 PM
Steffen Schuler
Guest
 
Default Re: mktime [05/Jul/2008:00:27:35 +0000]

dnlchen@gmail.com wrote:
> We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC
> into a timestamp.
>
> How can we deal with this format [05/Jul/2008:00:27:35 +0000] ?
>
> If using gensub(/\[(..)\/(...)\/(....)..)..)..)/, "\\3 \\2 \\1 \
> \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35".
>
> Is there a way converting "Jul" to "07" so that we can blah("\\2") ->
> 07 ?


$ cat date2mktime.awk
#!/bin/sh

TZ=UTC0 gawk '
BEGIN {
tm = "[05/Jul/2008:00:27:35 +0000]"
printf "%s ### %s\n", tm, strftime("%c", to_time(tm))
}

function to_month_idx(month_abbr, months)
{
months = "JanFebMarAprMayJunJulAugSepOctNovDec"
match(months, month_abbr)
return (RSTART + 2) / 3
}

function to_time(tm, dyn_re, month_abbr, format)
{
dyn_re = "^\\[(..)/(...)/(....)..)..)..) \\+(....)\\]$"
month_abbr = gensub(dyn_re, "\\2", 1, tm)
format = gensub(dyn_re, "\\3 %02d \\1 \\4 \\5 \\6", 1, tm)
return mktime(sprintf(format, to_month_idx(month_abbr)))
}'
$ chmod u+x date2mktime.awk
$ ./date2mktime.awk
[05/Jul/2008:00:27:35 +0000] ### Sat 05 Jul 2008 12:27:35 AM UTC
$


--
Steffen
Reply With Quote
  #4  
Old 07-05-2008, 01:08 AM
dnlchen@gmail.com
Guest
 
Default Re: mktime [05/Jul/2008:00:27:35 +0000]

On Jul 4, 7:07*pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 7/4/2008 7:37 PM, dnlc...@gmail.com wrote:
>
> > We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC
> > into a timestamp.

>
> > How can we deal with this format [05/Jul/2008:00:27:35 +0000] ?

>
> > If using gensub(/\[(..)\/(...)\/(....)..)..)..)/, "\\3 \\2 \\1 \
> > \4 \\5 \\6",""), the output will be "2008 Jul 05 00 27 35".

>
> > Is there a way converting "Jul" to "07" so that we can blah("\\2") ->
> > 07 ?

>
> Play with this:
>
> function cvttime(t, * * a) {
> * * * * split(t,a,"[/:]")
> * * * * match("JanFebMarAprMayJunJulAugSepOctNovDec",a[2])
> * * * * a[2] = sprintf("%02d",(RSTART+2)/3)
> * * * * return( mktime(a[3]" "a[2]" "a[1]" "a[4]" "a[5]" "a[6]) )}
>
> BEGIN{
> t1="01/Dec/2005:00:04:42"
> t2="01/Dec/2005:17:14:12"
> print cvttime(t2) - cvttime(t1)
>
> }
>
> * * * * Ed.


This is awesome! When I use script languages to parse the logs, I find
awk has both efficiency and flexibility. A perl module can finish this
task, however it is not as efficient as awk.

Now gawk has some new stuff such as array, built-in functions. Why
don't we think about bringing in the modules? If so I think awk will
get more popular.
Reply With Quote
  #5  
Old 07-05-2008, 11:10 AM
Ted Davis
Guest
 
Default Re: mktime [05/Jul/2008:00:27:35 +0000]

On Fri, 04 Jul 2008 17:37:29 -0700, dnlchen wrote:

> We can use mktime("YYYY MM DD HH MM SS [DST]") to convert a DATESPEC into
> a timestamp.
>
> How can we deal with this format [05/Jul/2008:00:27:35 +0000] ?
>
> If using gensub(/\[(..)\/(...)\/(....)..)..)..)/, "\\3 \\2 \\1 \ \4
> \\5 \\6",""), the output will be "2008 Jul 05 00 27 35".
>
> Is there a way converting "Jul" to "07" so that we can blah("\\2") -> 07 ?


I use a variation on the approach already given:

MonthAbrvList = " JanFebMarAprMayJunJulAugSepOctNovDec"
Month = sprintf( "%.2i",index( MonthAbrvList, Abrv )/3 )

Note that the two leading spaces on the list remove the need for +2 and
that the number has leading zeros if single digit. I generally place the
MonthAbrvList assignment in a BEGIN block and use the actual conversion
where needed rather than making the whole thing a function. Also note
that the two spaces can be anything that doesn't cause a false match, and
it is probably better if they are something that clearly shows how many
there are.

--

T.E.D. (tdavis@mst.edu) MST (Missouri University of Science and Technology)
used to be UMR (University of Missouri - Rolla).


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:54 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.