Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Problem with localtime

Posted by mescorpion (mescorpion), 14 June 2005
I am trying to find difference between 2 given dates...
I have the date in the format as the "date" command output.
Lets say:
Tue Jun 14 00:15:35 PDT 2005

So i am trying to find the difference...
the easier way i found was while taking the ouput of date command take the output of
$ date +%s

or else we can use time function also while executing the script..
So we will take the time in seconds while executing the date...
so now... for finding the difference... we will find the difference between seconds... and convert it into HH:MM:SS.

So i will subtract the seconds and pass it to localtime which give me the string in format.... lets assume difference... is always less than 24 hrs...
in that case it has to return me the correct time after 1 Jan 1970 right ....

mean to say if i pass 0 seconds to localtime it should return 1st Jan 1970 00:00:00.
But localtime is showing something else...
is that depends upon some other thing...
is that logic correct...

please answer....

Posted by admin (Graham Ellis), 14 June 2005
localtime returns a string containing the time and date if you run it in a scalar context, and a list of the various components of the time and date if you run it in a list context.   Thus:

$now = time();

$demo = localtime($now);
@demo = localtime($now);

$demo2 = localtime(0);
@demo2 = localtime(0);

print ("$demo\n");
print ("@demo\n");
print ("$demo2\n");
print ("@demo2\n");

gives you

Tue Jun 14 09:01:51 2005
51 1 9 14 5 105 2 164 1
Thu Jan  1 01:00:00 1970
0 0 1 1 0 70 4 0 0

Posted by mescorpion (mescorpion), 14 June 2005
the thing is on my linux system

here is what i have coded:

$ cat
print $demo;

Here is the output:

$ perl
Wed Dec 31 16:00:00 1969

Thats y i am got worried, is something wrong with my system....

Posted by admin (Graham Ellis), 14 June 2005
I think you're 8 hours behind GMT?  West coast of the USA?

You are using the LOCALtime function to get the time on your system ... where at 4p.m. on 31st December, it's midnight here in London.

Note the gmtime function which returns information about normalised (GMT) times.

Posted by mescorpion (mescorpion), 14 June 2005
hey its working fine with gmtime...

thanks for that... i used gmtime its working fine...
rather than using localtime its giving correct results....

$ cat
print $demo;
print "\n".$demo1;

$ perl
Wed Dec 31 16:00:00 1969
Thu Jan  1 00:00:00 1970

Thank u very much

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho