Re: Using timelocal function
by kyle (Abbot) on Feb 04, 2008 at 17:21 UTC
|
I'm not sure what your question is. Maybe if you posted some code with notes about what you'd like it to do, that would help.
You mention that you get 18000 when you wanted to get zero. I notice that this is five hours, and I wonder if that's how far your time zone is away from where the epoch date is midnight.
If the real problem is how to get the number of days since Jan 1, 1968, then there are a lot of date and time modules out there to help. For example, using DateTime...
use DateTime;
my $epoch = DateTime->new( year => 1968, month => 1, day => 1,
hour => 0, minute => 0, second => 0,
time_zone => 'America/Chicago' );
my $now = DateTime->now();
my $duration = $now->delta_days( $epoch );
print $duration->delta_days(), "\n";
__END__
14644
See also How do I find the difference in days between two dates, in a cool perl way? | [reply] [d/l] |
Re: Using timelocal function
by olus (Curate) on Feb 04, 2008 at 17:01 UTC
|
Could you please show us what you have done so far so that we can point any possible mistakes you may have done?
Please read How do I post a question effectively? for assistance on getting help from the monks.
| [reply] |
Re: Using timelocal function
by rpike (Scribe) on Feb 04, 2008 at 17:23 UTC
|
#=== Assuming March 31, 2008 from the epoch(?)
#=== This script will be running on both Windows and
#=== unix machines
$days = uniVerseDate(0,0,0,31,2,2008,0,0,0,1,0,1970
...
...
sub uniVerseDate {
my ($fS,$fM,$fH,$fD,$fMth,$fY,$sS,$sM,$sH,$sD,$sMth,$sY) = @_;
my ($fTot, $sTot, $diff, $secDays) = 0;
$fTot = timelocal($fS,$fM,$fH,$fD,$fMth,$fY);
$sTot = timelocal($sS,$sM,$sH,$sD,$sMth,$sY);
$diff = $fTot - $sTot;
$secsDay = 60 * 60 * 24;
#=== Add 732 to get days from Jan 1, 1968 to
#=== Jan 1, 1970
return (($diff / $secsDay) + 732);
}
| [reply] [d/l] |
|
|
I think 732 days is a bit long for two years, even given that 1968 was a leap year. According to my math (done with DateTime), there were 731 days.
I think the reason you're getting a trailing .9583333333 days (a little under an hour) is because your date range crosses daylight savings time (which begins in March). It might also have to do with various leap seconds that have been added over the years.
In any case, I think you'd be much better off using a module to do this calculation for you (see How do I find the difference in days between two dates, in a cool perl way?). They've already been through rigorous testing for this and many other problems.
| [reply] |
|
|
| [reply] |
|
|
According to the values I was getting back from my function the 732 added to the difference found gave me the exact numeric value that I needed. How do I "install" modules such as DateTime? I tried a few minutes earlier to put it into one of my namespaces and kept getting a CGI error back when I tried putting in use DateTime at the top of my .pl script. Thanks for the info. I'll give DateTime a try instead as the whole offset from GMT isn't too appealing.
| [reply] |
|
|
Daylight savings starts in April.
| [reply] |
|
|
|
|
Thank you for sharing your subroutine. This is an important piece of what is meant by an example.
Now all you have to do is call the subroutine with specific values for its parameters, and show us the output.
| [reply] |
Re: Using timelocal function
by ikegami (Patriarch) on Feb 05, 2008 at 01:07 UTC
|
Don't use timelocal for date math, use timegm. Not all days have the same length in many time zones.
use Time::Local qw( timegm );
my ($y1,$m1,$d1) = (1985,12,25);
my ($y2,$m2,$d2) = (2005,2,4);
my $t1 = timegm(0,0,0,$d1,$m1-1,$y1);
my $t2 = timegm(0,0,0,$d2,$m2-1,$y2);
my $days = ($t2-$t1)/(24*60*60);
print("$days\n"); # 6981
Furthermore, the epoch time (what timelocal/timegm returns) of many systems only works with dates/times starting midnight, Jan 1st, 1970.
| [reply] [d/l] |
Re: Using timelocal function
by rpike (Scribe) on Feb 04, 2008 at 17:54 UTC
|
Data going in : 0, 0, 0, 4, 1, 2008, 0, 0, 0, 1, 0, 1970
No of seconds from epoch for first date : 1202101200
No of seconds from epoch for second date : 18000
Difference of 2 dates in seconds : 1202083200
Return : 14645 (days returned from the function)
0, 0, 0, 1, 0, 2001, 0, 0, 0, 1, 0, 1970
No of seconds from epoch for first date : 978325200
No of seconds from epoch for second date : 18000
Difference of 2 dates in seconds : 978307200
Return : 12055 (days returned from the function)
This function always cave back with whole numbers but when I put in March 31, 2008 it came back with 14700.9583333333 days. | [reply] [d/l] |
Re: Using timelocal function
by Narveson (Chaplain) on Feb 04, 2008 at 17:05 UTC
|
| [reply] |
Re: Using timelocal function
by rpike (Scribe) on Feb 04, 2008 at 19:46 UTC
|
Thanks for all the info kyle. Strange isn't it how the ones most polite are the most knowledgable in most cases. Narveson, it's the whole responding to questions with questions and riddled with attitude that makes these discussion groups / forums all the less attractive. The constant questions and lack of an answer doesn't help anybody (i.e. doesn't give me any help and it doesn't make you look any smarter). I normally wouldn't respond in this way but the little jab about posting the example bit is a bit much. I appreciate any help I get in here but if you can't answer a question and all you can do is ask more questions and waste more time with ignorance I'd sooner you don't respond. Thanks again kyle for the friendly and informative responses. | [reply] |
|
|
rpike, it is not unreasonable to be asked to provide the code you're trying to get to work. It helps explain what you are trying to do, and it's possible that the problem isn't where you think it is. One line of code can equal a lot of words. Moreover, monks have often been known to make other helpful suggestions about coding style, module usage, alternative approaches, etc. when they can actually see your code, so it is to your benefit to post your code and examples.
In the future, if someone ticks you off I would just ignore them. The worst thing that can happen on this site is to have your posts ignored.
Concerning your date problem, I would consider using a module that specifically works with dates and not seconds. My favorite module for this is Time::JulianDay. Besides avoiding the timezone issue, there may be problems if you use timelocal with dates before the Unix epoch (1/1/1970).
| [reply] [d/l] [select] |