Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Ola2 Absent Member.
Absent Member.
428 views

How can I convert a date and time string grabbed from an xml and compare it with current time?

Hello I'm quite new at BDL scripting so please be gentle :) 

I have a problem converting  a  timestamp that I grab from an xml and convert it into let's say second since midnight

My method of grabbing the xml value is:

WebXmlParseNodeValue(sXmlParseRs1Rt1Tt1,
STRING_COMPLETE,
ToEncoding("/Results[1]/Result[1]/TestTime[1]"),
0,
NULL,
1,
ToEncoding("text/xml; charset=utf-8")
);

the above code generates a date and time format like this:  2018-11-14T11:13:52.3282097+01:00

I then pick out the date and time in separate variables
StrRegexImmediate( sXmlParseRs1Rt1Tt1,"[0-9]+-[0-9]+-[0-9]+","&", sXmlParseRs1Rt1Tt1b );
StrRegexImmediate( sXmlParseRs1Rt1Tt1,"[0-9]+:[0-9]+","&", sXmlParseRs1Rt1Tt1c );

 

I can verify that I've grabbed the correct values with 
RepMessage ("formatted Date sXmlParseRs1Rt1Tt1b: " +sXmlParseRs1Rt1Tt1b, SEVERITY_INFORMATIONAL); //this output = 2018-11-14
RepMessage ("formated ToD sXmlParseRs1Rt1Tt1c: " +sXmlParseRs1Rt1Tt1c, SEVERITY_INFORMATIONAL); //this output =11:13

 

I have also declared nTime variable
nTime := GetCurrentTime();  //this generates current time in seconds since midnight

 

So after this intro, my question is; how do I convert the HH:MM-string of sXmlParseRs1Rt1Tt1c into a number variable that I can use to compare it with the value of nTime?

 

[magic code I need help with that converts sXmlParseRs1Rt1Tt1c into seconds from midnight and sets a new variable of nPleaseMakeItWork  ]

 

Then is should just be a matter of 

 

if (nTime - nPleaseMakeItWork  > 600)
then
   Repmessage ("Too old ",SEVERITY_ERROR);
else
   Repmessage ("We are good ",SEVERITY_INFORMATIONAL);
end;

 

Looking forward to your replies

0 Likes
3 Replies
Micro Focus Frequent Contributor
Micro Focus Frequent Contributor

RE: How can I convert a date and time string grabbed from an xml and compare it with current time?

JConvertDatetoTS.zip

Hi Ola,

I have attached a Silk Performer project which uses Java to convert time strings to timestamps.

You should be able to adapt this project to suit your requirements.

Best Regards,

Neil

0 Likes
Ola2 Absent Member.
Absent Member.

RE: How can I convert a date and time string grabbed from an xml and compare it with current time?

Thank you for your reply, that is the same project I got from BMC support.
I'm reluctant to use java as I know that language even less than BDL.

So I came up with this solution.. not a complete script, just the important bits
I'm sure there are more efficient ways of coding it but I'm a beginner so I'm happy that I have a working solution

//From this xml (heaviliy edited for privacy reasons)
<Results>
<Result>
<TestTime>2018-12-05T06:54:45.2528664+01:00</TestTime>
<Ok>true</Ok>
</Result>


//I grab the timestamp from xml
WebXmlParseNodeValue(sTimeFromXml,
STRING_COMPLETE,
ToEncoding("/Results[1]/Result[1]/TestTime[1]"),
0,
NULL,
1,
ToEncoding("text/xml; charset=utf-8")
);
//sTimeFromXml is now "2018-12-05T06:54:45.2528664+01:00"

//Then I grab hours, minutes and seconds to separate variables and convert them from string to number
StrRegexImmediate( sTimeFromXml,"[0-9]+:[0-9]+:[0-9]+","&", sTimeFromXmlb ); //sTimeFromXmlb := 06:54:45

Substr( sTimeFromXmlb, sHourFromXml,1,2 ); //sHourFromXml := 06
Substr( sTimeFromXmlb, sMinuterFromXml,4,2 ); //sMinuterFromXml := 54
Substr( sTimeFromXmlb, sXmlSeconds,7,2 ); //sXmlSeconds := 45

nXmlHourInSeconds := number( sHourFromXml ); //populates a numeric variable with a string value
nXmlHourInSeconds := ( nXmlHourInSeconds*60*60 ); // nXmlHourInSeconds := 21600

nXmlMinuteInSeconds := number( sMinuterFromXml); //populates a numeric variable with a string value
nXmlMinuteInSeconds := ( nXmlMinuteInSeconds*60 ); // nXmlMinuteInSeconds := 3240

nXmlSeconds := number( sXmlSeconds); //nXmlSeconds := 45

//Adding all the seconds from midnight
nXmlSecSinceMidnight := ( nXmlHourInSeconds + nXmlMinuteInSeconds + nXmlSeconds); //nXmlSecSinceMidnight := 24885

//getting the current time in seconds
nTime := GetCurrentTime(); // nTime := 24900 (06:55:00)

//Comparing the two variables. In this case we allow 600 seconds (10 minutes) difference
if ( nTime-nXmlSecSinceMidnight > 600 )then
RepMessage ("Too old",SEVERITY_ERROR);
else
RepMessage ("Good to go",SEVERITY_INFORMATIONAL);
end;

0 Likes
Highlighted
Jonny Absent Member.
Absent Member.

RE: How can I convert a date and time string grabbed from an xml and compare it with current time?

HI

Does your code work if the two times you compare happen to cross a day? If the first time is before midnight and the current time is after midnight?
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.