February 5, 2019

Substrings Working on sqlTime

Subject has the tl;dr. Here are the details. I wanted to be able to type sqlTime Madrid and get the local time. However, the actual argument I needed to use for this was Europe/Madrid. That's a bit annoying.

So, as with most of my passion projects, annoyance is the mother of invention. First things first, how would I handle this. Regular expressions came to mind. Shortly thereafter PCRE popped into my head. This library provides Perl-compatible regular expressions in C. It's open source and its license is acceptable. However, I determined it to be too heavy to incorporate for what is only implementing substring matching.

Then I found that postgres, which the system is built on, supports a plethora of functions for matching strings. Problem solved?

Perhaps not, I then tried the regex library from 4.4 BSD, which is to be found everywhere I'd use this setup. Again, it was overkill.

Finally, I had un ballon du rouge in me and went back to first principles. What did I aim to accomplish here? Essentially I wanted to allow the city name instead of having to specify the continent. Since the city name would certainly be in the timezone name, I needed a substring function. man -k substring on my system yielded strstr, strcasestr, strnstr, strcasestr_l(3) - locate a substring in a string. char *strcasestr(const char *big, const char *little); is the provided prototype for the case insensitive substring function; strstr is the case sensitive one. And the functions locates the first occurrence of the null-terminated string little in the null-terminated string big. and return a pointer to the start of the substring and null if it's not found. A few if statements later and sqlTime Madrid was returning the correct output. I then committed the code, updated the gist, wrote this post, and decided to get to bed. If you'd like to try it out, leave me your platform and I'll send you a binary.

No comments:

Post a Comment