params icon indicating copy to clipboard operation
params copied to clipboard

Default value for time/dates?

Open brandonparsons opened this issue 7 years ago • 2 comments

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]

brandonparsons avatar Apr 14 '17 20:04 brandonparsons

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
}

vasspilka avatar Feb 09 '20 19:02 vasspilka

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])]

dimakula avatar Sep 15 '21 20:09 dimakula