String to Date with convert - velocity

I would like a tip, please :
To convert a String to Date in velocity:
It is $convert.parseDate($currentMessage.date.begin)
I try too :
#set($str = $!currentMessage.date.begin)
$str
#set($dateTransforme = $date.toDate('yyyy-MM-dd', $date.date))
$dateTransforme
$dateTransforme.parseDate($str) <br />
N.B. $currentMessage.date.begin is a string.
I continue to have at the running :
$dateTransforme.parseDate($str)
Why? My string is in the format '2014-02-26'
Thanks,

Here is the working code with datetool
#set ($toDateRegDate= $dateTool.toDate('yyyy-MM-dd hh:mm:ss Z', $Video.Date.getData()))
#set ($videoDate = $dateTool.format('MMMM dd,yyyy', $toDateRegDate,$locale))
$videoDate

Finally, It's OK:
## tranformation of the data to dates : date.begin
#set($dateBegin = $date.toDate('yyyy-MM-dd',$!currentMessage.date.begin))
## tranformation of the data to dates : date.end
#set($dateEnd = $date.toDate('yyyy-MM-dd',$!currentMessage.date.end))
Ale

Convert a string to a date object
#set($dateObj = $date.toDate("dd/MM/yyyy", "08/09/2015"))
Format the date objet to "yyyy-MM-dd"
#set( $dateFormated = $date.format("yyyy-MM-dd", $dateObj))

Related

How do you compare selector attributes in Testcafe?

I'm trying to compare the date of videos on a webpage to today's date. If the difference between the two dates is more than X days, report back as false.
The videos on the webpage have a tag in them which uses the format yyyy-mm-dd
I've got a selector set up to find the videos const videoDate = Selector('OPTA-video').withAttribute('data-secondary-time')
Now how do I set a variable to today's date and compare the two? I'm completely stuck!
I was using Katalon Studio before and here's the groovy script that did the same job:
String videoDate = WebUI.getAttribute(findTestObject('OPTA-video'), 'data-secondary_time')
LocalDate todaysDate = LocalDate.now()
LocalDate videoDateParsed = LocalDate.parse(videoDate, dtf)
if (ChronoUnit.DAYS.between(videoDateParsed, todaysDate) > 1) {
KeywordUtil.markFailed('The videos are 2+ days old.')
} else {
KeywordUtil.logInfo('The videos are up to date.')
}
You can use the getAttribute TestCafe method to access an attribute value. Then, parse the attribute value into the JavaScript Date object:
String videoDate = Selector('OPTA-video').getAttribute('data-secondary-time');
Date videoDateParsed = Date.parse(videoDate);
Date todaysDate = Date.now()
...
In the following thread you can find how to compare Date objects.
This is one of the scripts that I am using.
//getting your XPath test value into a string
String ann_time =
WebUI.getText(findTestObject("ObjectRepository/navigateTOElement/announcements_date"))
//converting time to simple date format
SimpleDateFormat sdf = new SimpleDateFormat('HH:mm')
Date sdf_anntime = sdf.parse(new String(ann_time))
//getting Current time
SimpleDateFormat dateFormatGmt = new SimpleDateFormat('HH:mm')
dateFormatGmt.setTimeZone(TimeZone.getTimeZone('GMT'))
SimpleDateFormat dateFormatLocal = new SimpleDateFormat('HH:mm')
currDate = dateFormatLocal.parse(dateFormatGmt.format(new Date()))
// time gap in long format
long duration = currDate.getTime() - sdf_anntime.getTime()
//time gap to mins
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration)
//compare time gap with globale variable
if (diffInMinutes < GlobalVariable.News_updated_time) {
log.logInfo("system is getting updated,last updated "+ diffInMinutes + "min ago")
} else {
CustomKeywords.'errorMessage.logFailed.markStepFailed'('from 1 h, system was not updated')
log.logInfo('from '+ diffInMinutes+ 'h, system was not updated')
}

how to cast timestamp to get hour HH format in spark scala

I have a csv data file which contains a column of times in such format HH:MM:SS
I am trying to query the csv using spark-sql in order to get the most busy and less busy hours of entrances/exits .
can anyone help me solve this problem ? much appreciated !
here is a sample of my csv file :
emp_name,emp_badge,door_number,date_time,usage_type
Capucine Letellier,28161comp,5,22:36:27,ENTRANCE
Zoé Bonnin de la Lenoir,75976comp,5,01:08:49,ENTRANCE
Henri Potier,66586comp,4,03:13:16,ENTRANCE
Théodore Rodriguez,39004comp,3,20:55:11,ENTRANCE
Christine Bonneau de Rodrigues,23965comp,4,18:45:42,EXIT
You can use the hour(string date) function which returns the hour of the timestamp. Example: hour('2009-07-30 12:58:59') = 12, hour('12:58:59') = 12.
Next, you can calculate the busy hours and less busy hours like this:
import org.apache.spark.sql.expressions.Window
import org.apache.spark.sql.functions._
val rawData = spark.read.csv("data.csv")
// Busy Hours calculation
val windowSpecBusyHours = Window.partitionBy("_c4").orderBy(col("transactions").desc)
val busyHours = rawData
.withColumn("hours", hour(col("_c3")))
.groupBy("_c4", "hours").agg(count("*").alias("transactions"))
.withColumn("dense_rank", dense_rank().over(windowSpecBusyHours))
.select("_c4", "hours", "transactions").where(col("dense_rank") === 1)
busyHours.show(false)
// Less Busy Hours calculation
val windowSpecLessBusyHours = Window.partitionBy("_c4").orderBy(col("transactions").asc)
val lessBusyHours = rawData
.withColumn("hours", hour(col("_c3")))
.groupBy("_c4", "hours").agg(count("*").alias("transactions"))
.withColumn("dense_rank", dense_rank().over(windowSpecLessBusyHours))
.select("_c4", "hours", "transactions").where(col("dense_rank") === 1)
lessBusyHours.show(false)
If your csv contains
string like "HH:MM:ss" then:
val myCsv = spark.read.csv("path/to/csv")
//this one splits you string by : and takes the first part of it
val addHour = myCsv.withColumn("hour", split($"date_time", ":")(0))
timestamp format then:
val myCsv = spark.read.csv("path/to/csv")
// Cast it first to timestamp because csv doesn't keep column format, after that format it to HH
val addHour = myCsv.withColumn("hour", date_format($"date_time".cast("timestamp"), "HH"))
I hope this helped you.
Later edit:
To use $ operator for column you need to import spark implicits:
import spark.implicits._
Now you can use $ instead of col("column_name") function.

Powershell $cmd.ExecuteNonQuery() changes DATETIME to NVARCHAR

Caveat: I am new to PowerShell...
I am trying to automate a load process for 48 tables. The table names are all of the format "Vocabulary_MMddyyyy", as in "Vocabulary_12052016". I have no trouble iterating through the table names and parsing out the MMddyyyy part (the release version for the vocabulary). Powershell does not recognize the '12052016' as a proper datetime format. So, I do some parsing to build a proper date string that I convert to DateTime with the Get-Date cmdlet:
$version = ($table.Table_name -replace 'Vocabulary_', '').Trim();
$mm = $version.Substring(0,2);
$dd = $version.Substring(2,2);
$yyyy = $version.Substring(4,4);
$date = $mm + "/" + $dd +"/" + $yyyy
$startDate = Get-Date -Date $date -Format "d"
$endDate = ($startDate).AddDays(-1)
write-host 'Version: ' $version
write-host 'StartDate: ' $startDate
write-host 'EndDate: ' $endDate
This properly sets $startDate and $endDates (though despite the "d" format, I still get HH:MM:SS AM part):
Version: 12052016
StartDate: 12/5/2016 12:00:00 AM
EndDate: 12/4/2016 12:00:00 AM
This is the SQL signature of the stored procedure I want to call:
CREATE PROCEDURE [vocab].[prInsertLoadProcessID] (
#CodeQualifierID INT,
#SourceVersion VARCHAR(100),
#SourceDate DATE,
#LoadTypeID INT,
#isTestLoad BIT,
#ValidationDate DATE,
#LoadDescription VARCHAR(500),
#GlobalEffectiveDateStart DATE,
#GlobalEffectiveDateEnd DATE
)
Now, I go to build up a SQL command to execute the stored procedure ($conn2 is properly defined and open; it executes simple select statements yielding correct results):
$cmd.Connection = $conn2;
$cmd.CommandText = "vocab.prInsertLoadProcessID ";
$cmd.CommandType = [System.Data.CommandType]::StoredProcedure;
$cmd = New-Object System.Data.SqlClient.SqlCommand
$cmd.Connection = $conn2;
$cmd.CommandText = "vocab.prInsertLoadProcessID ";
$cmd.CommandType = [System.Data.CommandType]::StoredProcedure;
$cmd.Parameters.AddWithValue("#CodeQualifierID", 6);
$cmd.Parameters.AddWithValue("#SourceVersion", $version);
$cmd.Parameters.AddWithValue("#SourceDate", $startDate);
$cmd.Parameters.AddWithValue("#LoadTypeID", 1);
$cmd.Parameters.AddWithValue("#isTestLoad", 0);
$cmd.Parameters.AddWithValue("#ValidationDate", 'NULL');
$cmd.Parameters.AddWithValue("#LoadDescription", $LoadDescription);
$cmd.Parameters.AddWithValue("#GlobalEffectiveDateStart", $startDate);
$cmd.Parameters.AddWithValue("#GlobalEffectiveDateEnd", $endDate);
Each of the dates (#SourceDate, #GlobalEffectiveDateStart, #GlobalEffectiveDateEnd) show as DateTime when I look at the output from adding the parameters (here is one example):
CompareInfo : None
XmlSchemaCollectionDatabase :
XmlSchemaCollectionOwningSchema :
XmlSchemaCollectionName :
ForceColumnEncryption : False
**DbType : DateTime**
LocaleId : 0
**ParameterName : #GlobalEffectiveDateEnd**
Precision : 0
Scale : 0
**SqlDbType : DateTime**
SqlValue : 12/6/2015 12:00:00 AM
UdtTypeName :
TypeName :
Value : 12/6/2015 12:00:00 AM
Direction : Input
IsNullable : False
Offset : 0
Size : 0
SourceColumn :
SourceColumnNullMapping : False
SourceVersion : Current
When I run $cmd.ExecuteNonQuery() I get:
Exception calling "ExecuteNonQuery" with "0" argument(s): "Error converting data
type nvarchar to date." this is a Category: MethodInvocationException and a FullyQualifiedErroID: SQLException.
So, after reading for hours and trying everything I can think of I am stuck. My variables appear to be typed as DATETIME, but ExecuteNonQuery() seems to read treat them as NVARCHAR.
What am I missing? I can certainly rewrite the stored procedure to take strings instead of dates and let SQL do the conversion, but it really seems I should be able to pass dates from Powershell to SQL.
There must be a solution to this. What is causing the $cmd.ExecuteNonQuery() to change the datatype of my inputs? Or, am I setting up the $cmd.Parameters incorrectly?
Kind thanks for any help.

Kendo DateTimePicker: Need to return dates in UTC

In my MVC application I am storing dates in UTC. In my view I am doing this to show the dates in date time pickers in local time
<div class="datetimepicker">#(Html.Kendo().DateTimePickerFor(m => m.StartDateTime).Value(Model.StartDateTime.ToLocalTime()))</div>
The problem is that when the datetimes arereturned in the post methods back to the server they are returned in local time. Is there a way from the view to return the datetimes in UTC?
Hi datetimepicker in kendo i dono abt it,but just a thought if u can get the value from datetimepicker and pass through a function as below which converts to UTC and then send it to server , i believe ur task is complete..sry if i am wrong...
function convertToUtc(str) {
var date = new Date(str);
var year = date.getUTCFullYear();
var month = date.getUTCMonth()+1;
var dd = dategetUTCDate();
var hh = date.getUTCHours();
var mi = date.getUTCMinutes();
var sec = date.getUTCSeconds();
// 2010-11-12T13:14:15Z
theDate = year + "-" + (month [1] ? month : "0" + month [0]) + "-" +
(dd[1] ? dd : "0" + dd[0]);
theTime = (hh[1] ? hh : "0" + hh[0]) + ":" + (mi[1] ? mi : "0" + mi[0]);
return [ theDate, theTime ].join("T");
}
Your question is a good one and the approach that we suggest is covered in this code library article.

Retaining the last modified date while using ChannelSftp for file transfer

I am using ChannelSftp(jsch-0.1.42.jar) to copy files from a unix server and can successfully do it but the last modified date gets overwritten by the current time.
I am using
chanSftp.get(nextName, "C:/Test/" + nextName);
Is there any way of retaining the last modified time of the remote file on the local copy?
I have the last modified time of the remote file which i got using
attrs = lsEntry.getAttrs();
Date modDate = (Date) format.parse(attrs.getMtimeString());
Using a SimpleDateFormat is of less performance. Instead one could use the methods getMTime() and getATime() directly. But they deliver a value reduced by the milliseconds.
That's why they return an int and not a long as expected in conformance to date.getTime().
SftpATTRS attrs = lsEntry.getAttrs();
Date dateModify = new Date(attrs.getMTime() * 1000L);
Date dateAccess = new Date(attrs.getATime() * 1000L);
In jsch-0.1.50 be careful of using getAtimeString() there is the factor 1000L missing.
In jsch-0.1.51 the getAtimeString() bug with the missing factor is fixed.
This changes the last modified time of the file downloaded from the remote server,
String remoteFilePath = "testDir/testFile.txt";
SftpATTRS attrs = sftpChannel.lstat(remoteFilePath);
SimpleDateFormat format = new SimpleDateFormat(
"EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date modDate = (Date) format.parse(attrs.getMtimeString());
String localFilePath = "C:/temp/downloadedFile.txt";
sftpChannel.get(remoteFilePath, localFilePath);
File downloadedFile = new File(localFilePath);
downloadedFile.setLastModified(modDate.getTime());
Merging the answers above to a working solution:
sftpChannel.get(REMOTE_FILE, LOCAL_FILE);
SftpATTRS attrs = sftpChannel.lstat(REMOTE_FILE);
Date dateModify = new Date(attrs.getMTime() * 1000L);
File downloadedFile = new File(LOCAL_FILE);
downloadedFile.setLastModified(dateModify.getTime())