I wrote the Directions API and the response is what I would expect it to be.
Here's the sample response from documentation:
"duration": {
"value": 74384,
"text": "20 hours 40 mins"
},
"distance": {
"value": 2137146,
"text": "1,328 mi"
},
I wanted to check with the community if there is a way for the API to return a formatted text for duration.
"20 hours 40 mins" is formatted as string, however, I'd like to be able to perform operations with duration, so time format would be ideal. I am thinking it would be good practice to have the response be formatted versus transforming the pandas dataframe. That's the alternative I guess.
Just pass the "value" variable of "duration" which is in seconds to timedelta()
Example :
import datetime as dt
dt.timedelta(seconds = value)
Hope this helps!
Related
I currently have a JSON schema that required date time validation.
"type": "array",
"items": {
"type": "object",
"properties": {
"activeFrom": {"type": "string", "format": "datetime", "pattern": ********},
}
...
Validation should exist so that
Dates are in the format YYYY-MM-DDTHH:MM:SS.00-00:00
Represents (Year-Month-Day"T"Hour:Minute:Second.Milisecond-TimeZoneHour:TimeZoneMinute)
GMT Time Zone = 00:00
Example: 2022-11-12T16:32:21.00-00:00
Incorrect dates are not acceptable (i.e. 31 days in February, or incorporate leap year)
I have tried many different patterns (******) but can't seem to find one that works.
Appreciate any help and apologies if this questions structure is incorrect, new to SO :)
I think this will check dates in the way you're looking for.
First though pip install python-dateutil
EDIT: Have changed following comments.
from dateutil.parser import parse, ParserError
from datetime import timedelta
def check_date_is_legit(input_date):
"""
Checks if an input_date is legitimate
:param input_date: string, date to check.
:return result: False if the date isn't legit and True if it is.
"""
try:
parsed = parse(input_date)
if parsed.tzname() == 'UTC':
result = True
else:
result = False
except ParserError:
result = False
return result
My entries in MongoDB have a publishedDate field as follows:
publishedDate:"{'$date': '1999-08-01T00:00:00.000-0700'}"
How do I retrieve the entries via collection.find with $regex, using user's input for year?
From MongoDB version >= 4.4 we can write custom filters using $function operator so try this:
let yearRegex = /^1999/;
db.testCollection.find({
$expr: {
$function: {
body: function(publishedDate, yearRegex) {
return yearRegex.test(publishedDate);
},
args: [{ $toString: "$publishedDate" }, yearRegex],
lang: "js"
}
}
});
Note: Instead of $toString we can also use $dateToString with timezone to cover edge cases.
"{'$date': '1999-08-01T00:00:00.000-0700'}" looks like MongoDB extended JSON notation for a Datetime object.
If the data in the collection is actually a date, note that the timezone in the database will be UTC, so the start/end would be off by a few hours if you intended to use any other timezone.
You can build a date object for the beginning of the year, and another for the beginning of the following year, and query for dates between:
let queryYear = 1999
db.collection.find({
publishedDate:{
$gte: new Date( queryYear + "-01-01T00:00:00-0700" ),
$lt: new Date( (queryYear+1) + "-01-01T00:00:00-0700")
}})
This allows to you build a date object with the desired timezone, and this query could also make use of an index on the publishedDate field.
I just started using mongoDB (Version 3.6.8) today, and I like it.
I was reading that it should be possible to have a date object directly in the database, but I can't get it to work.
Also I was wondering if it is the best solution or if I should just store my dates as "Epoch millis" instead?
I am trying to use use the $dateFromString keyword which should work but i receive this error:
bson.errors.InvalidDocument: key '$dateFromString' must not start with '$'
My code looks like this:
from datetime import date
import pymongo
dbcli = pymongo.MongoClient('mongodb://192.168.1.8:27017')
db = dbcli['washbase']
col = db['machine']
def conv(dato):
return {
'$dateFromString': {
'dateString': dato,
'format': '%Y-%m-%d',
'timezone':'Europe/Copenhagen',
}
}
today = date.today().isoformat()
data = {
'day': conv(today),
'time':12,
'room':'2B',
}
col.insert_one(data)
The reason why I need something like a date-object in the database is because I want to do a conditional query on the data, so that the database only sends the data i require. So i expect to do something like this.
result = col.find(
{
'day' : {
'$gt' : {
'$date' : '2020-01-01'
}
}
}
)
for x in results:
print(x)
But when I do this the app prints nothing.
The $dateFromString is an operator for MongoDB aggregations. An aggregation is a powerful way to create complex queries in MongoDB. Hence, this might not be what you need.
I would recommend storing the dates in the normal format. So your code should look something like this:
from datetime import date
import pymongo
dbcli = pymongo.MongoClient('mongodb://192.168.1.8:27017')
db = dbcli['washbase']
col = db['machine']
today = date.today().isoformat()
data = {
'day': today,
'time':12,
'room':'2B',
}
col.insert_one(data)
If you are concerned about timezones, MongoDB stores each date in UTC by default, converting whatever timezone is specified in your date to UTC. When reading the dates, you can then convert them to whatever timezone you need.
EDIT:
When writing your query, try using an actual date object. This converts the query date to an actual ISO date that the DB can understand.
col.find({'day': {'$gte': ISODate(date.today) }})
If you're trying to find entries that fall within a date range, you can do something like:
col.find({'day': {'$gte': ISODate(date.today), '$lte': ISODate(date.today + 24 hours) }})
Record_collection as in database :
{
"_id": 792ydihd2989002,
"mydate":ISODate("2020-06-08T09:05:12.191Z"),
"_id": 7676wwhdhwte77,
"mydate":ISODate("2020-07-08T09:05:12.191Z"),
"_id": 7676wwhdeete77,
"mydate": ,
}
Expected Output:
I need a query to get all he records where the 'mydate' is older than 20 minutes or the mydate value is empty. Either the mydate must be 20 minutes older or null.
I have tried in pymongo also with mongodb but it matches with date and not with the time.
i am trying to place an order with the coinbase pro api, but it fails because of the invalid expire_time.
i tried to pass the "cancel_after" parameter as timestamp, date string in iso format and seconds but still no success.
i dont really understand what is meant by
cancel_after [optional]* min, hour, day
my params are as follows:
params = {
"type":"limit",
"side":"buy",
"product_id": "BTC-EUR",
"price": "1000.0",
"size": "0.01",
"stop_price": "1100.0",
"stop": "entry",
"time_in_force": "GTT",
"cancel_after": ?
}
has some faced the same problem ?
API-Reference for placing an order
cancel_after [optional]* min, hour, day
min,hour or day muste passed as string.
there is no real datetime value you can only set:
"min" => 1 Minute
"hour" => 1 Hour
"day" => 1 Day