From 428c433dc245e23389fde47371cc8a5419e9fb14 Mon Sep 17 00:00:00 2001
From: omidmottaghi <omidmottaghi>
Date: Sun, 9 Jul 2006 22:55:01 +0000
Subject: [PATCH] some bugs in persian_mktime temporary fixed!

---
 include/functions_calendars.inc.php | 74 ++++++++++++++++++++++-------
 1 file changed, 57 insertions(+), 17 deletions(-)

diff --git a/include/functions_calendars.inc.php b/include/functions_calendars.inc.php
index d0f597e..102fe84 100644
--- a/include/functions_calendars.inc.php
+++ b/include/functions_calendars.inc.php
@@ -426,7 +426,7 @@ function persian_date_utf($format, $timestamp='') {
 
 /**
  * Create a Unix timestamp for a Persian date
- * This function only works with month >= 0 and day > 0
+ * This function works only with day > 0
  *
  * @author Omid Mottaghi
  * @access public
@@ -440,6 +440,8 @@ function persian_date_utf($format, $timestamp='') {
  * @return  int returned timestamp
  */
 function persian_mktime($hour='', $min='', $sec='', $mon='', $day='', $year='', $is_dst=-1){
+	$j_days_in_month = array(, 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
+	
 	if ( (string) $hour == '') { $hour = persian_date_utf('H'); }
 	if ( (string) $min  == '') { $min  = persian_date_utf('i'); }
 	if ( (string) $sec  == '') { $sec  = persian_date_utf('s'); }
@@ -447,24 +449,62 @@ function persian_mktime($hour='', $min='', $sec='', $mon='', $day='', $year='',
 	if ( (string) $mon  == '') { $mon  = persian_date_utf('n'); }
 	if ( (string) $year == '') { $year = persian_date_utf('Y'); }
 	
-	// these lines is temporary
-	if($mon == 0){
-		$year --;
-		$mon = 12;
-	}
+	/* 
+	   an ugly, beta code snippet to support days <= zero!
+	   it should work, but days in one or more months should calculate!
+	*/
 	
-	/*if($mon <= 0){
-		// change sign and plus one!
-		$mon = abs($mon) + 1;
-		echo $mon.'<br>';
-		// calculate years and months that should be decreased
-		$years  = floor($mon/12) + 1;
-		$months = ($years*12) - $mon + 1;
-		echo $years.'<br>';
-		die($months);
+	/*
+	if($day <= 0){
+		// change sign
+		$day = abs($day);
+		
+		// calculate months and days that shall decrease
+		// this do-while has a lot of errors!!!
+		do{
+			// $month_days = $j_days_in_month[$mon]
+			$months  = floor($day/30);
+			$days = $day % 30;
+		}while();
+		
+		$mon -= $months;
+		$day -= $days;
+		if ($day < 1) {
+			$mon--;
+		}
+	}
+	*/
+
+	if($mon <= 0){
+		// change sign
+		$mon = abs($mon);
+		
+		// calculate years and months that shall decrease
+		$years  = floor($mon/12);
+		$months = $mon % 12;
+		
 		$year -= $years;
-		$mon  = $months;
-	}*/
+		$mon  -= $months;
+		if ($mon < 1) {
+			$year--;
+			$mon += 12;
+		}
+	}
+	
+	if ($day < 1) {
+		$temp_month = $mon-1;
+		$temp_year  = $year;
+		if($temp_month <= 0){
+			$temp_month = 12;
+			$temp_year--;
+		}
+		if ($temp_month>1 && (($temp_year%4==0 && $temp_year%100!=0) || ($temp_year%400==0))){
+        	$j_days_in_month[12] = 30;
+    	}else{
+    		$j_days_in_month[12] = 29;
+    	}
+		$day += $j_days_in_month[$temp_month];
+	}
 	
     list($year, $mon, $day)=p2g($year, $mon, $day);
     return mktime($hour, $min, $sec, $mon, $day, $year, $is_dst);
-- 
2.39.5