Gossamer Forum
Home : Products : DBMan : Customization :

More Unix Date ???

Quote Reply
More Unix Date ???
Hi Carol, it's me again still messing with my date/time sort deally-bob. The last time you helped me with a (human date/time) to Unix coversion. Well I am just now getting back to work on that and I have found a new problem.

I think I need a zero stripping function to strip the zero's from the human date $hour, $min, $sec

Here is the date/time format I am using:
Aug-17-1999 08:00:49

The PostTime field equals the above date/time string.

my $SortDate = &date_to_unix("$rec{'PostTime'}");


I am passing the above date string to the date-to-unix sub but whenever the hour changes it comes up with the wrong unix time.


Here is my date-to-unix sub:
sub date_to_unix {

my ($date) = $_[0];
my (%months) = ("Jan" => 0, "Feb" => 1, "Mar" => 2, "Apr" => 3, "May" => 4, "Jun" => 5,
"Jul" => 6, "Aug" => 7, "Sep" => 8, "Oct" => 9, "Nov" => 10,"Dec" => 11);

my ($time);
my ($mon, $day, $year, $hour, $min, $sec) = split(/ /, $_[0]);
my ($mon, $day, $year) = split(/-/, $_[0]);
my ($hour, $min, $sec) = split(/:/, $_[0]);
unless ($mon and $day and $year) { return undef; }
unless (defined($months{$mon})) { return undef; }
use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$time = timelocal($sec,$min,$hour,$day,$months{$mon},$year);
};

if ($@) { return undef;} # Could return 0 if you want.
return ($time);
}

Don't I need to add the reverse of this to strip the zero's ?

($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");
($hour < 10) and ($hour = "0$hour");

This I copied from my get date/time sub, I know the above adds zero's if less than 10. I just don't know how to remove them in the date-to-unix sub or if I have to.

Remember my date sort has to include the time, that is why we came up with a combined date/time sub.

I think this is my last big hurtle once I get past this I'm home free. Yeehah

I hope things are going well for you

Quote Reply
Re: More Unix Date ??? In reply to
This looks like a problem:

Code:
my ($mon, $day, $year, $hour, $min, $sec) = split(/ /, $_[0]);
my ($mon, $day, $year) = split(/-/, $_[0]);
my ($hour, $min, $sec) = split(/:/, $_[0]);

Let me explain what this code will do, first, and then I'll suggest a fix for it.

Since your date/time is in the form of Aug-17-1999 08:00:49, and the first line splits the string at the space, after the first line, you end up with

$mon = 'Aug-17-1999'
$day = '08:00:49'
$year = ''
$hour = ''
$min = ''
$sec = ''

With the second line, you are splitting the full date/time string again, since you're using $_[0]. After the second line, you would have

$mon = 'Aug'
$day = '17'
$year = '1999 08:00:49'
$hour = ''
$min = ''
$sec = ''

After the third line, you would have

$mon = 'Aug'
$day = '17'
$year = '1999 08:00:49'
$hour = 'Aug-17-1999 08'
$min = '00'
$sec = '49'

This is not what you want.

This is what I would try:

Code:
my ($date1,$time1) = split(/ /, $_[0]);
my ($mon, $day, $year) = split(/-/, $date1);
my ($hour, $min, $sec) = split(/:/, $time1);

This will put the correct values into the variables.

You don't need to explicitly strip the 0s off. You can just use

Code:
$hour = int($hour);
$min = int($min);
$sec = int($sec);

That will, in effect strip off leading 0s.


------------------
JPD





Quote Reply
Re: More Unix Date ??? In reply to
Thank you Carol, it looks like it is working fine now. Once again you come to my rescue.

I owe you BIG time woman!!! Email me any time you need a hand, I will bend over backwards to do whatever grunt work you need done. With your help I may even end up holding my own with Perl. Thanks Again! Smile