1 Date And Time Functions - essenius/FitNesseFitSharpSupportFunctions GitHub Wiki
FitNesse has a widget that allows you to insert today's date/time on test pages, and do simple math with it:
Today is !today
Yesterday was !today -1
Tomorrow is !today +1
Now is !today -t
Today in custom format is !today (yyyy-MM-dd HH:mm:ss.SSS)
This is what you see when you run it:
However, there are a few limitations. One is that you can only add/subtract integers - so e.g. you can't go a few hours back or forth. Another one is that you cannot use a custom format for a calculated date. Here are two lines showing the problem.
12 hours from now is !today +0.5
Yesterday in custom format is !today -1 (yyyy-MM-dd HH:mm:ss.SSS)
The result isn't what we would expect. FitNesse calculates !today +0
(which is simply !today
) and concatenates .5
to it. And it doesn't use the format in the second example.
To work around this, I made a few simple fixture functions which I included in the SupportFunctions
assembly. Here is an example of how to use it:
|script |common functions |
|$yesterday= |Add |-1 |days to |today |
|show |echo |$yesterday |
|$ninetyMinutesAgo=|Add |-1.5 |Hours to |now |
|show |Date |$ninetyMinutesAgo|formatted|HH:mm:ss.fff |
|$YesterdayNoon= |Add |12 |hours to |$yesterday |
|$nextSolarEclipse=|Parse|21 Aug 2017, 17:46 |
|check |echo |$nextSolarEclipse|2017-08-21T17:46:00 |
|check |Date |$nextSolarEclipse|formatted|dd-MMM-yyyy|21-Aug-2017|
As you can see, you can use float (or, more precisely, double) values to add a certain number of days or hours to a date. This can be a parseable string representation of a date, the current date (Today
) or current date/time (Now
). These tokens are not case sensitive. Other options are today in UTC (UtcToday
) and current date/time in UTC (UtcNow
). You can display any date in any format. You can also create symbols containing specific dates, and create dates relative to those as well. If you assign a date to a variable, the default way of displaying is the sortable format.
Tick Values
The SupportFunctions
fixture has another feature called Ticks, which represent the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (current time zone) in the Gregorian calendar. They are represented as long integer values (64 bit).
This is how you use the functionality in FitNesse:
|script |common functions |
|$tickdate=|Parse |621247104000000000 |
|check |echo |$tickdate|1969-08-28T00:00:00|
|$ticks1= |Ticks |
|$ticks2= |Ticks |
|check |ticks between|$ticks1 |and |$ticks2 |>0 |
|$now1= |parse |now |
|$now2= |parse |now |
|check |ticks between|$now1 |and |$now2 |>0 |
|check |to ticks |$tickdate|621247104000000000 |
|check |ticks since |$now2 |>0 |
The Parse
function command shows that if you pass a long integer into it, it will interpret it as a Ticks value, and will convert it into the corresponding date. The Ticks
function will get the Ticks value for the current moment. It will always return a unique value. Ticks Between And
calculates the difference in ticks between two date values - and you can use both dates and tick values as parameters. You use To Ticks
to convert a date value into the corresponding Ticks value. Finally, Ticks Since
gives the number of ticks between the date in the parameter and the current timestamp.
Here is what you should see when you run the test:
Note that there is a peculiarity in FitNesse when you use comparison operators in check - FitNesse converts the numbers to doubles before the comparison. So FitNesse may see two very large long integers with a very slight difference as identical, since the conversion to double reduces the precision. That is why there is also the Ticks Between And function - generally the difference between two dates results in much lower tick values, that do not suffer from precision loss when converted to doubles.
Date Format
It is possible to get or set the date format in use.
|script |common functions |
|set date format|M/d/yyyy h:mm:ss tt |
|$inputDate1= |Parse |12/3/2017 2:54:39 AM |
|$inputDate2= |Parse |2/2/2017 2:53 AM |
|check |ticks between|$inputDate1|and |$inputDate2|<0 |
|check |date |$inputDate2|formatted|dd-MMM-yyyy|02-Feb-2017|
|set date format|$format |
DateFormatFunctions