Cycle does not include steps to access and work with Date and Time.
Cycle now includes steps to execute simple Groovy statements as well as execute complex Groovy scripts.
The 2 Cycle steps that execute Groovy are:
I execute Groovy "<GROOVY_STATEMENTS>"
I execute Groovy script "<GROOVY_SCRIPT_FILE>"
This article assumes conceptual familiarity with Groovy/Java including general syntax, data types, import statements, class references and method usage.
For more information on Groovy see Apache Groovy
2 items of note regarding Cycle and Groovy.
These examples use classes methods from the improved Java 8+ Date/Time API which replaced deprecated Date and Calendar functionality.
@inline_date
Scenario: Date
Given I execute Groovy """import java.time.*; dateStr = LocalDate.now()format("MM/dd/yyyy")"""
Then I echo $dateStr
This example imports the java.time library and in a single statement gets the current date, formats it and converts it to a string for Cycle output. The format used in this example is MM/dd/yyyy but any syntactically valid format can be used.
A variation on this Scenario is to pass in the format from Cycle as a variable.
@inline_date_variable
Scenario: Date Variable
Given I assign "MM/dd/yyyy" to variable "format"
Then I execute Groovy "import java.time.*; dateStr = LocalDate.now()format(format)"
Then I echo $dateStr
@inline_date_time
Scenario: Date Time
Given I execute Groovy """import java.time.*; dateTimeStr = LocalDateTime.now()format("MM/dd/yyyy HH:mm:ss")"""
Then I echo $dateTimeStr
This example imports the java.time library and in a single statement gets the current date time, formats it and converts it to a string for Cycle output. The format used in this example is MM/dd/yyyy HH:mm:ss but any syntactically valid format can be used.
A variation on this Scenario is to pass in the format from Cycle as a variable.
@inline_date_time_variable
Scenario: Date Time
Given I assign "MM/dd/yyyy HH:mm:ss" to variable "format"
Given I execute Groovy """import java.time.*; dateTimeStr = LocalDateTime.now()format(format)"""
Then I echo $dateTimeStr
The need may arise to not only access and use dates but also manipulate specified dates by creating ranges, date addition/subtraction or reformatting. Since these require multiple steps we will be using Groovy scripts to perform the actions.
In the next example Scenario we will be passing our known date as a variable and the user defined input format into our Groovy script. We will be returning the calculated dates based our user defined days variable in the user defined output format and echoing in the Cycle Output for validation. Once they are returned to Cycle they can be used however desired.
@script_date
Scenario:Groovy script date
Given I assign "04/30/2019" to variable "dateStr"
And I assign "MM/dd/yyyy" to variable "format"
And I assign "dd-MM-yyyy" to variable "outFormat"
And I assign 7 to variable "days"
When I execute Groovy script "scripts\date_script.groovy" within 8 seconds
Then I echo $plusWeekStr
Then I echo $minusWeekStr
The following date_script.groovy file is what gets executed in the Scenario. We import the necessary libraries to perform the functions needed. As previously mentioned Cycle requires strings and numbers so the next step is to convert the passed in string to a date data type to be manipulated in Groovy. We then add the number of days based on the days variable assigned in Cycle to create a new date variable. Similarly we create another date variable representing the passed in date minus the number of passed in days. The final step is to convert the new date variables into strings to be used as Cycle variables.
import java.text.DateFormat
import java.time.format.DateTimeFormatter
import java.time.*
//Convert string to date
strToDate = LocalDate.parse(dateStr,format)
//Add days value to the date
plusWeek = strToDate + days
//Minus days value from the date
minusWeek = strToDate - days
//Convert dates to strings for Cycle output
plusWeekStr = plusWeek.format(outFormat)
minusWeekStr = minusWeek.format(outFormat)
In the next example Scenario we will be passing our known date time as a variable and the user defined input format into our Groovy script. We will be returning the calculated date times based our user defined hours or seconds variable in the user defined output format and echoing in the Cycle Output for validation. Once they are returned to Cycle they can be used however desired.
@script_date_time
Scenario:Groovy script date time
Given I assign "04/30/2019 23:21:45" to variable "dateStr"
And I assign "MM/dd/yyyy HH:mm:ss" to variable "format"
And I assign "yyyy-MM-dd HH:mm:ss" to variable "outFormat"
And I assign 2 to variable "hours"
And I assign 45 to variable "seconds"
Then I execute Groovy script "scripts\date_time_script.groovy" within 8 seconds
Then I echo $plusHourStr
Then I echo $minusSecondsStr
The following date_time_script.groovy file is what gets executed in the Scenario. We import the necessary libraries to perform the functions needed. As previously mentioned Cycle requires strings and numbers so the next step is to convert the passed in string to a date data type to be manipulated in Groovy. We then add the number of hours based on the hours variable assigned in Cycle to create a new date variable. Similarly we create another date variable representing the passed in date time minus the number of passed in seconds. The final step is to convert the new date variables into strings to be used as Cycle variables.
import java.text.DateFormat
import java.time.format.DateTimeFormatter
import java.time.*
//Convert string to date
strToDate = LocalDateTime.parse(dateStr,format)
//Add hours value to the date time
plusHour = strToDate.plusHours(hours)
//Minus seconds value from the date time
minusSeconds = strToDate.minusSeconds(seconds)
//Convert date back to string for Cycle output
plusHourStr = plusHour.format(outFormat)
minusSecondsStr = minusSeconds.format(outFormat)