Is there a way to configure rdflib serializer to use a specific UTC date format? - rdflib

I have a server that rejects a triple using the UTC offset syntax:
rtc_cm:due "2020-09-08T14:30:00+00:00"^^xsd:dateTime (Turtle)
but succeeds using the Z suffix:
rtc_cm:due "2020-09-08T14:30:00Z"^^xsd:dateTime (Turtle)
Is there a way to configure the Python rdflib serializer to prefer the Z suffix for UTC dates.
I noticed that rdflib.util.date_time() seems to do that, but the serializer does not, at least by default.

If you're using timezones, you need to use the literal xsd:dateTimeStamp.
See https://www.w3.org/TR/xmlschema11-2/#dateTimeStamp

Related

Retrieve Hash values with key in a specific date format

I'm using Rails. I've stored a count by month in a postgres db as a hash using hstore.
The stored hash is formatted as follows:
{"2017-03-01 00:00:00 UTC"=>"10", "2017-04-01 00:00:00 UTC"=>"3"}
I'm struggling to find a great way to retrieve specific month counts from this hash due to the date format used for the key.
QUESTION
What is the best way to format a string to match the current hash key date format?
For example for March in the Hash the key is "2017-03-01 00:00:00 UTC"
However, a new DateTime for March 1 2017 is formatted as "2017-03-01T00:00:00+00:00"
Or is it best to change the format of how I am storing the hash in the first place?
If you need a timestamp in a specific format, the standard tool to use is DateTime#strftime (all the time-ish classes will have a strftime method and they all behave the same). In your case:
some_datetime.utc.strftime('%Y-%m-%d %H:%M:%S %Z')
And hooking that up to ActiveRecord:
Model.where('your_hstore -> :key', :key => some_datetime.utc.strftime('%Y-%m-%d %H:%M:%S %Z'))
Or:
Model.where('your_hstore -> ?', some_datetime.utc.strftime('%Y-%m-%d %H:%M:%S %Z'))
%Z should be the "Time zone abbreviation name" and for me it produces strings like 'UTC', 'PDT', ... If your strftime (which almost certainly is just a wrapper around the system's libc version of strftime) doesn't produce the strings that you want then you have some options:
Drop the timezone completely if it will always be UTC. Then the keys would look like 2017-03-01 00:00:00 and you'd use '%Y-%m-%d %H:%M:%S' as your strftime format string.
If they keys are actually just dates as they appear to be, then use dates and drop the time-of-day. Then your keys would look like 2017-03-01, you'd use Date instances in Ruby rather than DateTimes, and you'd say some_date.strftime('%Y-%m-%d') or some_date.iso8601 in Ruby to get your hstore keys.
If you are using non-UTC timezones, then convert everything to UTC and go with 1 or 2.
If you don't want any of the above, switch to numeric timezone offsets (2017-05-10 18:05:57 +0000, 2017-05-10 18:06:48 +00:00, ...) and use %z, %:z, or %::z in the strftime format string (see the docs for difference between these three).
These of course require reworking any data you already have in the database but it is best to get the out of the way sooner rather than later.

How to use strptime to convert timestamp string with milliseconds to time object

I need to use strptime to convert a timestamp string with milliseconds to a Time object.
A work around is using parse:
t= Time.parse('29 Sep 2013 12:25:00.367')
=> 2013-09-29 12:25:00 -0400
But it is very important for my code to use strptime, because I want to be able to pass multiple types of format including: "HH:MM", "HH", etc. through the function.
I can do it with nanoseconds like this:
Time.strptime("12:34:56:789434", "%H:%M:%S:%N")
=> 2016-03-16 12:34:56 +0100
I want something like this:
Time.strptime("12:34:56:789", "%H:%M:%S:%[insert magic letter that represent milliseconds]")
My thought is that there must be a way to do it with milliseconds as well.
Is it possible and how?
Try %L.
Refer to Ruby's DateTime documentation.
If you can update to Ruby 1.9.3, it supports this using %3N:
http://ruby-doc.org/core-1.9.3/Time.html#method-i-strftime

Eve: query by date range using 'where'

Given this example curl, I'd expect to find all events that start after the given date.
events?where={"start_time": {"$gt": "2014-10-25T03:00:00Z"}}
curl -i -g 'http://127.0.0.1:5000/events?where={%22start_time%22:%20{%22$gt%22:%20%222014-10-25T03:00:00Z%22}}'
However, mongo doesn't return anything unless I intercept the request and turn it into a date object. I've defined start_date to be a datetime object in my schema.
Is this expected behavior? Is there another way to get the results I want without using a pre-request hook and validating/converting the datetime strings for certain keys?
Try this:
events?where={"start_time": {"$gt": "Fri, 10 Oct 2014 03:00:00 GMT"}}
You are supposed to pass RFC1123 dates with your queries and/or JSON fields.

What timestamp is this?

I'm working with a Dataset which gives me a Time Variable for Objects, just like the created_at. The value tho is :
1398037671
Is this a special kind of encoding Timestamps or am i missing something ?
I guess it is "seconds since the Epoch" timestamp
Time.at(1398037671)
2014-04-21 01:47:51 +0200
http://www.ruby-doc.org/core-2.1.1/Time.html#method-c-at
That's a Unix timestamp. That specific timestamp represents 04 / 20 / 14 # 11:47:51pm UTC
You can find out more about them here: http://www.unixtimestamp.com/index.php and at good old wikipedia here: http://en.wikipedia.org/wiki/Unix_time
In Ruby, you can generate a Unix timestamp with Time.now.to_i (or obviously any other time if you don't want the timestamp for now).

How to convert Evernote API Timestamps to Postgresql Timestamps

I'm accessing the Evernote API via the evernote gem for ruby on rails, and I'm storing the objects (notebooks, tags, notes, etc.) in a Postgresql database.
Evernote returns timestamps that look like this:
1344141917000
1344141967000
1344138641000
The evernote api documentation says this is the number of milliseconds that have passed since the base time of January 1, 1970, 00:00:00 GMT
I've conducted the following exercise in the rails console in an attempt to reconstruct the date.
evernote_timestamp_base = Time.gm(1970,01,01,00,00,00)
=> 1970-01-01 00:00:00 UTC
evernote_timestamp_base + 1344138641000
=> 44564-01-22 04:43:20 UTC
Definitely not right. But chopping those last three zeros off yields the right date:
evernote_timestamp_base + 1344138641
=> 2012-08-05 03:50:41 UTC
Am I missing something here? What's the deal with those last three zeros? Will I have to parse and chop the evernote timstamp values and then add them to the 1970 base, or is there an easier way?
Also, what's the best Postgresql data type for storing these values?
Thanks in advance for your help.
To do this in Ruby, use Time.at. You'll need to divide by 1000 since Evernote timestamps are in millseconds and Ruby/Unix timestamps are in seconds.
createdNote = noteStore.createNote(authToken, note)
createTime = Time.at(createdNote.created / 1000);
puts "Note was created at #{createTime}"
In postgresql you could use a timestamp [with|without] time zone as the type for the column.
The unix timestamp is defined as the number of seconds since January 1, 1970, 00:00:00 GMT which is what Ruby and many other systems use and or support. PostgreSQL can do the conversion for you to. Just pass the value in seconds to the to_timestamp function.
SELECT to_timestamp(1344138641000/1000.0)
To convert it back use
SELECT EXTRACT(EPOCH FROM col)

Resources