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

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

Post by newbi » Mon, 11 Aug 2003 13:15:08



I need to convert a date format written to a file by a proprietary app
from "YYYY-MM-DD HH:MI:SS" to the unix epoch time ... which will be feed
to rrdtool.

Anyone know of an available command-line utility to do that or do I have
to write one myself ?

 
 
 

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

Post by Mike Delane » Mon, 11 Aug 2003 16:09:52




:  
:  I need to convert a date format written to a file by a proprietary app
:  from "YYYY-MM-DD HH:MI:SS" to the unix epoch time ... which will be feed
:  to rrdtool.
:  
:  Anyone know of an available command-line utility to do that or do I have
:  to write one myself ?
:  

The GNU version of date will do what you want:


1060499855

 
 
 

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

Post by Val3r1 » Tue, 02 Sep 2003 06:45:55


Originally posted by Mike Delaney

> The GNU version of date will do what you want:


> 1060499855

Thanks. And what about the reverse function ?

Something like fromunixtime(), directly on the Unix shell ?

Thanks again

--
Posted via http://dbforums.com

 
 
 

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

Post by Chi Hung Ch » Tue, 02 Sep 2003 12:52:35


On Solaris, gdate is not available. Even my RH9 does not come with
gdate.
I provide perl and tcl implementations. At least Perl 5.0 comes with
Solaris 8
I find Tcl is easier in this context. Hope it helps

Perl:
#! /usr/bin/perl

use Time::Local;

($yr,$mth,$day,$hr,$min,$sec)=split(/[-:]/,"2003-04-05:06:07:08");

# avoid octal
$mth=~s/^0//; $day=~s/^0//; $hr=~s/^0//; $min=~s/^0//; $sec=~s/^0//;

$time=timelocal($sec,$min,$hr,$day,$mth-1,$yr);
print $time, "\n";

($_sec,$_min,$_hr,$_day,$_mth,$_yr)=localtime($time);
printf("%4d-%02d-%02d:%02d:%02d:%02d\n",1900+$_yr,1+$_mth,$_day,$_hr,$_min,$_sec);

Tcl:
#! /usr/local/bin/tclsh

# conver "2003-04-05:06:07:08" to "2003-04-05 06:07:08"
regexp {(\d+)-(\d+)-(\d+):(\d+):(\d+):(\d+)} "2003-04-05:06:07:08" x
yr mth day hr min sec
set tt [clock scan "$yr-$mth-$day $hr:$min:$sec"]
puts $tt
puts [clock format $tt -format {%Y-%m-%d:%H:%M:%S}]


> Originally posted by Mike Delaney

> > The GNU version of date will do what you want:


> > 1060499855

> Thanks. And what about the reverse function ?

> Something like fromunixtime(), directly on the Unix shell ?

> Thanks again