Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

about python datetime type

What's the equivalent type in types module for datetime? Example:

import datetime
import types
t=datetime.datetime.now()
if type(t)==types.xxxxxx:
    do sth

I didn't find the relevent type in types module for the datetime type; could any one help me?

like image 965
mlzboy Avatar asked Oct 05 '10 02:10

mlzboy


People also ask

What is datetime in Python used for?

datetime in Python is the combination between dates and times. The attributes of this class are similar to both date and separate classes. These attributes include day, month, year, minute, second, microsecond, hour, and tzinfo.

What is a datetime?

The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar. Time values are measured in 100-nanosecond units called ticks.

What is a datetime object?

date Objects. A date object represents a date (year, month and day) in an idealized calendar, the current Gregorian calendar indefinitely extended in both directions. January 1 of year 1 is called day number 1, January 2 of year 1 is called day number 2, and so on. 2 class datetime. date (year, month, day)


1 Answers

>>> type(t)
<type 'datetime.datetime'>
>>> type(t) is datetime.datetime
True

Is that the information you're looking for? I don't think you'll be able to find the relevant type within the types module since datetime.datetime is not a builtin type.

Edit to add: Another note, since this is evidently what you were looking for (I wasn't entirely sure when I first answered) - type checking is generally not necessary in Python, and can be an indication of poor design. I'd recommend that you review your code and see if there's a way to do whatever it is you need to do without having to use type checking.

Also, the typical (canonical? pythonic) way to do this is with:

>>> isinstance(t, datetime.datetime)
True

See also: Differences between isinstance() and type() in python, but the main reason is that isinstance() supports inheritance whereas type() requires that both objects be of the exact same type (i.e. a derived type will evaluate to false when compared to its base type using the latter).

like image 58
eldarerathis Avatar answered Oct 13 '22 22:10

eldarerathis