#
#  PRE: if
#
uint32 test_integer
uint32 result_integer
string result_string1
string result_string2
date test_date

# Use pre-defined date and time
test_integer := 1506101100

# Convert to string representation
result_string1 := %date(%{test_integer})

# Some systems report GMT some UTC...
if (!(result_string1 == "Fri 22 Sep 17:25:00 GMT 2017")) && (!(result_string1 == "Fri 22 Sep 17:25:00 UTC 2017")) {
	test_fail
}

# Convert string to integer
result_integer := %date(%{result_string1})


if (!(result_integer == test_integer)) {
	test_fail
}

# Compare two methods of reading request timestamp in local timezone
result_string1 := %localdate(request)
result_string2 := %S

if (!(result_string1 == result_string2)) {
	test_fail
}

# Convert different string format
result_string1 := "2017-09-22 17:25:00"

result_integer := %sqldate(%{result_string1})

if (!(result_integer == test_integer)) {
	test_fail
}

# Use a date attribute
test_date := 1659985459
result_string1 := %sqldate(%{test_date})

if !(result_string1 == '2022-08-08 19:04:19') {
	test_fail
}

# Invalid format
result_string1 := '201-32-22 17:25:00'
result_string2 := %sqldate(%{result_string1})

# This shouldn't exist, as the RHS above is NULL, and therefore the assignment will fail
if (result_string2) {
	test_fail
}

if (!(Module-Failure-Message == "Failed to parse time string \"201-32-22 17:25:00\" as format '\%Y-\%m-\%d \%H:\%M:\%S'")) {
	test_fail
}

# Invalid type
NAS-IP-Address := "192.168.1.1"
result_string2 := %date(%{NAS-IP-Address})

if (result_string2) {
	test_fail
}

if (!(Module-Failure-Message == "Can't convert type ipaddr into date")) {
	test_fail
}

#
#  Do date comparisons
#
if (test_date != (date) 'Aug  8 2022 19:04:19 UTC') {
   test_fail
}

if (test_date < (date) 'Aug  8 2022 18:00:00 UTC') {
   test_fail
}

if (test_date > (date) 'Aug 9 2022 00:04:19 UTC') {
   test_fail
}


success
