Waivio

Recommended Posts

Fixing Custom DateTime Formats into Unix Time in Python

2 comments

oniemaniego1.1 K4 years agoPeakD4 min read

https://files.peakd.com/file/peakd-hive/oniemaniego/Dam0Upxg-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.

https://files.peakd.com/file/peakd-hive/oniemaniego/ez6DC3oN-test_0.png

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.

https://files.peakd.com/file/peakd-hive/oniemaniego/uuNeYwJj-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.

https://files.peakd.com/file/peakd-hive/oniemaniego/2Z78kcSZ-test_02.png

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

https://files.peakd.com/file/peakd-hive/oniemaniego/VoIIP2yO-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.

https://files.peakd.com/file/peakd-hive/oniemaniego/pQ20BMx7-test_4.png

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


Curious about Crypto?
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.

https://files.peakd.com/file/peakd-hive/oniemaniego/WZnQAgwL-137904_tXg8twdd.pngOnie 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.

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


Not yet on Hive? Earn while blogging.
Sign Up

Comments

Sort byBest
AI
Waivio AI Assistant
How can I help you today?