finding epoch seconds for given date.
Hi,
I could able to find out the epoch seconds for particular date in linux using
date -d 'Apr 2 2005 15:31:17' '+%s'
But I am looking for the commands in solaris which does the same work.
I will be thank full if some one could help me.
I tried to do manual......but epoch seconds that I get are differing..
I found following snippet in mktime function
556 unsigned long
557 mktime(const unsigned int year0, const unsigned int mon0,
558const unsigned int day, const unsigned int hour,
559const unsigned int min, const unsigned int sec)
560 {
561 unsigned int mon = mon0, year = year0;
562
563 /* 1..12 -> 11,12,1..10 */
564 if (0 >= (int) (mon -= 2)) {
565mon += 12;/* Puts Feb last since it has leap day */
566year -= 1;
567 }
568
569 return ((((unsigned long)
570(year/4 - year/100 + year/400 + 367*mon/12 + day) +
571year*365 - 719499
572 )*24 + hour /* now have hours */
573)*60 + min /* now have minutes */
574 )*60 + sec; /* finally seconds */
575 }
but could not make out why 367*mon/12.....instead of 365, I could not locate
the position where previous months days in this year added.
Is 719499 is number of days from 0th year to 1970 ?
Thank you,
Sateesh.
# 1
I normally perl it, even though i'm sure there are easier ways.
perl -e 'use Time::Local; print timelocal("17","31","15","02","03","2005"),"\n";'
or somewhat more explained:
perl -e 'use Time::Local; print timelocal("second","minute","hour","day of month","month (0 for january, 1 for february and so on)","year"),"\n";'
.7/M.
# 5
What part do you not understand? You gave the correct syntax in the first post.
% gnudate -d 'Apr 2 2005 15:31:17' '+%s'
1112484677
Are you just looking for algorithms that do the conversion? I typed that into google and this page popped up.
<http://www.esqsoft.com/javascript_examples/date-to-epoch.htm>
It's in javascript, but it explains the algorithm that is used. Is that what you're looking for?
--
Darren
# 6
Hi Darren,
Regarding the first option of OS command
date ...'+%s' works in linux only. It will not work in Solaris. So if I get some command in solaris which does same work it will be great.
Regarding second option, If I get algorithm, irrespective of OS I can write it in
shell. But in the link given by you I found the way to find number of leap years
betwen given year and 1970 is wrong.
Thank you for the help. I still could not able to solve the problem.
Thank you,
Sateesh.
# 7
> Hi Darren,
>Regarding the first option of OS command
> date ...'+%s' works in linux only. It will not
> work in Solaris. So if I get some command in solaris
> which does same work it will be great.
Most distributions that use a Linux kernel distribute the gnu 'date' program. That program works the same on Solaris as it does on Linux.That's why I suggested it to you earlier.
# /build/coreutils-5.93/bin/date '+%s'
1177951907
# uname -rsv
SunOS 5.8 Generic_117350-43
--
Darren