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.
$ 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.