Gossamer Forum
Home : Products : DBMan : Customization :

datetounix

Quote Reply
datetounix
Hi all,

can you help me with sub datetounix, I can make it enter date and time, but can not convert it back to epoch time.

here is my default.cfg
Code:

'Start Time' => [7, 'alpha', 12, 18, 1, &get_date, ''],

'Received Time' => [8, 'alpha', 12, 18, 0, '', ''],
'Status' => [9, 'alpha', 0, 60, 1, 'Waiting', ''],
'Down Time' => [10, 'alpha', 12, 18, 0, '', '']
);

and here is my index.pl
Code:
sub get_time {
# --------------------------------------------------------
# Returns the time in the format "hh-mm-ss".
#
my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);
($sec < 10) and ($sec = "0$sec");
($min < 10) and ($min = "0$min");
($hour < 10) and ($hour = "0$hour");

return "$hour:$min:$sec";
}

sub get_date {
# --------------------------------------------------------
# Returns the date in the format "Mmmm dd, yyyy hh:mm ".
# Warning: If you change the default format, you must also modify the &date_to_unix
# subroutine below which converts your date format into a unix time in seconds for sorting
# purposes.
my ($time) = @_;
($time) || ($time = time());

my ($sec, $min, $hour, $day, $mon, $year, $dweek, $dyear, $daylight) = localtime($time);

($day < 10) and ($day = "0$day");

$mon += 1;
($mon < 10) and ($mon = "0$mon");

$year = $year + 1900;

if ($min < 10) {
$min = "0" . $min;
}

return "$mon\/$day\/$year $hour:$min";


}





sub date_to_unix {

my ($date) = $_[0];

my ($time);
my ($mon, $day, $year, $time) = split(/ /, $date);
$day =~ s/,//;
my ($hour, $min) = split(/:/, $time);

unless ($day and $mon and $year) { return undef; }

use Time::Local;
eval {
$day = int($day); $year = int($year) - 1900;
$min = int($min); $hour = int($hour);
$time = timelocal(0,$min,$hour,$day, $mon, $year);
};
if ($@) { return undef; } # Could return 0 if you want.
return ($time);
}

and now is my html.pl
Code:
sub html_record {
# --------------------------------------------------------
# How a record will be displayed. This is used primarily in
# returning search results and how it is formatted. The record to
# be displayed will be in the %rec hash.

my (%rec) = @_; # Load any defaults to put in the VALUE field.
($db_auto_generate and print &build_html_record(%rec) and return);


my $font_color = 'Font face="Verdana, Arial, Helvetica" Size=-1 Color=#3300FF';
my $font = 'Font face="Verdana, Arial, Helvetica" Size=-1';



if ($rec{'Status'} eq "Waiting") {

# Calculate Down Time if we received part.


$now = time();

$diff = (time() - (&date_to_unix($rec{'Start Time'})) );



$seconds = $diff % 60;
$diff = ($diff - $seconds) / 60;
$min = int($diff % 60);
$diff = ($diff - $min) / 60;

$hrs = $diff % 24;

$diff = ($diff - $hrs) / 24;
$days = $diff % 7;


print qq|
<TD bgcolor=yellow>$rec{'System SFC'}</TD><TD bgcolor=yellow>$rec{'Work Order'}</TD><TD bgcolor=yellow>$rec{'System Type'}</TD><TD bgcolor=yellow>$rec{'Shortage'}</TD><TD bgcolor=yellow>$rec{'Description'}</TD><TD bgcolor=yellow>$rec{'Area'}</TD><TD bgcolor=yellow>$rec{'Start Time'} </TD><TD bgcolor=yellow>$rec{'Received Time'}&nbsp;</TD><TD bgcolor=yellow>$rec{'Status'}&nbsp;</TD><TD bgcolor=yellow> $days days $hrs:$min</TD>|;

}
else {

$diff = ((&date_to_unix($rec{'Received Time'})) - (&date_to_unix($rec{'Start Time'})));
$seconds = $diff % 60;
$diff = ($diff - $seconds) / 60;
$min = int($diff % 60);
$diff = ($diff - $min) / 60;

$hrs = $diff % 24;

$diff = ($diff - $hrs) / 24;
$days = $diff % 7;


print qq|
<TD bgcolor=d3d3d3>$rec{'System SFC'}</TD><TD bgcolor=d3d3d3>$rec{'Work Order'}</TD><TD bgcolor=d3d3d3>$rec{'System Type'}</TD><TD bgcolor=d3d3d3>$rec{'Shortage'}</TD><TD bgcolor=d3d3d3>$rec{'Description'}</TD><TD bgcolor=d3d3d3>$rec{'Area'}</TD><TD bgcolor=d3d3d3>$rec{'Start Time'} and $estart</TD><TD bgcolor=d3d3d3>$rec{'Received Time'}&nbsp</TD><TD bgcolor=d3d3d3>$rec{'Status'}&nbsp;</TD><TD bgcolor=d3d3d3>$days days $hrs:$min</TD>|;
}

}

I would like to make it if the status is Waiting then, downtime is (now - start time) and give me in days hrs and minutes.
But if status is not waiting (received) then my downtime is ( recived time - starttime) and give me in days hrs:minutes.

What did I do wrong ?
Thanks for your help.
TDBlush
Quote Reply
Re: [britneythuyduong] datetounix In reply to
This sounds like a question with someone who is a programmer but since you didn't receive a response I'll provide a suggestion which may be able to help you.

I'm thinking you may need to setup separate subs which would do the calculations for you. Thinking about how I used portions of the autodelete mod to calculate times ads would expire in my classifieds database.

Perhaps looking at the autodelete mod would help you to find a solution.

There are also various threads in the FAQ for working with dates which may be of some help.

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/