Current time in PartiQL query - time

How can I get the current time in a PartiQL query? The obvious answer of using the SQL NOW() function is not supported:
PartiQL> SELECT NOW() FROM data;
org.partiql.lang.eval.EvaluationException: No such function: now
Evaluator Error: at line 1, column 8: No such function: now

The builtin function UTCNOW() provides ISO 8601 formatted time, and UNIX_TIMESTAMP()gives seconds since UTC expoch. See BuiltInFunctions.md for the complete list of PartiQL builtins.
PartiQL> SELECT UTCNOW() FROM data;
==='
<<
{
'_1': `2021-02-11T07:43:58.980Z`
}
>>
---
OK!
PartiQL> SELECT UNIX_TIMESTAMP() FROM data;
==='
<<
{
'_1': 1613029453
}
>>
---
OK!
Note, have to select from something in PartiQL, you cannot do a naked select now() like you can in standard SQL.
UNIX_TIMESTAMP() returns whole seconds by default. Fractional seconds are available by chaining UNIX_TIMESTAMP() and UTCNOW():
PartiQL> SELECT UNIX_TIMESTAMP(UTCNOW()) FROM [1];
==='
<<
{
'_1': 1613030587.152
}
>>
---
OK!
PS: Thanks for the FROM [1] time, Matthew.

Related

Time Delta problem in Hackerrank not taking good answer / Python 3

The hackerrank challenge is in the following url: https://www.hackerrank.com/challenges/python-time-delta/problem
I got testcase 0 correct, but the website is saying that I have wrong answers for testcase 1 and 2, but in my pycharm, I copied the website expected output and compared with my output and they were exactly the same.
Please have a look at my code.
#!/bin/pyth
# Complete the time_delta function below.
from datetime import datetime
def time_delta(tmp1, tmp2):
dicto = {'Jan':1, 'Feb':2, 'Mar':3,
'Apr':4, 'May':5, 'Jun':6,
'Jul':7, 'Aug':8, 'Sep':9,
'Oct':10, 'Nov':11, 'Dec':12}
# extracting t1 from first timestamp without -xxxx
t1 = datetime(int(tmp1[2]), dicto[tmp1[1]], int(tmp1[0]), int(tmp1[3][:2]),int(tmp1[3][3:5]), int(tmp1[3][6:]))
# extracting t1 from second timestamp without -xxxx
t2 = datetime(int(tmp2[2]), dicto[tmp2[1]], int(tmp2[0]), int(tmp2[3][:2]), int(tmp2[3][3:5]), int(tmp2[3][6:]))
# converting -xxxx of timestamp 1
t1_utc = int(tmp1[4][:3])*3600 + int(tmp1[4][3:])*60
# converting -xxxx of timestamp 2
t2_utc = int(tmp2[4][:3])*3600 + int(tmp2[4][3:])*60
# absolute difference
return abs(int((t1-t2).total_seconds()-(t1_utc-t2_utc)))
if __name__ == '__main__':
# fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input())
for t_itr in range(t):
tmp1 = list(input().split(' '))[1:]
tmp2 = list(input().split(' '))[1:]
delta = time_delta(tmp1, tmp2)
print(delta)
t1_utc = int(tmp1[4][:3])*3600 + int(tmp1[4][3:])*60
For a time zone like +0715, you correctly add “7 hours of seconds” and “15 minutes of seconds”
For a timezone like -0715, you are adding “-7 hours of seconds” and “+15 minutes of seconds”, resulting in -6h45m, instead of -7h15m.
You need to either use the same “sign” for both parts, or apply the sign afterwards.

Subtract Time from CSV using Ruby

Hi I would like to subtract time from a CSV array using Ruby
time[0] is 12:12:00AM
time[1] is 12:12:01AM
Here is my code
time_converted = DateTime.parse(time)
difference = time_converted[1].to_i - time_converted[0].to_i
p difference
However, I got 0
p time[0].to_i gives me 12
is there a way to fix this?
You can use Time#strptime to define the format of the parsed string.
In your case the string is %I:%M:%S%p.
%I = 12 hour time
%M = minutes
%S = seconds
%p = AM/PM indicator
So to parse your example:
require 'time'
time = %w(12:12:00AM 12:12:01AM)
parsed_time = time.map { |t| Time.strptime(t, '%I:%M:%S%p').to_i }
parsed_time.last - parsed_time.first
=> 1
Use the Ruby DateTime class and parse your dates into objects of that class.

DateTime Ruby How to format correctly

My project is supposed to fetch specific values from multiple hashes a put those values in a text file. Ideally what I need my code to do is to have every date for the employees be seven days apart, so the text file would look something like this:
"Rachel Thorndike
2017-10-09-T04:29:46-05:00
Stacie Smith
2017-10-16-T04:29:46-05:00"
What this is supposed to do is fetch employee's names and put the time of their "handoff" on the line under them. I looked online and found the DateTime that Ruby features but it looks like whatever I do isn't working. My code is this:
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.strptime(jsonUser["start"], '%d-%m-%Y %H:%M')
utc = parsed.next_day(7).strftime('%d-%m-%Y %H:%M')
puts utc
end
But terminal returns this code with an error 'strptime': invalid date (ArgumentError). Would anybody help me get this code to work the way I want to? Anything that points me to the right direction? With explanations, if it isn't too much.
Thank you so much!
Update
I was able to get the iso8601 to appear under their name. My new code is
require 'date'
jsonUser["users"].each do |user|
somefile.puts user["user"]["summary"]
print 'Handoff Date + Time: '
parsed = DateTime.iso8601(jsonUser["start"])
utc = parsed.next_day(7).iso8601
somefile.puts utc
end
BUT the .next_day method isn't increasing by 7 days that I want too. Thought? I have only got one value that is appearing and that is going under every line. Its the value of jsonUser["start"] + 7 days...so `2017-
10-16T04:29:46-05:00`
This is what parse.next_day(7) gives me.
"Sr Chid
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ash A
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Ven D
Handoff Date + Time: 2017-10-16T04:29:46-05:00
Abhi S
Handoff Date + Time: 2017-10-16T04:29:46-05:00"
The value of jsonUser["start"] is 2017-10-09T04:29:46-05:00 so the good thing is that it did increase by 7 but it only did it once.
Update for Amadan
require 'date'
date = DateTime.iso8601(jsonUser["start"])
jsonUser["users"].each do |user|
if user["user"]["self"] == nil
nil
else
somefile.puts user["user"]["summary"].gsub(/\w+/, &:capitalize).gsub(/[.]/, ' ')
somefile.print 'Handoff Date + Time: '
date = date.next_day(7)
somefile.puts date.iso8061
end
end

how do I use cipher to query time range for neo4j nodes?

I have the following:
#neo.execute_query("match (node) where node.value = 'Rachel' return node.uuid, node.epoch_utc_i, node.value")
=> {"columns"=>["node.uuid", "node.epoch_utc_i", "node.value"], "data"=>[["87f7d4c7-c161-4ba2-bce6-8c3c5104f60c", 1493774726, "Rachel"], ["23574509-3d67-4783-a00a-66a2b49b5cbd", 1493968856, "Rachel"], ["e7f01367-baa6-431b-8760-1979c215d777", 1494035989, "Rachel"], ["4cc0f450-a1c4-4992-85c1-9bcb4d759d6a", 1494047641, "Rachel"], ["e3a83a43-3b0f-4a7f-944b-4f582fb47b72", 1494183024, "Rachel"], ["1d8be261-e788-449c-9fa1-9db82816fa37", 1494531971, "Rachel"]]}
However, I am unable to use WHERE to return only those with the epoch_utc_i time between Today and Yesterday, for example:
2.2.1 :045 > yesterday = Chronic.parse('1 day ago').to_i
=> 1494906466
2.2.1 :046 > #neo.execute_query("match (node) where node.value Contains 'Rachel' AND node.epoch_utc_i > yesterday return node.uuid, node.epoch_utc_i, node.value")
Neography::SyntaxException: NeographyError:
--message: Variable `yesterday` not defined (line 1, column 72 (offset: 71))
Edit: tried passing the value into the query
#neo.execute_query("match (node)-[:gratefulFor]->(node2) where node.bot_client_id = 'aiaas-1409611358153-user-0149' AND node2.epoch_utc_i > $yesterday return node.bot_client_id, node2.epoch_utc_i, node2.value", {:yesterday => yesterday})
Neography::SyntaxException: NeographyError:
--message: Variable `$yesterday` not defined (line 1, column 121 (offset: 120))
--request: {:path=>"/db/data/cypher", :body=>"{\"query\":\"match (node)-[:gratefulFor]->(node2) where node.bot_client_id = 'aiaas-1409611358153-user-0149' AND node2.epoch_utc_i > $yesterday return node.bot_client_id, node2.epoch_utc_i, node2.value\",\"params\":{\"yesterday\":1494908349}}"},
Question:
How could I achieve as I intended in my code above, only those nodes where the epoch time is greater than the epoch time for yesterday?
You would have to pass the variable to the query. Parameters are passed to Cypher queries using $yesterday or {yesterday} (old notation). The query will be as follows:
MATCH (node) WHERE node.value
CONTAINS 'Rachel' AND node.epoch_utc_i > {yesterday}
RETURN node.uuid, node.epoch_utc_i, node.value"
The query execution will be passed the yesterday variable and interpolated into the query above.
#neo.execute_query(query, {:yesterday => yesterday})

In KRL How can I get the current year, month, and day?

I am working on an application in which I need to get the current year, month, and day. Is there a way to get this information in the pre block of a rule?
Can I get this data as a string or a number or both?
There are currently time functions documented on http://docs.kynetx.com/docs/Time but none of them seem to work for what I am trying to do.
Is there a way to set the timezone when getting this data?
I was able to do it using strftime which appears to be an undocumented feature so use with caution.
ruleset a60x518 {
meta {
name "date-test"
description <<
date-test
>>
author "Mike Grace"
logging on
}
rule testing {
select when pageview ".*"
pre {
retTime = time:strftime(time:now({"tz":"America/Denver"}), "%c");
month = time:strftime(time:now({"tz":"America/Denver"}), "%B");
year = time:strftime(time:now({"tz":"America/Denver"}), "%Y");
day = time:strftime(time:now({"tz":"America/Denver"}), "%d");
}
{
notify("time",retTime) with sticky = true;
notify("month",month) with sticky = true;
notify("year",year) with sticky = true;
notify("day",day) with sticky = true;
}
}
}
App run on example.com twice. Once with the timezone set to New York and onother time set to Denver
I used this site http://www.statoids.com/tus.html to get the correct strings to use for the timezone. I have no idea if they all work. I just found this site and tried a few and they worked so use with caution.
Perhaps the docs got reverted. For convenience, here is the documentation for strftime:
time:strftime()
Convert a datetime string to a different format
Usage
time:strftime(`<string>`,`<format>`)
Valid format arguments to strftime follow the POSIX strftime conventions.
Samples
time:strftime(xTime,”%F %T”) # 2010-10-06 18:15:24
time:strftime(xTime,”%F”) # 2010-10-06
time:strftime(xTime,”%T”) # 18:19:29
time:strftime(xTime,”%A %d %b %Y”) # Wednesday 06 Oct 2010
time:strftime(xTime,”%c”) # Oct 6, 2010 6:25:55 PM
The other time functions:
time:now()
Current datetime based upon user’s location data
Usage
time:now()
time:now({“tz” : <timezone>)
time:new()
Create a new RFC 3339 datetime string from a string (allows some flexibility in how the source string is formatted)
Usage
time:new() # Equivalent to time:now()
time:new(<string>)
Valid formats for the datetime source string can be found in ISO8601 (v2000).
time:add()
Add (or subtract) a specific number of time units to a source string
Usage
time:add(<string>,{<unit> : n})

Resources