Thanks to Corion, I managed to do it. Here is what I needed to get an Excel format timestamp (I added some comment, to make it easier to understand) :
# This function will return 1 if it is a leap year, or 0 if it's not sub LeapYear { my ($iYear) = @_; return 1 if ( $iYear == 1900 ); return ( ( ( $iYear % 4 ) == 0 ) && ( ( $iYear % 100 ) || ( $iYear % 400 ) == 0 ) ) ? 1 : 0; } sub LocaltimeExcel { my ( $iSec, $iMin, $iHour, $iDay, $iMon, $iYear, $iwDay, $iMSec ) += @_; # Changing year and month to the right format $iMon++; $iYear += 1900; # Time calculation my $iTime; $iTime = $iHour; $iTime *= 60; $iTime += $iMin; $iTime *= 60; $iTime += $iSec; $iTime += $iMSec / 1000.0 if ( defined($iMSec) ); $iTime /= 86400.0; #3600*24 my $iY; my $iYDays; # Date calculation $iY = 1900; $iYDays = 366; while ( $iY < $iYear ) { $iTime += $iYDays; $iY++; $iYDays = ( LeapYear($iY) ) ? 366 : 365; } for ( my $iM = 1 ; $iM < $iMon ; $iM++ ) { if ( $iM == 1 || $iM == 3 || $iM == 5 || $iM == 7 || $iM == 8 || $iM == 10 || $iM == 12 ) { $iTime += 31; } elsif ( $iM == 4 || $iM == 6 || $iM == 9 || $iM == 11 ) { $iTime += 30; } elsif ( $iM == 2 ) { $iTime += ( LeapYear($iYear) ) ? 29 : 28; } print LeapYear($iYear) . "\n"; } $iTime += $iDay; return $iTime; }
These are the modified functions I copied in Spreadsheet::ParseExcel::Utility. You can use them like the following (for example) :
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); my $timestamp = &LocaltimeExcel( $sec, $min, $hour, $mday, $mon, $year + ); $timestamp =~ s/\./\,/; print $timestamp;
Here, I convert the current localtime into what I need. I hope this will be useful for someone else.
Thanks Monks !
Varkh
In reply to Re: Using Excel timestamp with perl
by Varkh
in thread Using Excel timestamp with perl
by Varkh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |