Dear Expert
How to convert date in format of YYYY-MM-DD HH:MM:SS to unix format
using a script or command if avaliable
Example
"2011-05-15 18:00:00" is converted to 1330970400
I tried to use option d in date command but no use, Im using solaris 10
Thanks a lot
try this:
perl -e 'print time . "\n"'
ctsgnb
April 5, 2011, 8:04am
3
The standard date command on solaris does not handle such conversion, download and intall the GNU date instead :
# date -d "2011-05-15 18:00:00 UTC" +'%s'
1305482400
# date -d "1970-01-01 00:00:00 UTC + 1305482400 seconds" -u +'%Y-%m-%d %T %Z'
2011-05-15 18:00:00 UTC
This is a questionable advice: it will solve the problem but at the expense of portability. Scripts should be written do expect the common minimum of utilities offered by the OS.
I suggest you take a look at Perderabos datecalc script - probably the last date calculation tool you will ever need.
I hope this helps.
bakunin
Using ksh93, which is available on Solaris 10 as /usr/bin/ksh93 and on Solaris 11 as /usr/bin/ksh:
$ printf "%(%s)T\n" "2011-05-15 18:00:00 UTC"
1305482400
$
2 Likes
fpmurphy:
Using ksh93
Absolutely fantastic, i wasn't aware of this feature in ksh93. Thank you for enlightening me.
bakunin
Thanks... What if I need it for predefined time ?
---------- Post updated at 10:25 AM ---------- Previous update was at 10:20 AM ----------
Thank you
---------- Post updated at 10:26 AM ---------- Previous update was at 10:25 AM ----------
I never heard of ksh93, how to use it as i tried it on solaris 10 but not found
A KSH or Perl script is hardly portable in that sense, though it's potentially easier to install Perl or a recent KSH than GNU date.
Date math/conversion/formatting, I think, remains a pretty big pothole.
I never heard of ksh93, how to use it as i tried it on solaris 10 but not found
Korn shell (ksh) usually comes in a 1988 or a 1993 version.
AFAIK Solaris has a ksh93 available.
I hope this helps.
bakunin
yahyaaa:
Thanks... What if I need it for predefined time ?
---------- Post updated at 10:25 AM ---------- Previous update was at 10:20 AM ----------
Thank you
---------- Post updated at 10:26 AM ---------- Previous update was at 10:25 AM ----------
I never heard of ksh93, how to use it as i tried it on solaris 10 but not found
if you want to use Perl for predefined date you can use below code:-
#!/usr/bin/perl -l
use Date::Parse;
print str2time("Mon Apr 18 16:45:00 UTC 2011"); #1303145100
print str2time("04/18/2011 07:45:00PM"); #1303145100
print str2time("2011-04-18 19:45:00") ; ##local time zone is Asia/Amman which is GMT+3 #1303145100
print str2time("2011-04-18 16:45:00 UTC") ; #1303145100
print str2time("2011-04-18 16:45:00 GMT") ; #1303145100
print str2time("2011-04-19 01:45:00 JST") ; #1303145100
Note:- JST time is Japan standard time (UTC/GMT+9), and UTC is same as GMT time.
;););):D:D:D
1 Like
Thanks a lot ...its working
I do not have /usr/bin/ksh93 on either update 4 or update 8. What revision of Solaris 10 are you using? I do see /usr/dt/bin/dtksh which claims to be based on ksh 93 but...
$
$ /usr/dt/bin/dtksh
$ printf "%(%s)T\n" "2011-05-15 18:00:00 UTC"
/usr/dt/bin/dtksh: printf: T: unknown format specifier
$ set -o emacs
$ Version M-12/28/93d
I typed a cntl-V after the "set -o emacs" to get the version info.