﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
3002	Bug - _DateAdd('s', 1, '1970/01/01')  Returns '1970/01/01'	mLipok		"Repro:


{{{
#include <Date.au3>

ConsoleWrite(@CRLF)
; positive results
_DateAdd_TEST('1970/01/01 00:00:00')
; negative results
_DateAdd_TEST('1970/01/01')
; positive results
_DateAdd_TEST_2('1970/01/01')

Func _DateAdd_TEST($sDate)
	$sDate = _DateAdd('s', 1, $sDate)
	ConsoleWrite('@error  = ' & @error & @CRLF)
	ConsoleWrite($sDate & @CRLF)
	ConsoleWrite(@CRLF)
EndFunc   ;==>_DateAdd_TEST

Func _DateAdd_TEST_2($sDate)
	If StringRegExpReplace($sDate, '(\d{4}\/\d{2}\/\d{2})', '') = '' Then $sDate &= ' 00:00:00'
	$sDate = _DateAdd('s', 1, $sDate)
	ConsoleWrite('@error  = ' & @error & @CRLF)
	ConsoleWrite($sDate & @CRLF)
	ConsoleWrite(@CRLF)
EndFunc   ;==>_DateAdd_TEST_2
}}}

solusion proposal:

new internal function:
{{{
Func __DateExpand(Byref $sDate)
	If StringRegExpReplace($sDate, '(\d{4}\/\d{2}\/\d{2})', '') = '' Then $sDate &= ' 00:00:00'
EndFunc   ;==>__DateExpand
}}}

and modyfication:

{{{
Func _DateAdd($sType, $iNumber, $sDate)
	Local $asTimePart[4]
	Local $asDatePart[4]
	Local $iJulianDate
	; Verify that $sType is Valid
	$sType = StringLeft($sType, 1)
	If StringInStr(""D,M,Y,w,h,n,s"", $sType) = 0 Or $sType = """" Then
		Return SetError(1, 0, 0)
	EndIf
	; Verify that Value to Add  is Valid
	If Not StringIsInt($iNumber) Then
		Return SetError(2, 0, 0)
	EndIf

	__DateExpand($sDate)

	; Verify If InputDate is valid
	If Not _DateIsValid($sDate) Then
		Return SetError(3, 0, 0)
	EndIf

	; split the date and time into arrays
	_DateTimeSplit($sDate, $asDatePart, $asTimePart)

	; ====================================================
	; adding days then get the julian date
	; add the number of day
	; and convert back to Gregorian
	If $sType = ""d"" Or $sType = ""w"" Then
		If $sType = ""w"" Then $iNumber = $iNumber * 7
		$iJulianDate = _DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $iNumber
		_DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
	EndIf
	; ====================================================
	; adding Months
	If $sType = ""m"" Then
		$asDatePart[2] = $asDatePart[2] + $iNumber
		; pos number of months
		While $asDatePart[2] > 12
			$asDatePart[2] = $asDatePart[2] - 12
			$asDatePart[1] = $asDatePart[1] + 1
		WEnd
		; Neg number of months
		While $asDatePart[2] < 1
			$asDatePart[2] = $asDatePart[2] + 12
			$asDatePart[1] = $asDatePart[1] - 1
		WEnd
	EndIf
	; ====================================================
	; adding Years
	If $sType = ""y"" Then
		$asDatePart[1] = $asDatePart[1] + $iNumber
	EndIf
	; ====================================================
	; adding Time value
	If $sType = ""h"" Or $sType = ""n"" Or $sType = ""s"" Then
		Local $iTimeVal = _TimeToTicks($asTimePart[1], $asTimePart[2], $asTimePart[3]) / 1000
		If $sType = ""h"" Then $iTimeVal = $iTimeVal + $iNumber * 3600
		If $sType = ""n"" Then $iTimeVal = $iTimeVal + $iNumber * 60
		If $sType = ""s"" Then $iTimeVal = $iTimeVal + $iNumber
		; calculated days to add
		Local $iDay2Add = Int($iTimeVal / (24 * 60 * 60))
		$iTimeVal = $iTimeVal - $iDay2Add * 24 * 60 * 60
		If $iTimeVal < 0 Then
			$iDay2Add = $iDay2Add - 1
			$iTimeVal = $iTimeVal + 24 * 60 * 60
		EndIf
		$iJulianDate = _DateToDayValue($asDatePart[1], $asDatePart[2], $asDatePart[3]) + $iDay2Add
		; calculate the julian back to date
		_DayValueToDate($iJulianDate, $asDatePart[1], $asDatePart[2], $asDatePart[3])
		; caluculate the new time
		_TicksToTime($iTimeVal * 1000, $asTimePart[1], $asTimePart[2], $asTimePart[3])
	EndIf
	; ====================================================
	; check if the Input day is Greater then the new month last day.
	; if so then change it to the last possible day in the month
	Local $iNumDays = _DaysInMonth($asDatePart[1])
	;
	If $iNumDays[$asDatePart[2]] < $asDatePart[3] Then $asDatePart[3] = $iNumDays[$asDatePart[2]]
	; ========================
	; Format the return date
	$sDate = $asDatePart[1] & '/' & StringRight(""0"" & $asDatePart[2], 2) & '/' & StringRight(""0"" & $asDatePart[3], 2)
	; add the time when specified in the input
	If $asTimePart[0] > 0 Then
		If $asTimePart[0] > 2 Then
			$sDate = $sDate & "" "" & StringRight(""0"" & $asTimePart[1], 2) & ':' & StringRight(""0"" & $asTimePart[2], 2) & ':' & StringRight(""0"" & $asTimePart[3], 2)
		Else
			$sDate = $sDate & "" "" & StringRight(""0"" & $asTimePart[1], 2) & ':' & StringRight(""0"" & $asTimePart[2], 2)
		EndIf
	EndIf
	;
	Return $sDate
EndFunc   ;==>_DateAdd
}}}
"	Bug	closed		Standard UDFs	3.3.13.19	None	No Bug		
