Posts

Showing posts from August, 2019
How to install python package in directory/folder If do you want to install any python module in your local directory instead of python site-package lib then you have do this. Create a directory/folder sudo mkdir <dir_name> sudo chmod 777 <dir_name> Install package with command sudo pip install boto3  -t <dir_name>/ or sudo pip install -t <dir_name>/ boto3 boto3 will be install inside in <dir_name>/ let suppose your directory is located in /var/www/<dir_name> and your .py file is location in /var/www/<test.py> How to use boto3 package in your test.py file import os path = os . path . dirname ( os . path . realpath ( __file__ )) user_home = os . environ [ "HOME" ] os . environ [ "PYTHONPATH" ] = path + '/<dir_name>' import sys sys . path . append ( path + '/<dir_name>' ) import boto3
Timezone in python import pytm, datetime Print all time zone with the help of pytm pytz.all_timezones or  for tz in pytz . all_timezones : print (tz) UTC Date Time date_time_utc = datetime.datetime.utcnow() Create timezone object america_st_johns = pytz.timezone('America/St_Johns') Convert UTC to other TimeZone local_dt = date_time_utc.replace(tzinfo=pytz.utc).astimezone(america_st_johns) Output # datetime.datetime(2019, 8, 20, 13, 49, 18, 905051, tzinfo=<DstTzInfo 'America/St_Johns' NDT-1 day, 21:30:00 DST>) Remove tzinfo from this object date_time.replace(tzinfo = None) Output # datetime.datetime(2019, 8, 20, 13, 51, 38, 177591)
Calculate tow date difference in python from datetime import datetime, time def date_diff_in_seconds(date2, date1):   timedelta = date2 - date1   return timedelta.days * 24 * 3600 + timedelta.seconds def calculate_seconds(seconds): minutes, seconds = divmod(seconds, 60) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) return (days, hours, minutes, seconds) #Specified date first_date = datetime.strptime('2019-07-19 01:00:00', '%Y-%m-%d %H:%M:%S') #Current date second_date = datetime.now() print("\n%d day(s), %d hour(s), %d minute(s), %d second(s)" % calculate_seconds(date_diff_in_seconds(second_date, first_date)))
How to use Date Time in python # Import datetime module import datetime from datetime import timedelta, datetime Today Date print((datetime.now()).replace(hour=0, minute=0, second=0, microsecond=0)) print(datetime.today()) date = datetime.today() print(date) print(date.day) print(date.year) print(date.month) print(date.hour) print(date.minute) print(date.second) print(date.microsecond) Create Static Date time object datetime(2011, 07,07) Python date object to Date str print(datetime.today().strftime('%Y-%m-%d %H:%M:%S') ) print(datetime.today().strftime('%Y-%m-%d')) Python date string to date object print( datetime.strptime('20/02/2018','%d/%m/%Y') ) print(datetime.strptime('20/02/2018','%d/%m/%Y')) Add Date and time in Date object to_date = datetime.today() + timedelta(days=1, hours=5, minutes=30) datetime.now().strftime('%Y%m%d%H%M%S') change timespan to time #timespan totsec = 14356 h =