It seems you have an aware datetime object. If you print it then it looks like:
2014 - 12 - 14 14: 46: 43.379518 + 00: 00
Converting datetime.date to UTC timestamp in Python shows several ways how you could get the timestamp e.g.,
timestamp = date_joined.timestamp() # in Python 3.3 +
Or on older Python versions:
from datetime import datetime # local time = utc time + utc offset utc_naive = date_joined.replace(tzinfo = None) - date_joined.utcoffset() timestamp = (utc_naive - datetime(1970, 1, 1)).total_seconds()
import dateutil.parser
from datetime
import datetime
import calendar
import pytz
def str2ts(s):
''
' Turns a string into a non-naive datetime object, then get the timestamp '
''
# However you get from your string to datetime.datetime object
dt = dateutil.parser.parse(s) # String to non - naive datetime
dt = pytz.utc.normalize(dt) # Normalize datetime to UTC
ts = calendar.timegm(dt.timetuple()) # Convert UTC datetime to UTC timestamp
return int(ts)
def ts2str(ts):
''
'Convert a UTC timestamp into a UTC datetime, then format it to a string'
''
dt = datetime.utcfromtimestamp(ts) # Convert a UTC timestamp to a naive datetime object
dt = dt.replace(tzinfo = pytz.utc) # Convert naive datetime to non - naive
return dt.strftime('%Y-%m-%d %H:%M:%S.%f%z')
Which we can test with:
# A list of strings corresponding to the same time, with different timezone offsets ss = [ '2014-12-14 14:46:43.379518+00:00', '2014-12-14 15:46:43.379518+01:00', '2014-12-14 16:46:43.379518+02:00', '2014-12-14 17:46:43.379518+03:00', ] for s in ss: ts = str2ts(s) s2 = ts2str(ts) print ts, s2
Output:
1418568403 2014 - 12 - 14 14: 46: 43.000000 + 0000 1418568403 2014 - 12 - 14 14: 46: 43.000000 + 0000 1418568403 2014 - 12 - 14 14: 46: 43.000000 + 0000 1418568403 2014 - 12 - 14 14: 46: 43.000000 + 0000
You can try the following Python 3 code:
import time, datetime
print(time.mktime(datetime.datetime.strptime("2014-12-14 14:46:43.379518", '%Y-%m-%d %H:%M:%S.%f').replace(tzinfo = datetime.timezone.utc).timetuple()))
which prints:
1418568403.0
The solution: use str(django_datefield)
.
parse(str(django_datefield))
I know this is an old post, but I want to highlight that the answer is likely what @Peter said in his comment:
It looks like member.date_joined is already a datetime object, and there 's no need to parse it. – Peter Feb 25 '
17 at 0: 33
In Example 1, I’ll explain how to replicate the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in the Python programming language.,In this tutorial, you’ll learn how to deal with the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’ in the Python programming language.,In Example 2, I’ll show how to fix the AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’.,When working with the datetime module in Python, there can also be error messages related to AttributeError: type object ‘datetime.datetime’ has no attribute ‘datetime’
from datetime
import datetime # Import datetime
x = datetime.datetime(2022, 8, 6) # Creating datetime object does not work # AttributeError: type object 'datetime.datetime' has no attribute 'datetime'
import datetime # Import datetime
x = datetime.datetime(2022, 8, 6) # Creating datetime object works properly print(x) # Print properly created datetime object # 2022 - 08 - 06 00: 00: 00
AttributeError: ‘datetime.datetime’ object has no attribute ‘timedelta’,AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. datetime is a built-in Python module that supplies classes for manipulating dates and times. One of the classes in datetime is called datetime. It can be unclear when both the module and one of the classes share the same name. If you use the import syntax:,How to Solve Python AttributeError: type object ‘datetime.datetime’ has no attribute ‘fromisoformat’,How to Solve Python AttributeError: ‘datetime.datetime’ has no attribute ‘datetime’
You are importing the datetime
class, not the datetime
module. The class timedelta
is an attribute of the datetime module. We can verify this by using dir()
.
import datetime # dir of datetime module attributes = dir(datetime) print(attributes) print('timedelta' in attributes)
import datetime # dir of datetime module attributes = dir(datetime) print(attributes) print('timedelta' in attributes)
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']
True
Let’s look at the code:
from datetime
import datetime, timedelta
today = datetime.now()
dates = []
dates.append(today.strftime("%m%d%Y"))
for i in range(0, 8):
next_day = today + datetime.timedelta(days = i)
dates.append(next_day.strftime("%m%d%Y"))
print(dates)
from datetime
import datetime, timedelta
dates = []
today = datetime.now()
dates.append(today.strftime("%m%d%Y"))
for i in range(0, 8):
next_day = today + timedelta(days = i)
dates.append(next_day.strftime("%m%d%Y"))
print(dates)
Let’s run the code to see the result:
['05192022', '05192022', '05202022', '05212022', '05222022', '05232022', '05242022', '05252022', '05262022']
Last updated: Apr 20, 2022
Copied!
import datetime
#⛔️ AttributeError: module 'datetime'
has no attribute 'today'
print(datetime.today())
Copied!#👇️ 2022 - 05 - 14
print(datetime.date.today())
#👇️ 2022 - 05 - 14 13: 45: 57.409176
print(datetime.datetime.today())
Copied!from datetime
import datetime, date
#👇️ 2022 - 05 - 14
print(date.today())
#👇️ 2022 - 05 - 14 13: 45: 57.409176
print(datetime.today())
Copied!from datetime
import datetime as dt, date
#👇️ 2022 - 05 - 14
print(date.today())
#👇️ 2022 - 05 - 14 13: 45: 57.409176
print(dt.today())
Copied! import datetime "" " [ 'MAXYEAR', 'MINYEAR', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo' ] "" " print(dir(datetime))
Copied!from datetime
import datetime, date
#👇️[...'today'...]
print(dir(datetime))
#👇️[...'today'...]
print(dir(date))
but as you try to get a datetime fields the correct type should be datetime. ,I tried a really simple function to retrieve the current time into a field. Here is the code to my function,I am new to OpenERP development and I am trying to learn how to assign functions to fields so I can retrieve values based on my needs, but after following the manual or taking a look at the code from another modules I can't seem to get it right.,Odoo's unique value proposition is to be at the same time very easy to use and fully integrated.
I tried a really simple function to retrieve the current time into a field. Here is the code to my function
def get_text(self, cr, uid, ids, fields, arg, context):
#records = self.browse(cr, uid, ids)
#sale = 'Some Text'
dd = datetime.now()
return dd
And this is how I call the get_text
function
'varText': fields.function(get_text, obj = 'sim.student', method = True, store = False, type = 'string', string = 'Text')
Whatever I try in my get_text
function I always get an error like the following:
AttributeError: 'datetime.datetime'
object has no attribute 'get'