Assertion of response body datetime in Postman - testing

I’m new to writing Tests in Postman and below is what I’m not able to get through after spending couple of days researching. I’ll appreciate the expertise:
Following steps I have done so far. From POST request response I parse it to grab Epoch time and then convert that into human readable date time.
Response Body:
1604448930
Tue Nov 03 2020 18:15:30 GMT-0600 (Central Standard Time)
Below is what I have in Tests script:
jsonData = JSON.parse(responseBody)
x = jsonData[0].ScheduledAttempt.ScheduledDateTime;
epochScheduledDateTime = jsonData[0].ScheduledAttempt.ScheduledDateTime
console.log(epochScheduledDateTime)
Date = new Date ((epochScheduledDateTime) *1000);
console.log(Date)
Now how can I compare my human readable date to the expected min and max datetime.
For eg: Tue Nov 03 2020 18:15:30 GMT-0600 (Central Standard Time) is between
Tue Nov 03 2020 18:00:00 GMT-0600 (Central Standard Time) and
Tue Nov 03 2020 20:00:00 GMT-0600 (Central Standard Time).
Appreciate your guidance.
Thank You
Kp

you don't have to convert the epoch time to dateString, instead you can convert other datestrings and the epoch time to millisecond using the getTime() method of the Date.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime
Return Value: A Number, representing the number of milliseconds since
midnight January 1, 1970
Now you can use the chai expect method "within" to validate if the value is within the range specified
pm.test("Validate date to be within the expected date range", function () {
epochScheduledDateTime = 1604448930
valueGot = 1604448930 * 1000
pm.expect(valueGot).to.be.within((new Date("Tue Nov 03 2020 18:00:00 GMT-0600 (Central Standard Time)")).getTime(), (new Date("Tue Nov 03 2020 20:00:00 GMT-0600 (Central Standard Time)")).getTime());
})
Or compare dates:
pm.test("Validate date to be within the expected date range", function () {
pm.expect(new Date((epochScheduledDateTime) * 1000)).to.be.within((new Date("Tue Nov 2 2020 18:00:00 GMT-0600 (Central Standard Time)")), (new Date("Tue Nov 03 2020 20:00:00 GMT-0600 (Central Standard Time)")));
})
postman expect class is from chai and all chai bdd assertions are supported:
https://www.chaijs.com/api/bdd/

Related

Convert Time and Date in string to date VB.net

I'm using RSS feeds from multiple sources and I'm sorting the feeds by date. However, the dates I receive are in different time zones.
This is the format of some of the date strings that I get:
Wed 08 Dec 2021 01:40:31 -0500
Wed 08 Dec 2021 11:11:19 -0600
The "-500' indicates the time zone which is UTC-5. I need to use that "-0500" to convert this string into a date and the date needs to be only in UTC.
Basically from "Wed 08 Dec 2021 01:40:31 -0500" to "12/08/2021 06:40:31"
I've managed to split the string up to sort by date but because of the time difference, it doesn't really help.
Is there coding which I can use to convert the string as-is into date and only UTC time? Or is there just a place where I can start?
Thank you in advance.
Using DateTime.ParseExact, you specify a format to convert from, and that can include "zzz" for the timezone offset. You can also specify that you want the result in UTC:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTime.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing, Globalization.DateTimeStyles.AdjustToUniversal)
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm:ss"))
Console.WriteLine(d.Kind.ToString())
Outputs:
2021-12-08 06:40:31
Utc
Of course, format the result as you desire.
Alternatively, if you need to, you can keep the offset by using a DateTimeOffset structure and adjust it to UTC for representation as a string:
Dim s = "Wed 08 Dec 2021 01:40:31 -0500"
Dim d = DateTimeOffset.ParseExact(s, "ddd dd MMM yyyy HH:mm:ss zzz", Nothing)
Console.WriteLine(d.ToUniversalTime.ToString("yyyy-MM-dd HH:mm:ss"))
Outputs:
2021-12-08 06:40:31

How do you convert timestamp with GMT timezone in Redshift?

How do you convert a string with DOW and GMT information to timestamptz in Redshift?
Example: Mon Apr 01 2019 14:08:20 GMT-0400 (EDT)
Amazon Redshift is based on PostgreSQL. Therefore, with the use of TO_DATE function, along with timezone related clauses, we could try the following.
SELECT
TO_TIMESTAMP('Mon Apr 01 2019 14:08:20 GMT-0400 (EDT)', 'XXX Mon DD YYYY HH:MI:SS')
AT TIME ZONE REGEXP_REPLACE('Mon Apr 01 2019 14:08:20 GMT-0400 (EDT)', '^.*(...).$', '\\1');
This results in the following.
Unfortunately, it doesn't seem like we can do this conversion in one go because time zone related format flags are valid for TIMESTAMPTZ only.

How to covert date format in react native?

I am woking on react native project want convert date as below
2020-04-12 06:02:00
To
Sun Apr 12 2020 06:02:00 GMT+0530 (IST)
I tried with moment as below
let momentObj = moment(resultFltLeg.estArrTime)
let showDate = moment(momentObj).format("EEE MMM dd yyyy HH:mm:ss 'GMT'z")
but getting below result
777 Apr Su 2020 06:02:00 'G4T'
Please suggest way to convert date in required format.
2.
After conversion i want to assign this date to other date with same format as below
myFltLeg.estArrTime = New Date(resultFltLeg.estArrTime);
Please let me know if is this correct way of assign one date to other date
myFltLeg.estArrTime // Mon Oct 12 2020 16:42:00 GMT+0530 (IST)
Try this :
const form = someday.format("ddd MMM D YYYY, hh:mm:ss ")
this will give you everything until the GMT - for that I think you need moment-timezone because the option to append z or zz seems deprecated.

Timezone Rails Postgres

How to select datetime column with timezone using ActiveRecord::Base.connection.execute()?
e.g
User.first.joined_at => Tue, 31 Jul 2018 05:00:34 MSK +03:00
but
ActiveRecord::Base.connection.execute("SELECT joined_at FROM users") => {"joined_at"=>"2018-07-31 02:00:34.684659"}
ActiveRecord::Base.connection.execute doesn't understand the database's types that well so you have to parse the strings yourself.
That timestamp should be in UTC as that's the Rails standard inside the database. You could go through Time.zone:
Time.zone = 'UTC' # Unless your application is already in UTC
t = Time.zone.parse('2018-07-31 02:00:34.684659').in_time_zone('Moscow')
# Tue, 31 Jul 2018 05:00:34 MSK +03:00
Or you could go through DateTime (which assumes UTC if the string doesn't have a timezone embedded in it):
DateTime.parse('2018-07-31 02:00:34.684659').in_time_zone('Moscow')
# Tue, 31 Jul 2018 05:00:34 MSK +03:00

Rails cookies with wrong expiring date

recently I've trying to set up a cookie in order to validate a session in a Rails 3.2 app.
The cookie was supposed to expire at the beginning of the next day, this way I would force users to login again (mandatory)
First I changed my app's timezone to the one I was expecting to handle:
config.time_zone = 'Caracas'
After that I created the cookie as follows:
cookies[:remember_token] = {value: user.remember_token,
expires: 1.day.from_now.beginning_of_day}
The cookie gets created without any problem but the expire date is wrong. In Chromium the date is set to today a 19:30 PM (Although my machine timezone is set accordingly to the app).
The strange thing comes when I change the definition of the cookie to:
cookies[:remember_token] = {value: user.remember_token,
expires: 1.day.from_now}
If I set to cookie only to 1 day from today then the expire date is set properly to exactly 24 hours from now.
Any ideas why beginning_of_date is not setting the date correctly?
Thanks in advance
It is down to the order of execution. The time zone is applied after the beginning of day is calculated. E.g. I am in UTC timezone, and I can do the following in rails console:
irb(main):019:0> 1.day.from_now.in_time_zone(Time.zone).beginning_of_day
=> Thu, 01 Nov 2012 00:00:00 UTC +00:00
irb(main):020:0> 1.day.from_now.beginning_of_day.in_time_zone(Time.zone)
=> Thu, 01 Nov 2012 00:00:00 UTC +00:00
irb(main):021:0> Time.zone.now.tomorrow.beginning_of_day
=> Thu, 01 Nov 2012 00:00:00 UTC +00:00
irb(main):022:0> 1.day.from_now.beginning_of_day.in_time_zone('Caracas')
=> Wed, 31 Oct 2012 19:30:00 VET -04:30
irb(main):023:0> 1.day.from_now.in_time_zone('Caracas').beginning_of_day
=> Thu, 01 Nov 2012 00:00:00 VET -04:30
So if you change your cookie calculation to the following it should work:
cookies[:remember_token] = {value: user.remember_token,
expires: 1.day.from_now.in_time_zone(Time.zone).beginning_of_day}