Fixing Custom DateTime Formats into Unix Time in Python
2 comments
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.
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.
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.
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.
Spent several minutes, to automate a task that can be done in sceonds on Google Sheets. Haha!
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.
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.
Buy and Sell on Coinbase
Get Started
About Me
@oniemaniego is a test engineer, but outside work, he experiments in 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. |
Sign Up
Comments