Date Format Fixture
Features:
- Format dates to timestamps (seconds/millis after epoch)
- Reformat dates/times to a different format
- Display timestamps as formatted dates
When the fixture is called without arguments, the date format is the system locale default short format and can be changed by using set date format followed by the desired format.
Input dates are expected to be in the default or specified input format.
To toggle between seconds/milliseconds after epoch, use timestamp has milliseconds followed by the desired boolean value (default: true)
Time zone can be set using set timezone and -if no argument is given- defaults to the system default time zone.
This fixture is intended to be used as a library. (See below)
Examples
Instantiate with system default date format and timezone
script | date format fixture | |||||
#Get a timestamp for a given date and convert it back to formatted dates | ||||||
set date format | dd-MM-yyyy | |||||
$stamp<-[1584316800000] | timestamp for date | 16-03-2020 | ||||
show | format timestamp | $stamp->[1584316800000] | 16-03-2020 | |||
show | format timestamp | $stamp->[1584316800000] | as | yyyy/MM/dd HH:mm:ss | 2020/03/16 00:00:00 | |
check | format timestamp | $stamp->[1584316800000] | as | dd-MM-yyyy | 16-03-2020 |
Instantiate the fixture with a given format and time zone
One could also specify only one constructor parameter, in which case the timezone remains the system's default.
script | date format fixture | dd-MM-yyyy HH:mm:ss | GMT+1 | |||
timestamp has milliseconds | false | |||||
show | format date | 10-05-2001 12:13:14 | as | yyyy/MM/dd HH:mm | 2001/05/10 12:13 | |
$stamp<-[989493194] | timestamp for date | 10-05-2001 12:13:14 | ||||
show | format timestamp | $stamp->[989493194] | 10-05-2001 12:13:14 | |||
check | format timestamp | $stamp->[989493194] | as | dd-MM-yyyy HH:mm:ss | 10-05-2001 12:13:14 | |
#Change the time zone, adding one hour to the expected outcome | ||||||
set timezone | GMT+2 | |||||
check | format timestamp | $stamp->[989493194] | as | dd-MM-yyyy HH:mm:ss z | 10-05-2001 13:13:14 GMT+02:00 | |
#Change input format and convert to a different format | ||||||
set date format | MM/dd/yyyy | |||||
check | format date | 11/14/2016 | as | dd-MM-yyyy | 14-11-2016 |
Use as a library fixture from any other fixture table
When we use this fixture as a library we can call date format methods from any script table.
By default a library will use system default date format and timezone.
library |
date format fixture |
Example usage of date formatting methods from a string fixture script table:
script | string fixture | |||||
$stamp<-[1584316800000] | timestamp for date | 16-03-2020 | as | dd-MM-yyyy | ||
show | format timestamp | $stamp->[1584316800000] | 3/16/20 | |||
check | format timestamp | $stamp->[1584316800000] | as | dd-MM-yyyy | 16-03-2020 |
But we can also configure the value used for format (leaving time zone alone).
library | |
date format fixture | dd-MM-yyyy |
Now the calls to the formatting methods behave differently:
script | string fixture | ||
check | format timestamp | $stamp->[1584316800000] | 16-03-2020 |
But we can also configure the values used for both format and time zone.
library | ||
date format fixture | dd-MM-yyyy | GMT+1 |
Now the calls to the formatting methods behave differently again (the date might change depending on the default system locale used to generate the timestamp):
script | string fixture | ||||
show | format timestamp | $stamp->[1584316800000] | as | yyyy/MM/dd HH:mm:ss | 2020/03/16 01:00:00 |
show | format timestamp | $stamp->[1584316800000] | 16-03-2020 |
It's also possible to use an locale that is not your default locale:
library | |||
date format fixture | EEE, MMM dd yyyy HH:mm:ss z | UTC | en_US |
Now we can also convert localized date strings:
script | string fixture | |||||
check | format date | Thu, Dec 14 2017 11:50:00 UTC | as | dd-MM-yyyy HH:mm:ss | 14-12-2017 11:50:00 | |
show | format timestamp | $stamp->[1584316800000] | Mon, Mar 16 2020 00:00:00 UTC |