Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to initialize time() object in python

I am trying to initialize a time object like this:

t = datetime.time(0,0,0)

but I am getting this error:

descriptor 'time' requires a 'datetime.datetime' object but received a 'int'

I have these things imported

import datetime
from datetime import datetime, date, time
import time

They seem a bit redundant so I am wondering if this is what is causing the problem

I am also using the strptime method and the combine method

    earliest = datetime.combine(earliest, t)
    value = datetime.strptime(value, format)
like image 387
Santiago Avatar asked Sep 05 '12 23:09

Santiago


People also ask

How do you initialize a time variable?

There are two ways to initialize the DateTime variable: DateTime DT = new DateTime();// this will initialze variable with a date(01/01/0001) and time(00:00:00). DateTime DT = new DateTime(2019,05,09,9,15,0);// this will initialize variable with a specific date(09/05/2019) and time(9:15:00).

How do you create a time object?

The time objects can be created, by calling the constructor of time class without providing any parameters. A time object without any parameters will have its hour, minute, second and microsecond initialized to zero.

How do you initialize a datetime variable in Python?

Creating Date Objects To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.


3 Answers

You can create the object without any values:

>>> import datetime
>>> datetime.time()
datetime.time(0, 0)

You, however, imported the class datetime from the module, replacing the module itself:

>>> from datetime import datetime
>>> datetime.time
<method 'time' of 'datetime.datetime' objects>

and that has a different signature:

>>> datetime.time()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' of 'datetime.datetime' object needs an argument
>>> datetime.time(0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'

Either import the whole module, or import the contained classes, but don't mix and match. Stick to:

import datetime
import time

if you need both modules.

like image 153
Martijn Pieters Avatar answered Oct 05 '22 09:10

Martijn Pieters


The constructor for time is:

class datetime.time(hour[, minute[, second[, microsecond[, tzinfo]]]])

(from http://docs.python.org/library/datetime.html#time-objects)

This works for me:

In [1]: import datetime

In [2]: t = datetime.time(0, 0, 0)

In [3]: print t
00:00:00
like image 21
sandesh247 Avatar answered Oct 05 '22 08:10

sandesh247


It's the fact that you're importing a conflicting datetime from datetime. You probably meant time, except you're also importing a conflicting time. So how about:

import datetime as dt

and

t = dt.time(0, 0, 0)
like image 32
Ry- Avatar answered Oct 05 '22 08:10

Ry-