How to convert dd/mm/yy to unix epoch time?

How to convert dd/mm/yy to unix epoch time?

Post by Pete » Thu, 09 Mar 2006 23:41:34



Hi,
I'm looking for a way to convert a given date in the format dd/mm/yy to
unix epoch time.
I've searched the news groups and found that nawk and awk can be used
to convert the current time, but I have been unable to find a way to
convert a specified date. Numerous posts say to use perl but I've never
used perl - I'm in the process of reading O'reilly's book on perl but
need to know a way of doing this soon.

Thanks for any help

Pete

 
 
 

How to convert dd/mm/yy to unix epoch time?

Post by Ed Morto » Fri, 10 Mar 2006 00:26:23



> Hi,
> I'm looking for a way to convert a given date in the format dd/mm/yy to
> unix epoch time.
> I've searched the news groups and found that nawk and awk can be used
> to convert the current time, but I have been unable to find a way to
> convert a specified date. Numerous posts say to use perl but I've never
> used perl - I'm in the process of reading O'reilly's book on perl but
> need to know a way of doing this soon.

> Thanks for any help

> Pete

For midnight March 14th 2005 (zero am March 15th 2005):

$ gawk 'BEGIN{print mktime("2005 3 15 0 0 0")}'
1110866400

$ gawk -v date="15/03/05" 'BEGIN{split(date,d,"/");print mktime("20"
d[3]" "d[2]" "d[1]" 0 0 0")}'
1110866400

Note the assumption that all dates start this century (putting "20" in
fromt of d[3]). You can add whatever logic applies to your application
to deal with that if necessary.

See "mktime()" in the GNU awk man page.

        Ed.

 
 
 

How to convert dd/mm/yy to unix epoch time?

Post by Stephane Chazela » Fri, 10 Mar 2006 00:35:58



Quote:> Hi,
> I'm looking for a way to convert a given date in the format dd/mm/yy to
> unix epoch time.
> I've searched the news groups and found that nawk and awk can be used
> to convert the current time, but I have been unable to find a way to
> convert a specified date. Numerous posts say to use perl but I've never
> used perl - I'm in the process of reading O'reilly's book on perl but
> need to know a way of doing this soon.

[...]

Here is a POSIX shell implementation of timegm (see
http://stchaz.free.fr/wide_strftime.sh). It expects a UTC date.

div() # args: x, y
# returns the integer before $1 / $2 ($(($1/$2)) is not OK for
# negative numbers)
{
  REPLY=$(($1 / $2))
  [ "$(($REPLY * $2))" -gt "$1" ] && REPLY=$(($REPLY - 1))

Quote:}

days_since_epoch() # args: year, month, day
{
  # returns (in $REPLY) the number of days since 1970/1/1 GMT
  # valid for any date in the limit of your integer size.
  # Expects y/m/d in the Gregorian calendar from 1752/9/14 on,
  # and in the Julian calendar until 1752/9/2. Note that even
  # though there were no days 3 nor 4 nor... 13 in September
  # 1752, if you provide those you will get the date in the
  # Gregorian calendar. days_since_epoch(1752, 9, 13) ==
  # days_since_epoch(1752, 9, 2).

  # "set" is used to avoid clobbering the variable namespace
  # with a temporary variable
  set -- "$((12 * ($1 + ($1 < 0)) + $2 - 3))" "$3"
  div "$1" 12
  set -- "$1" "$REPLY" "$2"
  div "$((367 * $1 + 7))" 12
  set -- "$1" "$2" "$3" "$REPLY"
  div "$2" 4
  set -- "$1" "$2" "$3" "$(($4 - 2 * $2 + $REPLY + $3))"
  if [ "$1" -eq 21030 ] && [ "$3" -lt 3 ] || [ "$1" -lt 21030 ]; then
    REPLY=$(($4 - 719471))
  else
    div "$2" 100
    set -- "$2" "$(($4 - $REPLY))"
    div "$1" 400
    REPLY=$(($2 + $REPLY - 719469))
  fi

Quote:}

timegm() # args: year, month, day, hour, minute, second
{
  days_since_epoch "$1" "$2" "$3"
  REPLY=$(($6 + 60 * ($5 + 60 * ($4 + 24 * $REPLY))))

Quote:}

var=03/12/89
IFS=/; set -f
set -- $var
Y=$(date +%Y)
d=${1#0} m=${2#0} y=${Y%??}${3#0}
[ "$(($y - $Y))" -gt 50 ] &&
  y=$(($y - 100))

timegm "$y" "$m" "$d" 0 0 0
printf '%s\n' "$REPLY"

# if you have GNU date:
date -d "$y-$m-$d 00:00:00 UTC" +%s

--
Stephane

 
 
 

How to convert dd/mm/yy to unix epoch time?

Post by DJ Stunk » Fri, 10 Mar 2006 07:28:01



> try this

> yymmdd=060302
> touch -t ${yymmdd}0000 afile
> perl -e 'print ((stat("afile"))[9])'

> thanks
> Michael Abootorab

lmao

-jp

 
 
 

1. Changing all date fields from mm/dd/yy to dd/mm/yy

Please, I'm looking for a way to change all date fields in a specified
file, like this:

solaris> cat file

05/23/06      03/12/06

07/10/06                   11/12/06           09/20/06

      03/11/06

I need change all date field from mm/dd/yy to dd/mm/yy, like this:

solaris> ./script < cat file

23/05/06        12/03/06

10/07/06                 12/11/06             20/09/06

       11/03/06

Thanks !

2. NIS+ Permission Denied to Group SYSADMIN, what gives?

3. Date-time conversion from "YYYY-MM-DD HH:MI:SS" to unix epoch

4. can't login as root

5. How to convert Posix date to YY/MM/DD ?

6. Display right-left switched

7. Files modified since yy/mm/dd hh:mm

8. CDRecord 1.6a7 with Yamaha 102 in RH5

9. How to convert Unix date to DD/MM/YYYY HH:MM:SS

10. how to convert given no. into unix dd:mm:yyyy -hh:mm:ss format

11. how can i convert a mm/dd/yyyy uu:mm:ss to a time_t ?

12. sort MM/DD/YY in reverse chron order with -k...

13. Sort by First Date Field (MM/DD/YY)