<s> from __future__ import absolute_import , division , print_function <EOL> from . expressions import ElemWise , schema_method_list , method_properties <EOL> import datashape <EOL> from datashape import dshape , isdatelike , isnumeric <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:date>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:date>' , '<STR_LIT>' , '<STR_LIT:time>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class DateTime ( ElemWise ) : <EOL> """<STR_LIT>""" <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( str ( self . _child ) , type ( self ) . __name__ . lower ( ) ) <EOL> def _schema ( self ) : <EOL> return dshape ( self . _dtype ) <EOL> @ property <EOL> def _name ( self ) : <EOL> return '<STR_LIT>' % ( self . _child . _name , self . attr ) <EOL> @ property <EOL> def attr ( self ) : <EOL> return type ( self ) . __name__ . lower ( ) <EOL> class Date ( DateTime ) : <EOL> _dtype = datashape . date_ <EOL> def date ( expr ) : <EOL> return Date ( expr ) <EOL> class Year ( DateTime ) : <EOL> _dtype = datashape . int32 <EOL> def year ( expr ) : <EOL> return Year ( expr ) <EOL> class Month ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def month ( expr ) : <EOL> return Month ( expr ) <EOL> class Day ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def day ( expr ) : <EOL> return Day ( expr ) <EOL> class Time ( DateTime ) : <EOL> _dtype = datashape . time_ <EOL> def time ( expr ) : <EOL> return Time ( expr ) <EOL> class Hour ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def hour ( expr ) : <EOL> return Hour ( expr ) <EOL> class Minute ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def minute ( expr ) : <EOL> return Minute ( expr ) <EOL> class Second ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def second ( expr ) : <EOL> return Second ( expr ) <EOL> class Millisecond ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def millisecond ( expr ) : <EOL> return Millisecond ( expr ) <EOL> class Microsecond ( DateTime ) : <EOL> _dtype = datashape . int64 <EOL> def microsecond ( expr ) : <EOL> return Microsecond ( expr ) <EOL> class UTCFromTimestamp ( DateTime ) : <EOL> _dtype = datashape . datetime_ <EOL> def utcfromtimestamp ( expr ) : <EOL> return UTCFromTimestamp ( expr ) <EOL> units = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> unit_aliases = { <EOL> '<STR_LIT:y>' : '<STR_LIT>' , <EOL> '<STR_LIT:w>' : '<STR_LIT>' , <EOL> '<STR_LIT:d>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT>' , <EOL> '<STR_LIT:h>' : '<STR_LIT>' , <EOL> '<STR_LIT:s>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def normalize_time_unit ( s ) : <EOL> """<STR_LIT>""" <EOL> s = s . lower ( ) . strip ( ) <EOL> if s in units : <EOL> return s <EOL> if s in unit_aliases : <EOL> return unit_aliases [ s ] <EOL> if s [ - <NUM_LIT:1> ] == '<STR_LIT:s>' : <EOL> return normalize_time_unit ( s . rstrip ( '<STR_LIT:s>' ) ) <EOL> raise ValueError ( "<STR_LIT>" % s ) <EOL> class DateTimeTruncate ( DateTime ) : <EOL> __slots__ = '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' <EOL> @ property <EOL> def _dtype ( self ) : <EOL> if units . index ( '<STR_LIT>' ) >= units . index ( self . unit ) : <EOL> return datashape . date_ <EOL> else : <EOL> return datashape . datetime_ <EOL> @ property <EOL> def _name ( self ) : <EOL> return self . _child . _name <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . _child , self . unit , self . measure ) <EOL> def truncate ( expr , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if not args and not kwargs : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> if args : <EOL> if kwargs : <EOL> raise TypeError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( args , kwargs ) ) <EOL> measure , unit = args <EOL> else : <EOL> [ ( unit , measure ) ] = kwargs . items ( ) <EOL> return DateTimeTruncate ( expr , measure , normalize_time_unit ( unit ) ) <EOL> schema_method_list . extend ( [ <EOL> ( isdatelike , set ( [ year , month , day , hour , minute , date , time , second , <EOL> millisecond , microsecond , truncate ] ) ) , <EOL> ( isnumeric , set ( [ utcfromtimestamp ] ) ) <EOL> ] ) <EOL> method_properties |= set ( [ year , month , day , hour , minute , second , millisecond , <EOL> microsecond , date , time , utcfromtimestamp ] ) </s>
