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.
Related
I'm trying to compare date that comes from ActionMailbox mail.date with a date field in my PostgreSQL DB Table to check if a post for the same date already exists. The dates comes in different format I guess, how canI format them in same way to compare? The time section is irrelevant.
Date format that comes from email as below I guess. Looking at the Logs on server
Date: Wed, 24 Mar 2021 09:57:57 +0000
Date format I have in the DB is as below. Output of Post.last in rails c
date: "2021-03-24 09:57:57.000000000 +0000
I need to check if dates matches or not?
Btw the interesting thing is, I can just save mail.date to db without any particular formatting, I guess it is formatting itself before saving.
Date format I have in the DB is as below.
Databases don't store timestamps nor dates as strings, they're stored as numbers. The string format is just for humans. Unless you're storing the date as a string.
I'm trying to compare date that comes from ActionMailbox mail.date with a date field in my PostgreSQL DB Table to check if a post for the same date already exists.
Those are standard date formats, RFC 2822 and ISO 8601. So long as your date column has a date type you don't need to convert them. Rails or Postgres will take care of the conversion.
Thing.where(date: mail.date)
However, your "date" field is storing a timestamp. It might be misnamed, or it might be mistyped. If you only want to store the date, use t.date in your migration.
If you did, you'd parse them into Time objects, then compare.
t1 = Time.zone.parse("Wed, 24 Mar 2021 09:57:57 +0000")
t2 = Time.zone.parse("2021-03-24 09:57:57.000000000 +0000")
p t1 == t2
Btw the interesting thing is, I can just save mail.date to db without any particular formatting, I guess it is formatting itself before saving.
Rails type conversion is parsing the String into an ActiveSupport::TimeWithZone object.
> thing = Thing.new
> thing.created_at = "Wed, 24 Mar 2021 09:57:57 +0000"
> thing.created_at
=> Wed, 24 Mar 2021 04:57:57.000000000 CDT -05:00
> thing.created_at.class
=> ActiveSupport::TimeWithZone
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
I am working with Breeze and running into some date/time issues.
I have a field in a form, with a date time picker, that is returning a value of 07/20/2018 14:00. For this example, assume I am in CST timezone (GMT -0500). What I would like to do is pass that value to my Breeze entity manager and have it saved in my database correctly. I get the date into a variable:
dateVariable = ctx.ChosenTime;
This works and puts a value of 07/20/2018 14:00 into the variable dateVariable.
I create a new entity:
var newEntity = entityManager.createEntity('Test Entity', {Date: dateVariable};
And when I debug and check the value of newEntity, it has a property called Date with the proper value. However, once I call entityManager.SaveChanges(), and then get the returned value back, it is displayed as 07/20/2018 19:00. Since Breeze is handling the display value (via data binding), I am not sure why this is happening. Any advice would be appreciated. Thanks
When the JSON response comes from the server, if the date string does not have a timezone specifier, Breeze assumes it is in UTC and puts a "Z" on the end before parsing. So it converts your local time to UTC after the round-trip to the server. The solutions are:
Change your server-side property to DateTimeOffset or similar data type that preserves the timezone of a date. This way the returned date will have a time zone.
Tell Breeze not to add the "Z" and just interpret the date in local time. See this SO answer for more information.
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.
I have a model where the data is stored in json format in a jsonb column in postgres.
I want to sort the output by a data field using an activerecord query.
Model.all.order("json_data -> 'date'")
gives me an output but orders it alphabetically based on the date string.
Is there an easy way I can sort this as a date?
Note: The dates are in the following format:
"Fri, 24 Jun 2016 04:13:26 -0700"
If the date is in a sensible format Postgres will deal with this automatically.
Model.all.order("(json_data ->> 'date')::timestamp with time zone DESC")
or
Model.all.order("(json_data ->> 'date')::timestamptz DESC")
If your date field string is a little unorthodox, you can do the following
Model.all.order("to_timestamp(json_data->>'date','Dy, DD Mon YYYY HH24:MI:SS ') DESC")
Details here
Note the ->> there to output the string rather than the json object.
You can of course just create an extra column and store your information there as per #Uzbekjon's answer below.
Is there an easy way I can sort this as a date?
Not as part of the jsonb field, since JSON doesn't know anything about dates.
So, an easy alternative would be to store them as a separate table column.
If you really have to store them as an element of your json field, then I would suggest one of the two options:
Store your field as timestamp. Since, json fields in postgresql support numeric values and it could (potentially) optimize the sorting.
Store your date in ISO 8601 format string. It would sort correctly, even if it is a string.