What is the Groovy idiomatic way of asking for the last past weekday (ex. Monday)?
(or current week, the first day of the week)?
Also is there something similar for asking for the current month, the first of month?
static Date firstDayInWeek(Date day) {
day.clearTime()
return day - day.calendarDate.dayOfWeek
}
A year ago, I wrote a simple DSL for calculating dates, this allows you to do:
def mondayInJuly = new DateDSL().with {
first.monday.in.july( 2011 )
}
which sets mondayInJuly to be a Date object representing
Mon Jul 04 00:00:00 UTC 2011
I need to update the script to allow you to use Groovy 1.8's more lenient parser
As you mention DateTime in tags, I suggest JodaTime is OK. So, we do:
def lastMonday =
new DateTime().withDayOfWeek(DateTimeConstants.MONDAY).minusWeeks(1)
def firstThisMoth = new DateTime().withDayOfMonth(1)
Related
I'm trying to get accurate time with:
use chrono::{DateTime, Local, Utc};
use std::time::SystemTime;
fn main() {
println!(
"Local.now() {}",
Local::now().format("%H:%m:%S").to_string()
);
println!("Utc.now() {}", Utc::now().format("%H:%m:%S").to_string());
let system_time = SystemTime::now();
let stime: DateTime<Utc> = system_time.into();
println!("SystemTime.now() {}", stime.format("%H:%m:%S"));
}
However, if I run it:
$ date && target/debug/mybin
Sun Jan 15 04:08:19 PM CET 2023
Local.now() 16:01:19
Utc.now() 15:01:19
SystemTime.now() 15:01:19
I don't know where comes from the shift, but I want to know what's the correct way to get the right time?
The %m token inserts the current month's number, which is 1 because it is January. You probably want %M instead, which inserts the minute number. So you are correctly obtaining the current time, but are incorrectly displaying it by using the month number in the place where you'd expect to see the minute number.
See chrono's strftime documentation for a complete list of formatting codes.
So I have the following script:
import groovy.time.TimeCategory
def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
def date = new Date(messageExchange.getTimestamp())
use(groovy.time.TimeCategory){
after24Hours = (date + 24.hours).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC')) }
assert dueDate == after24Hours
What I'm trying to do with this is take the date and time from a REST request (dueDate - which comes in UTC format and with a 24h delay) and create a new date and time from the timestamp of the moment when that request has been sent, which is registered from my system. I then convert that time to UTC to accommodate the format from dueDate and add 24h to it. At the end I verify that the date and time from dueDate and after24Hours is the same.
The output does return the same time but in certain cases if there is a delay between the time the request is being sent and the time is received then the assertion will fail. This depends on the server, usually there is a difference of like 1 millisecond but I'm thinking that if the server will be slower at some point this will definitely be bigger.
What could I do to allow some margin of error in the assertion, maybe like a few seconds or even a couple of minutes?
Ok, so I managed to do this:
import groovy.time.*
def dueDate = context.expand( '${Test 4 - create user task#Response#$[\'_embedded\'][\'userTaskDtoList\'][0][\'dueDate\']}' )
def date = new Date(messageExchange.getTimestamp())
use(groovy.time.TimeCategory){
after24Hours = (date + 24.hours).format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))
def date1 = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", dueDate)
def date2 = Date.parse("yyyy-MM-dd'T'HH:mm:ss'Z'", after24Hours)
TimeDuration difference = TimeCategory.minus(date2, date1)
log.info date1
log.info date2
assert difference < 2.minutes
}
The script seems to work and it does return an error only if the time is longer than the one I've set in the assertion.
Unfortunately I have another issue now.
For some reason, my date output looks like this:
Fri Oct 01 16:24:10 EEST 2021: INFO: Sat Oct 02 13:24:10 EEST 2021
Which is not the correct format. That date should appear in the Zulu format, after all when I parsed the dates that was the format that I used.
Am I missing something?
What could I do to allow some margin of error in the assertion, maybe
like a few seconds or even a couple of minutes?
Instead of asserting that they are equal, you could assert that the difference between them is less than a threshold that you get to define.
If you use something like AssertJ, and I'd recommend you do, then you can do something like the following:
assertThat(dueDate).isCloseTo(after24Hours, within(1, ChronoUnit.MINUTE));
This will give a small margin to the comparison of the dates, and should fix your issue.
Currently I like to calculate the total time taken for my soap ui automation project using groovy. I tried the following approach but it doesn't work:
Date startTime= new Date()
Date EndTime= new Date()
But i unable to compare the dates since it is taking the data types as string "Sat May 18 23:54:29 IST 2019" and I am unable to find the difference.
In groovy you can use the TimeCategory utility to subtract your dates, and get a TimeDuration object representing the difference. From this object you can inspect all sort of structured time/duration information.
Also, if you have a date in a String representation you can parse it into a Date using Date.parse passing as a parameter the format of the string and the string representation itself.
The following is a working demo of all this:
import groovy.time.*
def startTimeString = "Sat May 18 00:00:00 IST 2019"
def startTime = Date.parse("E MMM dd H:m:s z yyyy", startTimeString)
def endTime = new Date()
use (TimeCategory) {
TimeDuration duration = endTime - startTime
println "[${startTimeString}] was [${duration}] ago"
}
Complete code on GitHub
Hope this helps.
I am working with Java 8 Date and Time utility. I am using the TemporalAdjuster interface and the implementations found in TemporalAdjusters to manipulate the calculations for a specific day or date.
My requirement is to calculate the day after 3 month and 2 days.
For example, today is 6th of Decemeber (i.e 2016-12-06 in YYYY-MM-DD) and after 3 months and 2 days, the date would be 8th of March, 2017 (i.e. 2017-03-08 in YYYY-MM-DD).
I tried two ways of doing this using the Date and Time utility as follows:
//First
LocalDate dayAfter3MonthsAnd2Days = LocalDate
.now()
.with(firstDayOfNextMonth()).plusMonths(2).plusDays(2);
//Second
LocalDate dayAfter3MonthsAnd2Days = LocalDate
.now()
.with(firstDayOfMonth()).plusMonths(3).plusDays(2);
Both of them returns the date as 3rd of March, 2017 (i.e. 2017-03-03) as follows:
dayAfter3MonthsAnd2Days = 2017-03-03
Is there any way i can customize the TemporalAdjusters and get the desired output? Or any other alternate to achieve the goal?
I found out that i can simply use the following:
LocalDate dayAfter3MonthsAnd2DaysNew = LocalDate
.now()
.plusMonths(3)
.plusDays(2);
Alongwith that, i can also use the custom TemporalAdjuster which uses the above same process to do manipulations as follows:
TemporalAdjuster ta = TemporalAdjusters.ofDateAdjuster(
(LocalDate d) -> d.plusMonths(3).plusDays(2));
LocalDate dayAfter3MonthsAnd2DaysCustom = LocalDate
.now()
.with(ta);
Although the previous way is simpler, but we can use more custom proceedings using ofDateAdjuster method of TemporalAdjuster.
I have a datepicker on my page, when I select any date it produced a result something like
Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)
And I need to format it: YYYY-MM-DDTHH:mm:ss Z
So, for this I use moment syntax
var date='Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)';
moment(date).format('YYYY-MM-DDTHH:mm:ss Z');
which produces an output
2014-09-07T00:00:00 +05:00
That's good, but my api expect standard timezone offset 'Z' instead of parsing into local current time zone (i.e +5:00) in my case.
So, I want to produce this
2014-09-07T00:00:00Z
How is it possible?
Use moment.utc() to display time in UTC time instead of local time:
var dateValue = moment(date).utc().format('YYYY-MM-DDTHH:mm:ss') + 'Z';
or moment().toISOString() to display a string in ISO format (format: YYYY-MM-DDTHH:mm:ss.sssZ, the timezone is always UTC):
var dateValue = moment(date).toISOString();
JSFiddle
Rather than using a hard-coded format string and then concatenating the 'Z' after it, it would be better to just use the toJSON() function which has been around since moment js v2.0. Like so:
moment('Sun Sep 07 2014 00:00:00 GMT+0500 (Pakistan Standard Time)').toJSON();
Here's the complete JSFiddle.