Package: time-utility
Time Utility 0.2.1Time utility is a series of utility methods regarding date and time for python.Note: This package requires python 3.6 and higherInstallationpip3 install time_utilityImportfromtime_utilityimportTimeUtilityUsageThe following are the methods available in TimeUtility###is_naive(target_datetime)Checks if the given datetime object is naive.Parameters:target_datetime: datetime.datetime=> The target datetime to be checkedReturns:boolExample:fromtime_utilityimportTimeUtilityis_it_naive=TimeUtility.is_naive(TimeUtility.now())###is_aware(target_datetime)Checks if the given datetime object is awareExample:fromtime_utilityimportTimeUtilityis_it_aware=TimeUtility.is_aware(TimeUtility.now())####make_aware(target_datetime, timezone=pytz.utc)Returns an aware datetime object based on the naive target_datetimeParameters:target_datetime: datetime.datetime=> The target datetimetimezone[optional] => The preferred timezone. [default: UTC]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityfromdatetimeimportdatetimeaware=TimeUtility.make_aware(datetime.now())####now(timezone=pytz.utc)Returns an aware instance of the current datetimeParameters:timezone[Optional] => The preferred timezone [default: UTC]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilitynow=TimeUtility.now()####today(timezone=pytz.utc)Returns an aware instance of today's dateParameters:timezone[Optional] => The preferred timezone [default: UTC]returnsdatetime.dateExample:fromtime_utilityimportTimeUtilitytoday=TimeUtility.today()####get_date_start(timezone=pytz.utc, year = None, month = None, day = None)Returns the date and time of the beginning of the day with the given timezoneParameters:timezone[Optional] => The preferred timezone [default: UTC]year: int=> The target year. [default: None (will be set to current year)]month: int=> The target month. [default: None (will be set to current month)]day: int=> The target day. [default: None (will be set to current day)]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityday_start=TimeUtility.get_date_start(year=2020,month=5)####get_date_end(timezone=pytz.utc, year = None, month = None, day = None)Returns the date and time of the ending of the day with the given timezoneParameters:timezone[Optional] => The preferred timezone [default: UTC]year: int=> The target year. [default: None (will be set to current year)]month: int=> The target month. [default: None (will be set to current month)]day: int=> The target day. [default: None (will be set to current day)]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityday_start=TimeUtility.get_date_end(year=2020,month=5)####get_month_start(timezone=pytz.utc, year = None, month = None)Returns the date and time of the beginning of the month with the given timezoneParameters:timezone[Optional] => The preferred timezone [default: UTC]year: int=> The target year. [default: None (will be set to current year)]month: int=> The target month. [default: None (will be set to current month)]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityday_start=TimeUtility.get_month_start(year=2020,month=5)####get_month_end(timezone=pytz.utc, year = None, month = None)Returns the date and time of the ending of the month with the given timezoneParameters:timezone[Optional] => The preferred timezone [default: UTC]year: int=> The target year. [default: None (will be set to current year)]month: int=> The target month. [default: None (will be set to current month)]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityday_start=TimeUtility.get_month_end(year=2020,month=5)####get_year_start(timezone=pytz.utc, year = None)Returns the date and time of the beginning of the year with the given timezoneParameters:timezone[Optional] => The preferred timezone [default: UTC]year: int=> The target year. [default: None (will be set to current year)]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityday_start=TimeUtility.get_year_start(year=2020)####get_year_end(timezone=pytz.utc, year = None)Returns the date and time of the ending of the year with the given timezoneParameters:timezone[Optional] => The preferred timezone [default: UTC]year: int=> The target year. [default: None (will be set to current year)]returnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityday_start=TimeUtility.get_year_end(year=2020)####adjust_offset(original_datetime, offset, local_to_utc)Creates and returns a new datetime object using the given datetime object with the given offset in minutes.
The offset should be positive for timezones ahead of UTC (e.g. India) and negative for timezones before UTC (e.g. US)Note that the offset obtained in Javascript vianew Date().getTimezoneOffset()should be multiplied by -1.Parameters:original_datetime: datetime.datetime=> The original datetime to be adjustedoffset: int=> The offset in minuteslocal_to_utc=> Set to True if the original datetime is in local time and set to False if the original datetime is in UTC timereturnsdatetime.datetimeExample:fromtime_utilityimportTimeUtilityfromdatetimeimportdatetimeadjusted_datetime=TimeUtility.adjust_offset(TimeUtility.make_aware(datetime.now()),330,True)####get_period(year, month, day, period, offset = 0)Gets the start and the end datetime of a selected periodParameters:year: int=> The selected yearmonth: int=> The selected monthday: int=> The selected dayperiod: str=> The selected period. Your options are:TimeUtility.DAILY,TimeUtility.MONTHLY, andTimeUtility.ANNUALoffset: int[optional] => The offset from UTC to adjust the start and the end [default: 0]returns(datetime.datetime, datetime.datetime)Example:fromtime_utilityimportTimeUtilitystart,end=TimeUtility.get_period(year=2020,month=2,day=10,period=TimeUtility.MONTHLY,offset=330)####difference(large_time, small_time, time_span = TimeUtility.SECOND)Calculates the difference between two datetime object based on the given time-spanParameters:large_time: datetime.datetime=> The bigger datetime object of the twosmall_time: datetime.datetime=> The smaller datetime object of the twotime_span: str[Optional] => The time-span unit of time to calculate the difference. The options areTimeUtility.MICROSECOND,TimeUtility.SECOND,TimeUtility.MINUTE,TimeUtility.HOUR,TimeUtility.DAY. [default: TimeUtility.SECOND]returnsintExample:fromtime_utilityimportTimeUtilitydifference=TimeUtility.difference(TimeUtility.adjust_offset(TimeUtility.now(),120,False),TimeUtility.now(),TimeUtility.MINUTE)####is_leap_year(year = None)Checks if the given year is a leap year or notParameters:year: int[Optional] => The target year [default: None (will be set to current year)]returnsboolExample:fromtime_utilityimportTimeUtilityis_leap=TimeUtility.is_leap_year(year=2020)