Weird behaviour of my code

I'm trying to manipulate some datenum data and I encountered a weird thing.
Whenever I apply this AFL code (attached below), it gives correct result on interpretation window for just it's very first exectuion. Afterwards it'll just display "{EMPTY}". I just can't figure out what I'm missing.
Please help me out.
Thank You.

/*Functions*/
	function toDateNum(pDate, pMonth, pYear) {
		local pDate, pMonth, pYear;
		return 1e4*(pYear-1900) + (100*pMonth) + pDate;
	}
	function ShortMonthStrToNum(_month) {
		local _month, result;
		_month = StrToUpper(_month);
		
		if(_month == "JAN")			{ result = 1; }
		else if(_month == "FEB")	{ result = 2; }
		else if(_month == "MAR")	{ result = 3; }
		else if(_month == "APR")	{ result = 4; }
		else if(_month == "MAY")	{ result = 5; }
		else if(_month == "JUN")	{ result = 6; }
		else if(_month == "JUL")	{ result = 7; }
		else if(_month == "AUG")	{ result = 8; }
		else if(_month == "SEP")	{ result = 9; }
		else if(_month == "OCT")	{ result = 10; }
		else if(_month == "NOV")	{ result = 11; }
		else if(_month == "DEC")	{ result = 12; }
		else						{ result = -1; }
		
		return result;
	}
	function getYearFromDateNum(pDateNum) {
		local pDateNum, tmp;
		tmp = int(pDateNum/1e4);
		pDateNum = IIf(tmp >= 100, 2000+tmp-100, 1900+tmp);
		return pDateNum;
	}
	function AdvDateStrToNumFormat(pDateStr, format) {
		local pDateNum, format, _date, _month, _year, result;
		result = Null;
		
		if(format == "DD-MM-YYYY") {
			_date = StrToNum(StrExtract(pDateStr, 0, '-'));
			_month = StrToNum(StrExtract(pDateStr, 1, '-'));
			_year = StrToNum(StrExtract(pDateStr, 2, '-'));
			
			result = toDateNum(_date, _month, _year);
		}
		else if(format == "DD-Mmm-YYYY") {
			_date = StrToNum(StrExtract(pDateStr, 0, '-'));
			_month = ShortMonthStrToNum(StrExtract(pDateStr, 1, '-'));
			_year = 2000+StrToNum(StrExtract(pDateStr, 2, '-'));
			
			result = toDateNum(_date, _month, _year);
		}
		else if(format == "DDMMMYY") {
			_date = StrToNum(StrLeft(pDateStr, 2));
			_month = StrLeft(pDateStr, 5);
			_month = StrReplace(_month, StrLeft(_month, 2), "");
			_month = ShortMonthStrToNum(_month);
			_year = 2000+StrToNum(StrRight(pDateStr, 2));
			
			result = toDateNum(_date, _month, _year);
		}
		else if(format == "YYYY-MM-DD") {
			_date = StrToNum(StrExtract(pDateStr, 2, '-'));
			_month = StrToNum(StrExtract(pDateStr, 1, '-'));
			_year = StrToNum(StrExtract(pDateStr, 0, '-'));
			
			printf("| %g | %g | %g |\n", _date, _month, _year);
			result = toDateNum(_date, _month, _year);
		}
		
		return result;
	}
/////////////

/*Main*/
	tmp = AdvDateStrToNumFormat("2022-12-30", "YYYY-MM-DD");

	printf("| %.15g |\n", tmp);
	printf("| %.7g |\n", getYearFromDateNum(tmp));
	
	// This
	printf("| %s |\n", NumToStr(getYearFromDateNum(tmp), 1.0, False));
////////

You failed to provide all details, including the version number of the software.

On my end, using AmiBroker 6.40 the formula works just fine and produces this output and it does NOT change in subsequent executions.

| 30 | 12 | 2022 |
| 1221230 |
| 2022 |
| 2022 |

Note that interpretation displays text for ACTIVE pane. Maybe you just clicked somewhre else.

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.