How to change field.date format in OpenERP 7 (Odoo) - python

I'm using two dates to form a "period" in openErp:
_columns = {
'date_from': fields.date('From', required = True),
'date_to': fields.date ('To', required = True),
}
These two fields are inputs for the user, after they choose both dates I create a string called "period"
'period': str(date_from)+ ' // ' + str(date_to),
thing is, that the dates are in format "y-m-d" and i need them to be "d-m-y", even if i select my language in openERP it wont changue that string.
Is there any way that i can change that format ?
Thanks in advance.

As I found out when you try to get objects date/datetime field value it's returned as string, so this is ugly but for now (as I haven't seen better method) I do something like:
from dateutil import parser
...
my_date = parser.parse(my_object.date)
proper_date_string = my_date.strftime('%d-%m-%Y')
You also can use python datetime module and parse date string via strptime. But dateutil is required for openerp so you can use it.

Related

Converting a string to Timestamp with Pyspark

I am currently attempting to convert a column "datetime" which has values that are dates/times in string form, and I want to convert the column such that all of the strings are converted to timestamps.
The date/time strings are of the form "10/11/2015 0:41", and I'd like to convert the string to a timestamp of form YYYY-MM-DD HH:MM:SS. At first I attempted to cast the column to timestamp in the following way:
df=df.withColumn("datetime", df["datetime"].cast("timestamp"))
Though when I did so, I received null for every value, which lead me to believe that the input dates needed to be formatted somehow. I have looked into numerous other possible remedies such as to_timestamp(), though this also gives the same null results for all of the values. How can a string of this format be converted into a timestamp?
Any insights or guidance are greatly appreciated.
Try:
import datetime
def to_timestamp(date_string):
return datetime.datetime.strptime(date_string, "%m/%d/%Y %H:%M")
df = df.withColumn("datetime", to_timestamp(df.datetime))
You can use the to_timestamp function. See Datetime Patterns for valid date and time format patterns.
df = df.withColumn('datetime', F.to_timestamp('datetime', 'M/d/y H:m'))
df.show(truncate=False)
You were doing it in the right way, except you missed to add the format ofstring type which is in this case MM/dd/yyyy HH:mm. Here M is used for months and m is used to detect minutes. Having said that, see the code below for reference -
df = spark.createDataFrame([('10/11/2015 0:41',), ('10/11/2013 10:30',), ('12/01/2016 15:56',)], ("String_Timestamp", ))
from pyspark.sql.functions import *
df.withColumn("Timestamp_Format", to_timestamp(col("String_Timestamp"), "MM/dd/yyyy HH:mm")).show(truncate=False)
+----------------+-------------------+
|String_Timestamp| Timestamp_Format|
+----------------+-------------------+
| 10/11/2015 0:41|2015-10-11 00:41:00|
|10/11/2013 10:30|2013-10-11 10:30:00|
|12/01/2016 15:56|2016-12-01 15:56:00|
+----------------+-------------------+

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.

Formatting using dt.datetime Python

I'm trying to use dt.datetime to split my data by date. However, my date structure is ' date2013-07-04' see attached image. Therefore, it doesn't fit with the traditional '%m/%d/%Y %H:%M:%S.%f' format: I've tried re-formatting but I get 2 errors;
does not match format
redefinition of group name 'd' as group 4; was group 1.
The line of code I have is:
x = 'date2013-07-04'
df['Date'] = filename['Date'].apply(lambda x: dt.datetime.strptime(x,'%date%YYYY-%mm-%dd'))
Then the errors pull up. Can anyone help? Cheers!
The reason it doesn't work is that your format string is invalid, you can find the reference for python's date formatting here.
In the meantime you can try this snippet:
import datetime as dt
x = "date2013-07-04"
print(dt.datetime.strptime(x, "date%Y-%m-%d"))
It should do it.

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

getting date fields from oracle in correct format using Python and cx-oracle

I am retrieving a load of data from one oracle database and then inserting it into another non linked database that is structurally the same.
I am doing it by doing:
select * from xxx where id = parameter
and
Select COLUMN_NAME from user_tab_columns where table_name=xxx
then with zip putting them in a dictionary as table_name:Data to build the insert from
Problem is it is returning the date fields as datetime.datetime(99, 12, 31, 0, 0). I need this as 31-dec-2999. How can I get it to return it like this or do I need to create a regex to do this?
I'm new to all this so if my method seems ridiculous feel free to say so and suggest a better method
Many thanks
Adam
The cx_Oracle database adapter is giving you datetime.datetime() objects. Use methods on those objects to format them to a string if you require a different output.
The datetime.strftime() method would be best suited for your purposes:
dtobject.strftime('%d-%b-%Y')
Demo:
>>> import datetime
>>> dtobject = datetime.datetime(2999, 12, 31, 0, 0)
>>> dtobject.strftime('%d-%b-%Y')
'31-Dec-2999'
If, however, Oracle is really returning objects with the year set to 99 (not 2999) you need to repair the data:
if dtobject.year < 100:
dtobject = dtobject.replace(year=dtobject.year + 2900)
You can use strftime.
For example:
>>> import datetime
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2013-08-13 13:10
In your case, this can make it:
strftime("%d-%b-%Y")
where:
%b locale's abbreviated month name (e.g., Jan)
%d day of month (e.g., 01)
%Y year

Categories

Resources