Tip - 'format' and time/date

I find that the “format” inspector can make for easier conversion of date/time fields into other formats. The format inspector substitutes parameters into a string. I rarely see ‘format’ used outside of some dashboard javascript but I think it could be useful in a lot of cases, for example in formatting dates and times.

https://developer.bigfix.com/relevance/reference/format.html

The ‘format’ inspector starts with a string, and substitutes in positional tags such as {0}, {1}, etc. with parameters specified after the string. Parameters are separated by the + character.

Example:

Q: format "At {1}, user {0} is logged on." + name of current user + now
A: At 9/27/2010 3:03:26 PM, user Scott is logged on.

This can be useful in formatting time and dates:

q: (format "{0}{1}{2}{3}{4}{5}" + year of item 1 of it as string + month of item 1 of it as two digits as string + day_of_month of item 1 of it as two digits as string + two digit hour of  item 0 of it + two digit minute of item 0 of it + two digit second of item 0 of it) of (time (universal time zone) of it, date (universal time zone) of it) of now
A: 20200220000618
T: 0.194 ms
I: singular format

Want to change the way it’s presented? Pretty easy, only the part inside the “format” string needs to be modified; the rest of the relevance is unchanged.

q: (format "{0}-{1}-{2}, {3}:{4}:{5}" + year of item 1 of it as string + month of item 1 of it as two digits as string + day_of_month of item 1 of it as two digits as string + two digit hour of  item 0 of it + two digit minute of item 0 of it + two digit second of item 0 of it) of (time (universal time zone) of it, date (universal time zone) of it) of now
A: 2020-02-20, 00:12:40
T: 0.220 ms
I: singular format

Change the order to put the time before date? Again, only the order inside the format string changes…the rest of the relevance is untouched

q: (format "{3}:{4}:{5} {0}-{1}-{2}" + year of item 1 of it as string + month of item 1 of it as two digits as string + day_of_month of item 1 of it as two digits as string + two digit hour of  item 0 of it + two digit minute of item 0 of it + two digit second of item 0 of it) of (time (universal time zone) of it, date (universal time zone) of it) of now
A: 00:13:37 2020-02-20
T: 0.206 ms
I: singular format

Which, if I understand the history, is exactly why the format inspector was added - for easier localization of strings such as date/time representations and currencies.

Related:

15 Likes

The format command’s requirement for curly braces presents a problem when it’s wrapped inside relevance for other things, such as parameters. The solution is to use ASCII substitution for those curly braces.

parameter “theYear” = “{format “%7B0%7D” + (year of it as string) of date (local time zone) of now}”

6 Likes

15 posts were split to a new topic: BESClient install date on Linux

Jason, thank you. Your examples and explanation have been top notch! When browsing the forum for help, your answers always alleviate my difficulties. I used your second to last example and switched it from universal to local time zone.

2 Likes

Thanks for the feedback! Always happy to help

1 Like