###############################################################################
# DateTime.pl                                                                 #
###############################################################################
# YaBB: Yet another Bulletin Board                                            #
# Open-Source Community Software for Webmasters                               #
# Version:        YaBB 2 RC 2                                                 #
# Released:       April 27, 2005                                              #
# Distributed by: http://www.yabbforum.com                                    #
# =========================================================================== #
# Copyright (c) 2000-2005 YaBB (www.yabbforum.com) - All Rights Reserved.     #
# Software by: The YaBB Development Team                                      #
#              with assistance from the YaBB community.                       #
# Sponsored by: Xnull Internet Media, Inc. - http://www.ximinc.com            #
#               Your source for web hosting, web design, and domains.         #
###############################################################################

$datetimeplver = 'YaBB 2 RC 2 $Revision: 1.45 $';
if($action eq 'detailedversion') { return 1; }


use Time::Local 'timelocal';

sub calcdifference {  # Input: $date1 $date2
	$result=int($date2/86400)-int($date1/86400);
	#$result= int($date2/86400)-int($date1/86400);
}


sub calctime {  # Input: $date1 $date2
	$result = $date2-$date1;
}

sub timetostring {
	unless( $_[0] ) { return 0; }
	if(!$maintxt{'107'}) {$maintxt{'107'} = "at";}
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$dummy) = gmtime($_[0] + (3600*$timeoffset));
	($dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$dummy,$isdst) = localtime($_[0] + (3600*$timeoffset));
	$mon_num = $mon+1;
	if($isdst > 0){$hour = $hour + $dstoffset;}
	if($hour == 24){$hour = 0;}
	$savehour = $hour;
	$hour = "0$hour" if ($hour < 10);
	$min = "0$min" if ($min < 10);
	$sec = "0$sec" if ($sec < 10);
	$saveyear = ($year % 100);
	$year = 1900 + $year;

	$mon_num = "0$mon_num" if ($mon_num < 10);
	$mday = "0$mday" if ($mday < 10);
	$saveyear = "0$saveyear" if ($saveyear < 10);
	return "$mon_num/$mday/$saveyear $maintxt{'107'} $hour\:$min\:$sec";
}

sub stringtotime {
	unless( $_[0] ) { return 0; }
	$splitvar = $_[0];
	$splitvar =~ m~(\d{2}\/\d{2}\/\d{2})(.*?)(\d{2}\:\d{2}\:\d{2})~;
	my $adate = $1;
	my $atime = $3;
	my( $amonth, $aday, $ayear ) = split(m~/~, $adate);
	my( $ahour, $amin, $asec ) = split (m~:~, $atime);
	$asec = int($asec) || 1;        # Default to 1 so timelocal doesn't die
	$amin = int($amin) || 1;
	$ahour = int($ahour) || 1;
	$ayear = int($ayear) || 1;
	$amonth = int($amonth) || 1;
	$aday = int($aday) || 1;

# Uses 1904 and 2036 as the default dates, as both are leap years.
# If we used the real extremes (1901 and 2038) - there would be problems
# As timelocal dies if you provide 29th Feb as a date in a non-leap year
# Using leap years as the default years prevents this from happening.

    if ($ayear >= 36 && $ayear <= 99) { $ayear += 1900; }
    elsif ($ayear >= 00 && $ayear <= 35) { $ayear += 2000; }
    if ($ayear < 1904) { $ayear = 1904; }
    elsif ($ayear > 2036) { $ayear = 2036; }

	if( $amonth < 1 ) { $amonth = 0; }
	elsif( $amonth > 12 ) { $amonth = 11; }
	else { --$amonth; }

# Just some error checking to make sure times are in correct range
# If they are outside the correct range, timelocal dies. Which is bad.

	if( $aday < 1 ) { $aday = 1; }
	elsif( $aday > 31 ) { $aday = 28; }
	if( $ahour < 1 ) { $ahour = 1; }
	elsif( $ahour > 23 ) { $ahour = 23; }
	if( $amin < 1 ) { $amin = 1; }
	elsif( $amin > 59 ) { $amin = 59; }
	if( $asec < 1 ) { $asec = 1; }
	elsif( $asec > 59 ) { $asec = 59; }

	return( timelocal($asec, $amin, $ahour, $aday, $amonth, $ayear));
}


sub timeformat {
	$oldformat = $_[0];
	chomp $oldformat;
	if(!$oldformat) { return $oldformat; }
	$dontusetoday = $_[1];
	$mytimeselected = ${$uid.$username}{'timeselect'} || $timeselected;
	(undef, undef, undef, undef, undef, undef, undef, undef, $newisdst) = localtime($oldformat);
	if($newisdst > 0){$userdst = ${$uid.$username}{'dsttimeoffset'}  || $dstoffset; $dst=1;}
	# we find out what time the date of the post is according to the users timezone.
	if ($iamguest){
		$toffs = $timeoffset;
		$dstoffs = $dstoffset;
	} else {
		$toffs = ${$uid.$username}{'timeoffset'};
		$dstoffs = $userdst;
	}
	my ($newsecond,$newminute,$newhour,$newday,$newmonth,$newyear,$newweekday,$newyearday,$dummy) = gmtime($oldformat + (3600 * ($toffs + $dstoffs)));
	# Calculate number of full weeks this year
	$newweek = int(( $newyearday + 1 - $newweekday ) / 7 )+1;
	# Add 1 if today isn't Saturday
	if ($newweekday < 6) { $newweek = $newweek + 1; }
	$newweek = sprintf ("%02d", $newweek);
	$newmonth++;
	$newweekday++;
	$newyear += 1900;
	$newshortyear = substr($newyear,2,2);
	$newmonth = sprintf ("%02d", $newmonth);
	if ($mytimeselected != 4) { $newday = sprintf ("%02d", $newday); }
	$newhour = sprintf ("%02d", $newhour);
	$newminute = sprintf ("%02d", $newminute);
	$newsecond = sprintf ("%02d", $newsecond);

	$newtime = $newhour.":".$newminute.":".$newsecond;

	($secx,$minx,$hourx,$dd,$mm,$yy,$tmpx,$yd,$tmpx) = gmtime($date + (3600*($toffs + $dstoffs)));

	$mm = $mm + 1;
	$yy = ($yy % 100);

	$daytxt="";
	#the today yesterday problem was here... =(
	if (!$dontusetoday) {
		if ($yd == $newyearday && $yy == $newshortyear) {
			# today
			$daytxt = qq~<b>$maintxt{'769'}</b>~;
		} elsif ( ($yd == $newyearday+1 && $yy == $newshortyear) || ( $yd==1 && $dd==31 && $yy == $newshortyear+1)) {
			# yesterday
			$daytxt = qq~<b>$maintxt{'769a'}</b>~;
		} elsif ( ($yd == $newyearday-365 && $yy == $newshortyear+1) || ($yd == $newyearday-366 && $yy == $newshortyear+1) ){
			# yesterday, over a year end.
			$daytxt = qq~<b>$maintxt{'769a'}</b>~;
		}
	}

	if (!$maintxt{'107'}) {$maintxt{'107'} = $admin_txt{'107'};}

	if ($mytimeselected == 7) {
		$mytimeformat = ${$uid.$username}{'timeformat'};
		if($mytimeformat =~ m/MM/){$usefullmonth = 1;}
		if($mytimeformat =~ m/hh/){$hourstyle = 12;}
		if($mytimeformat =~ m/HH/){$hourstyle = 24;}
		$mytimeformat =~ s/\@/$maintxt{'107'}/g;
		$mytimeformat =~ s/mm/$newminute/g;
		$mytimeformat =~ s/ss/$newsecond/g;
		$mytimeformat =~ s/ww/$newweek/g;

		if($mytimeformat =~ m/\+/){
			if( $newday > 10 && $newday < 20 ) {
				$dayext = "<sup>$timetxt{'4'}</sup>";
			} elsif( $newday % 10 == 1 ) {
				$dayext = "<sup>$timetxt{'1'}</sup>";
			} elsif( $newday % 10 == 2 ) {
				$dayext = "<sup>$timetxt{'2'}</sup>";
			} elsif( $newday % 10 == 3 ) {
				$dayext = "<sup>$timetxt{'3'}</sup>";
			} else {
				$dayext = "<sup>$timetxt{'4'}</sup>";
			}
		}
		if($hourstyle == 12){
			$ampm = $newhour > 11 ? 'pm' : 'am';
			$newhour2 = $newhour % 12 || 12;
			$mytimeformat =~ s/hh/$newhour2/g;
			$mytimeformat =~ s/\#/$ampm/g;
		} elsif($hourstyle == 24){
			$mytimeformat =~ s/HH/$newhour/g;
		}
		if($daytxt eq ""){
			$mytimeformat =~ s/YYYY/$newyear/g;
			$mytimeformat =~ s/YY/$newshortyear/g;
			$mytimeformat =~ s/DD/$newday/g;
			$mytimeformat =~ s/D/$newday/g;
			$mytimeformat =~ s/\+/$dayext/g;
			if($usefullmonth == 1){
				$mytimeformat =~ s/MM/$months[$newmonth-1]/g;
			} else {
				$mytimeformat =~ s/M/$newmonth/g;
			}
		} else {
			$mytimeformat =~ s/DD/$daytxt/g;
			$mytimeformat =~ s/D/$daytxt/g;
			$mytimeformat =~ s/YY//g;
			$mytimeformat =~ s/M//g;
			$mytimeformat =~ s/\+//g;
		}
		if ($newisdst && ${$uid.$username}{'dsttimeoffset'} != 0){ 
			$mytimeformat =~ s/\*/$maintxt{'dst'}/g;
		} else {
			$mytimeformat =~ s/\*//g;
		}
		$newformat = $mytimeformat;
	} elsif ($mytimeselected == 1) {
		$newformat = $daytxt ? qq~$daytxt $maintxt{'107'} $newtime~ : qq~$newmonth/$newday/$newshortyear $maintxt{'107'} $newtime~;
	} elsif ($mytimeselected == 2) {
		$newformat = $daytxt ? qq~$daytxt $maintxt{'107'} $newtime~ : qq~$newday.$newmonth.$newshortyear $maintxt{'107'} $newtime~;
	} elsif ($mytimeselected == 3) {
		$newformat = $daytxt ? qq~$daytxt $maintxt{'107'} $newtime~ : qq~$newday.$newmonth.$newyear $maintxt{'107'} $newtime~;
	} elsif ($mytimeselected == 4) {
		$ampm = $newhour > 11 ? 'pm' : 'am';
		$newhour2 = $newhour % 12 || 12;
		$newmonth2 = $months[$newmonth-1];
		if( $newday > 10 && $newday < 20 ) {
			$newday2 = "<sup>$timetxt{'4'}</sup>";
		} elsif( $newday % 10 == 1 ) {
			$newday2 = "<sup>$timetxt{'1'}</sup>";
		} elsif( $newday % 10 == 2 ) {
			$newday2 = "<sup>$timetxt{'2'}</sup>";
		} elsif( $newday % 10 == 3 ) {
			$newday2 = "<sup>$timetxt{'3'}</sup>";
		} else {
			$newday2 = "<sup>$timetxt{'4'}</sup>";
		}
		$newformat = $daytxt ? qq~$daytxt $maintxt{'107'} $newhour2:$newminute$ampm~ : qq~$newmonth2 $newday$newday2, $newyear, $newhour2:$newminute$ampm~;
	} elsif ($mytimeselected == 5) {
		$ampm = $newhour > 11 ? 'pm' : 'am';
		$newhour2 = $newhour % 12 || 12;
		$newformat = $daytxt ? qq~$daytxt $maintxt{'107'} $newhour2:$newminute$ampm~ : qq~$newmonth/$newday/$newshortyear $maintxt{'107'} $newhour2:$newminute$ampm~;
	} elsif ($mytimeselected == 6) {
		$newmonth2 = $months[$newmonth-1];
		$newformat = $daytxt ? qq~$daytxt $maintxt{'107'} $newhour:$newminute~ : qq~$newday. $newmonth2 $newyear $maintxt{'107'} $newhour:$newminute~;
	}
	return $newformat;
}


sub CalcAge {
	$currentdate = timetostring(int(time));
	my($usermonth, $userday, $useryear, $act);
	my $user = $_[0];
	my $act = $_[1];

	if(${$uid.$user}{'bday'} ne '') {
		($usermonth, $userday, $useryear) = split(/\//, ${$uid.$user}{'bday'});

		if($act eq "calc") {
			if(length(${$uid.$user}{'bday'}) <= 2) { $age = ${$uid.$user}{'bday'}; }
			else {
				$age = $year - $useryear;
				if($usermonth > $mon_num || ( $usermonth == $mon_num && $userday > $mday ) ) { --$age; }
			}
		}
		if($act eq "parse") {
			if(length(${$uid.$user}{'bday'}) <= 2) { return; }
			$umonth = $usermonth;
			$uday = $userday;
			$uyear = $useryear;
		}
		if($act eq "isbday") {
			if($usermonth == $mon_num && $userday == $mday) { $isbday = "yes"; }
		}
	}
	else {
		$age = "";
		$isbday = "";
	}
}

1;