ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
$ cat t.pl
my $start = "2007-11-17 12:50:22";
my $stop = "2007-11-17 12:53:22";
my $diff = str2time($stop) - str2time($start);
printf "diff between %s and %s is %d seconds\n", $start, $stop, $diff;
diff between 2007-11-17 12:50:22 and 2007-11-17 12:53:22 is 180 seconds
Be careful when working with dates. If there is no time zone as part of the date string, and you are in a region where there is daylight savings in effect for some of the year there, the exact date and time is ambiguous.
If you format dates without a time zone, you will have problems like this time sequence where the time jumps "back". I've actually seen this sort of thing in production environment, and it can be a problem:
Sun Oct 28 01:58:10 2007
Sun Oct 28 01:59:10 2007
Sun Oct 28 01:00:10 2007
Sun Oct 28 01:01:10 2007
Where as the same time sequence printed with the time zone would look like this:
Sun Oct 28 01:58:10 BST 2007
Sun Oct 28 01:59:10 BST 2007
Sun Oct 28 01:00:10 GMT 2007
Sun Oct 28 01:01:10 GMT 2007
Consider the case where start time is Sun Oct 28 01:59:10 BST 2007 and stop time is Sun Oct 28 01:00:10 GMT 2007... with your date format you would end up with a negative result, which might go on to cause other problems in your software. By using timezones in your date strings can you avoid this. You can also avoid it by always using UTC for times in your application and converting them to local time only when displayed.