Fixing Custom DateTime Formats into Unix Time in Python

in GitPlaitlast month

sketch15746947535507113640783183722075.png

Disclaimer: I'm not an Accounting expert and doesn't guarantee the accuracy of terms and formula used. I just want to get how much is my money, OK?

I have been working on Project ThunderGuava which is both a learning-process and an output-based project.

The idea is to convert a Google Sheet solution into an Android App.

Google Sheets

I have been using Google Sheets to manage my daily transactions, made it straightforfward by only using relevent data, namely:

  • datetime
  • transaction type (in/out)
  • transaction sub-type (rental, health, charity, etc.)
  • transaction name
  • transaction cost
  • status (pending/done)
  • wallet (bank1..N, wallet, digital wallet, etc.)
  • entity (person/company/org)
  • notes

Making reports is easy, I just sum all income and expenses, and get the difference to have the Net Value.

Google Sheets is powerful, but I always wanted to make it portable, accessing its 1K rows using a smartphone is hard.

Adding more features is possible, but again, I wanted it on an Android app.

Custom DateTime Formats

So I converted the all transactions to SimpList format (personal data serialization language) and the remaining task is to add a Backup & Restore feature—or is it?

I forgot, my date of transaction is in %Y%m%d which looks intuitive amd minimalistic, I could fix it in Google Sheets, but I wanted to do it in Python.

Unix Time

personal preference

For database format, I prefer Unix Time for datetime columns but in Integer datatype.

Unix time is a system for describing a point in time. It is the number of seconds that have elapsed since the Unix epoch, minus leap seconds.

Wikipedia

So how could I convert a custom datetime format %Y%m%d into Unix Time?

Play around here unixtimeconverter.io or use the converter to get the Unix Time of a special day.

I found a solution on StackOverflow by sberry by using strptime to parse custom datetime format into actual datetime

>>> import time
>>> import datetime
>>> dt = datetime.datetime.strptime('2012-02-09', '%Y-%m-%d')
>>> time.mktime(dt.timetuple())

I used the snippet and added it to my template Python library, creating a new Spyder project named dunix, short for DateTime to Unix, not intended to fight with whatever existing dunix product/services out there, it's just for personal use.

It could just be some util function instead of a separate library, but I wanted organize my tools so I could find it easily when I needed it.

Custom DateTime to Unix Time

The exported transaction data has over 1K lines and is grouped into units, I needed a way to ungroup, format, then regroup back to SimpList format.

You could do this in regex, or any other way, I prefer loops.

After reading the file, I looped over line by line, then split the units, the get the key and value of the datetime, and called the dunix.convert function the saved it back into SimpList format.

test_1.png

Instead of chnaging it via Google Sheets or computing and changing all 1K lines manually, I could just feed the file and get the file now all the datetime converted into Unix Time.

DateTime in YYYYMMDD format

Spent several minutes, to automate a task that can be done in sceonds on Google Sheets. Haha!

test_3.png

I just printed a message every time before the conversion of the %Y%m%d into Unix Time is called.

Python is powerful, and there are so many ways that you can leverage it no matter how big or small your projects are.

Unix Time

Like what I said in How Not to Scare Beginners, knowing what's the starting point sheds light that coding can be easier when a plan is laid out.

So define what's bugging you or your code, get the most simple question, "What is wrong?" or "What I want my code to do?" then search in online you might find a lot of simple solutions to choose from.

#CodeRunDebug

About Me

@oniemaniego is a test engineer, but outside work, he experiments at the kitchen, writes poetry and fiction, paints his heart out, or toils under the hot sun.

Onie Maniego was born in Leyte, PH. He grew up in a rural area with a close-knit community and a simple lifestyle, he is often visiting his father's orchards during summer and weekends, which has a great impact on his works.

🌅 After 4PM
👽 DeviantArt
📓 Goodreads
👻 Kids Hideout
✒️ The Paper Drafts


👨🏽‍💻👨🏽‍🍳👨🏽‍🎨👨🏽‍🌾
INTJ-A


Don't forget to vote, comment, and follow me.

Sort:  

We are in learning phase. Thanks for making a nice tutorial on python. We are looking for people like you in our community. Together we can grow and make this platform source of learning for others.

Your post has been submitted to be curated with @gitplait community account because this is the kind of publications we like to see in our community.

Join our Community on Hive and Chat with us on Discord.

[Gitplait-Team]

Congratulations @oniemaniego! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You received more than 900 upvotes. Your next target is to reach 1000 upvotes.

You can view your badges on your board And compare to others on the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!