<s> from __future__ import absolute_import , division , print_function <EOL> from datetime import datetime , date , timedelta <EOL> import sys <EOL> def identity ( x ) : <EOL> return x <EOL> def asday ( dt ) : <EOL> if isinstance ( dt , datetime ) : <EOL> return dt . date ( ) <EOL> else : <EOL> return dt <EOL> def asweek ( dt ) : <EOL> if isinstance ( dt , datetime ) : <EOL> dt = dt . date ( ) <EOL> return dt - timedelta ( days = dt . isoweekday ( ) - <NUM_LIT:1> ) <EOL> def ashour ( dt ) : <EOL> return datetime ( dt . year , dt . month , dt . day , dt . hour , tzinfo = dt . tzinfo ) <EOL> def asminute ( dt ) : <EOL> return datetime ( dt . year , dt . month , dt . day , dt . hour , dt . minute , <EOL> tzinfo = dt . tzinfo ) <EOL> def assecond ( dt ) : <EOL> return datetime ( dt . year , dt . month , dt . day , dt . hour , dt . minute , <EOL> dt . second , tzinfo = dt . tzinfo ) <EOL> def asmillisecond ( dt ) : <EOL> return datetime ( dt . year , dt . month , dt . day , dt . hour , dt . minute , <EOL> dt . second , dt . microsecond // <NUM_LIT:1000> , tzinfo = dt . tzinfo ) <EOL> if sys . version_info < ( <NUM_LIT:2> , <NUM_LIT:7> ) : <EOL> def total_seconds ( td ) : <EOL> """<STR_LIT>""" <EOL> return ( td . microseconds + <NUM_LIT> * ( td . seconds + <NUM_LIT> * <NUM_LIT> * td . days ) ) / <NUM_LIT> <EOL> else : <EOL> total_seconds = timedelta . total_seconds <EOL> unit_map = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : identity } <EOL> def truncate_year ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> return date ( dt . year // measure * measure , <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def truncate_month ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> months = dt . year * <NUM_LIT:12> + dt . month <EOL> months = months // measure * measure <EOL> return date ( ( months - <NUM_LIT:1> ) // <NUM_LIT:12> , ( months - <NUM_LIT:1> ) % <NUM_LIT:12> + <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> def truncate_day ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> days = dt . toordinal ( ) <EOL> days = days // measure * measure <EOL> return date . fromordinal ( days ) <EOL> oneday = timedelta ( days = <NUM_LIT:1> ) <EOL> def truncate_week ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> return truncate_day ( dt , measure * <NUM_LIT:7> ) <EOL> epoch = datetime . utcfromtimestamp ( <NUM_LIT:0> ) <EOL> def utctotimestamp ( dt ) : <EOL> """<STR_LIT>""" <EOL> return total_seconds ( dt - epoch ) <EOL> def truncate_minute ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> return asminute ( truncate_second ( dt , measure * <NUM_LIT> ) ) <EOL> def truncate_hour ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> return ashour ( truncate_second ( dt , measure * <NUM_LIT> ) ) <EOL> def truncate_second ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> d = datetime ( <EOL> dt . year , dt . month , dt . day , tzinfo = dt . tzinfo ) <EOL> seconds = total_seconds ( dt - d ) // measure * measure <EOL> return dt . utcfromtimestamp ( seconds + utctotimestamp ( d ) ) <EOL> def truncate_millisecond ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> d = datetime ( <EOL> dt . year , dt . month , dt . day , tzinfo = dt . tzinfo ) <EOL> seconds = total_seconds ( dt - d ) * <NUM_LIT:1000> // measure * measure / <NUM_LIT> + <NUM_LIT> <EOL> return dt . utcfromtimestamp ( seconds + utctotimestamp ( d ) ) <EOL> def truncate_microsecond ( dt , measure ) : <EOL> """<STR_LIT>""" <EOL> d = datetime ( <EOL> dt . year , dt . month , dt . day , tzinfo = dt . tzinfo ) <EOL> seconds = total_seconds ( dt - d ) * <NUM_LIT> // measure * measure / <NUM_LIT> <EOL> return dt . utcfromtimestamp ( seconds + utctotimestamp ( d ) ) <EOL> truncate_functions = { '<STR_LIT>' : truncate_year , <EOL> '<STR_LIT>' : truncate_month , <EOL> '<STR_LIT>' : truncate_week , <EOL> '<STR_LIT>' : truncate_day , <EOL> '<STR_LIT>' : truncate_hour , <EOL> '<STR_LIT>' : truncate_minute , <EOL> '<STR_LIT>' : truncate_second , <EOL> '<STR_LIT>' : truncate_millisecond , <EOL> '<STR_LIT>' : truncate_microsecond } <EOL> def truncate ( dt , measure , unit ) : <EOL> """<STR_LIT>""" <EOL> from blaze . expr . datetime import normalize_time_unit <EOL> unit = normalize_time_unit ( unit ) <EOL> return truncate_functions [ unit ] ( dt , measure ) </s>
