Dataweave 2 - Cannot coerce String to DateTime - datetime

I have a String as DateTime like this 2019-05-21 00:00:00.000.
This is the code that I use in Dataweave 2 to transform String to DateTime:
SourceDate: payload.Source_date as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"}
But it returns this error:
"Cannot coerce String (2019-05-21 00:00:00.000) to DateTime, caused
by: Text '2019-05-21 00:00:00.000' could not be parsed at index 10
I need to use 'T' and Z to use the TimeZone automatically.
What could be the problem?

You can use LocalDateTime which will use the current timezone:
%dw 2.0
output application/json
---
SourceDate: payload.Source_date as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"}
And you can add the timezone:
SourceDate: payload.Source_date as LocalDateTime {format: "yyyy-MM-dd HH:mm:ss.SSS"} >> "GMT+1"

Related

Converting str into datetime and fetching all data from db

I tried to make a tasks.loop() for checking a muted user that needs to be unmuted, but there's a few problem while doing this, i can't use fetchall() for some reason because it will gives me this error
toremove = muteremove[2]
IndexError: list index out of range
If i use fetchone() maybe it only fetch 1 user every 10 secs, i mean how to fetch all the data every 10 sec to unmute a user?
Also if i use fetchone() it will say that it can't convert str into datetime.datetime object, how can i fix this?
#tasks.loop(seconds=10)
async def muted_user_check(self):
self.cur.execute(f"SELECT userId, guildId, expiredAt FROM mutedlist")
muteremove = self.cur.fetchall()
if muteremove is None:
print("No user to unmute :D")
if muteremove is not None:
toremove = muteremove[2]
timenow = datetime.utcnow()
if timenow > toremove:
self.cur.execute(f"DELETE FROM mutedlist WHERE guildId = {muteremove[1]} and userId = {muteremove[0]}")
To convert a string into a datetime object, you can use the strptime() method:
from datetime import datetime
def convert(date, format):
return datetime.strptime(date, format)
[input] convert('22/08/2020', '%d/%m/%Y')
[output] 2020-08-22 00:00:00
The output will be a datetime object that you can format with the strftime() method like so:
#Example
from datetime import datetime
now = datetime.now() #now will be a datetime object
now.strftime('%d/%m/%Y - %H:%M:%S') # DD/MM/YYYY - hours:minutes:seconds
Here's a list of some formats:
%A → Weekday (%a for abreviations and %w for numbers)
%-d → day of the mount (1, 2, 3, 4, ...)
%B → Mounth name (%b for abreviations and %-m for numbers)
%I → Hour (12h clock)
%p → AM or PM
%H → Hour (24h clock)
%M → Minutes
%S → Seconds
%f → Microseconds
%c → Local date and time representation
Using your code, it would be:
#tasks.loop(seconds=10)
async def muted_user_check(self):
self.cur.execute(f"SELECT * FROM mutedlist")
mute_list = self.cur.fetchall()
if not mute_list:
print("No user to unmute :D")
else:
timeNow = datetime.utcnow()
for mute in mute_list:
muteExpire = datetime.strptime(mute[3], '%Y-%m-%d %H:%M:%S')
if timeNow > muteExpire :
self.cur.execute(f"DELETE FROM mutedlist WHERE guildId=? AND userId=?", (mute[0], mute[1]))

Databricks pySpark datetime

I'm not sure why my datatime is being printed the way it is. I'm expecting the format of "%Y-%M-%D" (2020-05-11)
import datetime
from pyspark.sql.functions import *
currentdate = datetime.datetime.now().strftime("%Y-%M-%D")
print(dateValue)
Output:
2020-09-05/11/20
Try with %Y-%m-%d instead of %Y-%M-%D
currentdate = datetime.datetime.now().strftime("%Y-%m-%d")
print (currentdate)
#2020-05-11
#or using spark sql
currentdate=spark.sql("select string(current_date)").collect()[0][0]
print(currentdate)
#2020-05-11

How to format DATETIME type data from "2018-12-14T14:16:58.037967" to "2018-12-14 14:16:58.037967" with Groovy?

How to format DATETIME type data from 2018-12-14 14:16:58.037967 to 2018-12-14T14:16:58.037967 with Groovy?
Program:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
def DB_formattime = LocalDateTime.parse("2019-05-18 20:54:04.638314", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.n"))
print DB_formattime
Error:
Caught: java.time.format.DateTimeParseException: Text '2019-05-18 20:54:04.638314' could not be parsed at index 10
java.time.format.DateTimeParseException: Text '2019-05-18 20:54:04.638314' could not be parsed at index 10
at java_time_LocalDateTime$parse.call(Unknown Source)
at test.run(test.groovy:4)
Process finished with exit code 1
ihmo the simplest way here just to replace space with 'T' char...
"2018-12-14 14:16:58.037967".tr(' ','T')
however if you'd like to parse and format the date:
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
def DB_formattime = LocalDateTime.parse("2019-05-18 20:54:04.638314", DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.n"))
DB_formattime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.n"))

How to convert dtype: datetime to dtype: object with Python

How can I convert datetime object
15299 2015-07-27 10:00:00
Name: date, Length: 15300, dtype: datetime64[ns]
back to (dtype: object) whatever it's called
15299 07/27/2015 10:00:00
Name: date, Length: 15300, dtype: object
Thanks.
df['date'].dt.strftime("%m/%d/%Y %H:00:00")
Check this page out for formatting datetime in python.

Groovy: How do you initialise and compare date/time values from different timezones?

I need to standardise and compare date/time fields that are in differnt timezones. eg How do you find the time difference between the following two times?...
"18-05-2012 09:29:41 +0800"
"18-05-2012 09:29:21 +0900"
What's the best way to initialise standard varaibles with the date/time?
The output needs to display the difference and normalised data in a timezone (eg +0100) that is different to the incoming values and different to the local environment.
Expected Output:
18-05-2012 02:29:41 +0100
18-05-2012 01:29:21 +0100
Difference: 01:00:20
import java.text.SimpleDateFormat
def dates = ["18-05-2012 09:29:41 +0800",
"18-05-2012 09:29:21 +0900"].collect{
new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z").parse(it)
}
def dayDiffFormatter = new SimpleDateFormat("HH:mm:ss")
dayDiffFormatter.setTimeZone(TimeZone.getTimeZone("UTC"))
println dates[0]
println dates[1]
println "Difference "+dayDiffFormatter.format(new Date(dates[0].time-dates[1].time))
wow. doesn't look readable, does it?
Or, use the JodaTime package
#Grab( 'joda-time:joda-time:2.1' )
import org.joda.time.*
import org.joda.time.format.*
String a = "18-05-2012 09:29:41 +0800"
String b = "18-05-2012 09:29:21 +0900"
DateTimeFormatter dtf = DateTimeFormat.forPattern( "dd-MM-yyyy HH:mm:ss Z" );
def start = dtf.parseDateTime( a )
def end = dtf.parseDateTime( b )
assert 1 == Hours.hoursBetween( end, start ).hours
Solution:
Groovy/Java Date objects are stored as the number of milliseconds after
1970 and so do not contain any timezone information directly
Use Date.parse method to initialise the new date to the specified format
Use SimpleDateFormat class to specify the required output format
Use SimpleDateFormat.setTimeZone to specifiy the timezone of the output
data
By using European/London timezone rather than GMT it will
automatically adjusts for day light savings time
See here for a full list of the options for date time patterns
-
import java.text.SimpleDateFormat
import java.text.DateFormat
//Initialise the dates by parsing to the specified format
Date timeDate1 = new Date().parse("dd-MM-yyyy HH:mm:ss Z","18-05-2012 09:29:41 +0800")
Date timeDate2 = new Date().parse("dd-MM-yyyy HH:mm:ss Z","18-05-2012 09:29:21 +0900")
DateFormat yearTimeformatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss Z")
DateFormat dayDifferenceFormatter= new SimpleDateFormat("HH:mm:ss") //All times differences will be less than a day
// The output should contain the format in UK time (including day light savings if necessary)
yearTimeformatter.setTimeZone(TimeZone.getTimeZone("Europe/London"))
// Set to UTC. This is to store only the difference so we don't want the formatter making further adjustments
dayDifferenceFormatter.setTimeZone(TimeZone.getTimeZone("UTC"))
// Calculate difference by first converting to the number of milliseconds
msDiff = timeDate1.getTime() - timeDate2.getTime()
Date differenceDate = new Date(msDiff)
println yearTimeformatter.format(timeDate1)
println yearTimeformatter.format(timeDate2)
println "Difference " + dayDifferenceFormatter.format(differenceDate)

Resources