March 9, 2022

datediff

I’ve been trying to quit smoking and I wanted to have some feedback on how much time passed after the last cigarette i smoked.

So i started developing a program to tell me how much time passed from a saved date and time in a human readable format.

The first thing I did was to write a python program to save and calculate the difference from the saved timestamp.

It’s pretty easy, just

difference = now - saved

then

seconds, rest = divmod(difference, 1000)
minutes, rest = divmod(rest, 60)

And so on.

The annoying thing is that when you get to months, every one has a different length, so I decided to do what I’ve should have done before.

Search if anybody has tackled this problem before.

Turns out someone has.

Enters Dateutils

So I’ve found out the Dateutils project.

It has a program that actually does just what I needed called datedff.

I use termux and I couldn’t find the program in the repositories.

So being open source, I built it and installed it locally from source.

I downloaded the latest source from the site, at the time it was version 0.4.9.

wget https://bitbucket.org/hroptatyr/dateutils/downloads/dateutils-0.4.9.tar.xz 

Extract it

tar -xvf dateutils-0.4.9.tar.xz

And then built and installed it following the instructions on the termux page for installing a program

cd dateutils-0.4.9
./configure --prefix="$PREFIX"
make
make install

I set the PREFIX environment variable to $PREFIX to install it in the correct directory in termux. The rest was just a classic install of a package from source, make and make install.

I got a bunch of warnings in compiling but it built and installed with no errors.

The script

After installing it writing the script was just reading a date from a file and getting the time differences from the current date using datediff.

I used the -f option (output format) with following format string

"%Y %m %d %H %M %S"

Some parsing and formatting and that’s it, more or less, you can find it here on envs.net.

© Mauro Scomparin 2020 - 2023

Powered by Hugo & Kiss.