Dealing with time as a programmer can be tricky, especially if you take into account leap seconds, time-zone inconsistencies, culture influences, and more. This repository explores Python's great built-in modules such as time for time-related functions where dates are not needed. It also has a datetime module that supplies classes for manipulating dates and times. Calendar is another module that outputs calendars and provides functions using an idealized Gregorian calendar. We also look at dateparser, a third party Python package that packs a punch in terms of processing natural language.
You need a version of Python3.7 or above. I am currently using Python 3.9 as it is the most updated version as of mid March 2021. I had Python 3.6 installed and the .fromisoformat() method from datetime did not work. This is because I needed a later version of Python. All Python installations come with pip, a package manager, which will discussed further in this document. On Ubuntu/Linux, to uninstall outdated versions of Python.:
- sudo rm -rf /usr/bin/python2.x as well as python3.x
- sudo rm -rf /usr/lib/python2.x as well as python3.x
- sudo rm -rf /usr/local/lib/python2.x as well as python 3.x
updating Ubuntu:
-sudo apt-get update
Now download a python tgz file from https://www.python.org/downloads/ and unzip it and CD into it
./configure
make test
sudo make install
Python should be installed now. Check by running python
Python on other OS's are available here
Type python3
and you should be taken to the Python interpreter. You can try the following out for youself to get a feel for how the different methods are used.
The following shows the time module. Time in the time module is a datetime object. It has a method named time that returns the number of seconds since the UNIX epoch. As this file is being editted, that number is approximately 1616431766.3649557. This instant occured on Janruary 1st, 1970 and is what almost all computers count time from.
The datetime module is designed to make it less complicated to access attributes of the object related to dates, times, and timezones. These objects include datetime.date which stores the year, month, and day atrributes. datetime.time stores hour, minute, second, microsecond, and tzinfo (time zone information). datetime.datetime is a combination of date and time. The following shows how to create datetime instances:
Other methods that datetime provides are datetime.today() that creates a datetime instance with the current local date. datetime.now() creates a datetime.datime instance with the current local date and time. datetime.combine() combines instances of datetime.date and datetime.time into a single datetime.datetime instance. The following snippet shows an example.
Python also allows you to you create datetime instances from a string in ISO 8601 format using the .fromisoformat() method.
If your string is not in ISO-8401 format, you can always use the .strptime() method. This method uses a special mini-language to tell Python which parts of the string are associated with the datetime attributes. The following is an example:
Other ways of creating datetime instances involve third party packages like dateparser (see Setting up your dev environment for more details). Dateparser is a library which allows you to provide natural language inputs. The follwoing is an example.
Dateparser also includes the python-dateutil module. This module is quite useful, as it includes an interface to the IANA (the Internet Assigned Numbers Authorirty), an organization that maintains a database of all of the values of the time zone offsets. The following is an example:
In your terminal or command line prompt, type python --version
to see that you have the appropriate version of Python is installed.
Next, wherever you store your programming documents, make a folder and go into it with the following commands:
mkdir Dates
cd Dates
Then we are going to create an isolated virtual environment so that when we install third party packages, it won't take up too much space on your machine.
To make a new virtual environment folder, simply enter python3 -m venv venv
for Linux and python -m venv venv
for Windows. If you enter ls
or dir
you should see the venv folder. Also note, if you enter which pip3
, it will point to the directory of the virtual environment instead of the global environment. To activate your virtual environment, enter:
source ./venv/bin/activate
for Linux venv\Scripts\activate
for Windows. Enter deactive
to exit your virtual environemnt.
Enter your virtual environment. We are going to restore requirements so that the third-party dateparser module works. Then, we are going to install dateparser with the command - pip install -r requirements.txt
.
Using this method, as oppposed to manually entering pip3 install [package_name]
, makes sure that the desired third-party packages, with the desired version pinning, is explicitly being used. It also makes repeatibility possible and cleanups simple.
If you enter pip3 list
you will see that dateparser was installed as well as other secondary/transitive dependencies. It should look like this:
To clear up some of the space that these third party packages take up, simple run the command pip3 uninstall [package_name]
.
Use the command pip list --outdated
to view which packages are outdated, the current version and the most updated version number. To upgrade the package you can install a package manually using pip install package_name= 2.1.X
for exampple or you can use pip install --upgrade [package_name]
.
You are also welcome to create your own branch, make pull requests, and leave comments. I may merge your changes with the main branch if I see a change that is relevent.
Initially, I had a branch to do all my readme edits but I just kept commiting to my main branch so the changes are not well documented. But the main changes up to now have been editing the readme.
You can use datetime.now() to a stamp for each log.
datetime.datetime(2021, 3, 22, 16, 10, 6, 739554)
License type: MIT
The main source for the material comes from an article by Bryan Weber which can be found here.
Other related articles on the topic:
You can reach me at: dannymevs@hotmail.com