JSON Schema for DateTime Validation in Python - AWS Lambda - python

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

Related

How to use regex on date to extract entry by year?

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.

MongoDB/Python - Date in collection (to use for query)

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) }})

Google Maps Directions API response duration time format - Python

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!

Print date from MongoDB in Python

I have two objects in the db with diferent dates:
{
"_id" : ObjectId("5addeaf92602ff20497e9406"),
"success" : true,
"timestamp" : 1524477784,
"base" : "EUR",
"date" : "2018-04-22",
"rates" : {
"AED" : 4.492662,
"ALL" : 128.39508,
"AMD" : 586.837094}
and the second one:
{
"_id" : ObjectId("5addb57d0043582d48ba898a"),
"success" : true,
"timestamp" : 1524477784,
"base" : "EUR",
"date" : "2018-04-23",
"rates" : {
"AED" : 4.492662,
"ALL" : 128.39508,
"AMD" : 586.837094}
My python code:
import pymongo
uri = "mongodb://127.0.0.1:27017"
client = pymongo.MongoClient(uri)
database = client['db']
collection = database['currency']
d=(*something I guess*)(input('Insert date: '))
item = collection.find_one({})
data= item['date']['d']
print(data)
What I want is to insert a day or a specific date, and then the program would print that specific day info.
In the db the date is in String and I think that I have to convert it.
Thanks in advance.
Well, first of all data= item['date']['d'] this will not gona work because the 2nd [] is used for indexing. And in your case its up to you either you take input same as your string date or convert it into date the take input then put a check on it
For converting string into date do this:
from datetime import date
year, month, day = map(int, (d['date']).split("-"))
required_date = date(year, month, day)
print required_date
Note: assuming that i have your mongo object/dictionary in d
and for second case:
take input from user in the same format in string for e.g:
userinput = str(raw_input('Enter Date: in Format(year, month, day) with seprator "-" e.g: 2018-04-23 \n'))
if userinput == d['date']:
print 'Correct'
again assuming that d is your mongo object.
Note:
Its for exmaple what ever case you are using use then put a if check on it.
Hope it helps you! :)

Separate one year from a data value; taking care of leap years

This should be pretty simple but I can't get the syntax correct. So, I have a parameter dictionary as below -
paramDict = {
StartPostingDateFilter": {
"msgPrompt": "Start date",
"dataType": "datetime",
"tableField": [{"table":"TableName",
"field":"ColumnName"}],
"value": ["2006-01-01"]
}
}
StartPostingDateFilter = paramDict['StartPriorDateFilter']['value'][0]
Now, I want to subtract one year from the "StartPriorDateFilter" user provided date value. How do I take care of it if it's a leap year? I want to achieve the below -
If, StartPostingDateFilter = '2006-01-01'
Then create new variable, NewStartPostingDateFilter = '2005-01-01'
Thanks.
You could use dateutil:
from dateutil.relativedelta import relativedelta
from dateutil import parser
d = parser.parse(StartPostingDateFilter)
print((d - relativedelta(years=1)).date())
2005-01-01
You could also use datetime and replace catching feb 29th returning the year - 1 and feb 28th if it was:
d = datetime.strptime(StartPostingDateFilter, "%Y-%m-%d")
sub = d.replace(year=d.year - 1) if (d.month != 2 and d.day) != 29 else datetime(d.year-1, 2, 28)

Categories

Resources