params
params copied to clipboard
Default value for time/dates?
Is there any way to specify a default value for a field of type time
? e..g Set ~T[16:30:00]
as a default value?
I tried:
[field: :time, default: ~T[16:30:00]]
But it throws an error: ** (CompileError) nofile: invalid quoted expression: ~T[16:30:00]
It should be possible to use the full syntax of the time in the default (below), the error was due to macro compilation issues.
%Time{
calendar: Calendar.ISO,
hour: 16,
microsecond: {0, 0},
minute: 30,
second: 0
}
I had a similar issue, I tried using %Time{}
and Time.new/3
directly but it came back with the same error. The error message may have improved since this was reported, because it also contains the solution:
Please make sure your quoted expressions are made of valid AST nodes. If you would like to introduce a value into the AST, such as a four-element tuple or a map, make sure to call Macro.escape/1 before
Just wrap the time inside of a Macro.escape/1
call and it will work:
[field: :time, default: Macro.escape(~T[16:30:00])]