Affichage des articles dont le libellé est Code. Afficher tous les articles
Affichage des articles dont le libellé est Code. Afficher tous les articles

samedi 16 juin 2018

Programming with dates is hard

Time and date is not a trivial problem in programming. You can expect different precision, handle different timezones and different format when you convert them from and to a string.

You may remember some issues related to date that appended in the IT world. Take Y2K, that obliged companies to check and fix the way they handled date in their program before the millennium. I guess it's been a costly procedure in some of the case in term of research, fix and testing. For non tech people though, it's a ridiculous case: in our daily life, we use time and dates casually without thinking about it.

Another sign that handling dates is not trivial: the plethora of library available. In Python, the standard module datetime does not handle time zones, so we have pytz. But as it's not human friendly, everyone got to use Arrow. Then Pendulum came out, claiming that arrow got it wrong and it became the new standard for community. Meanwhile, Kenneth Reitz (requests, pipenv,…) released Maya to do the same thing. Help, we're lost!

It's not specific to Python. In Java, Joda-Time has been the preferred library to handle dates, rather than the standard library. Then Java 8 came out with a new java.time package that can be used instead of Joda. I worked recently in a code base that mixed both libraries. It inspired me this post.

On the other hand, when you have chosen a library, getting the current date is super simple, using a call like mytimelib.today(). Cool. How about testing then?

Actual time does not belong in your system, it's a dependency, bound to the real world. If your business domain relies on date for some sort, you may avoid to use your date library directly.

When I need to integrate business logic based on date, I use my own Date and Calendar classes which allows me to:

  • Get the precision I want for comparison. For instance, my domain can rely on lapsing dates with month precision and I don't need more.
  • Have a reference implementation. My dates can be created from instances created with different date libs.
  • Get default formats for date as String
  • Create a fake calendar for testing that gives me control on current date generation. So I do not need to scratch my head when I have to compare dates in tests.

In the past I worked on a project for an insurance company. We naively used mylib.today() calls all over the place. When the main features were implemented and tested, business people told us that they wanted to test the system behaviour when contracts get on term. To do so they wanted to be able to alter system current date.

Guess who replaced all the calls by injecting a custom Calendar and provided a service endpoint that allow users to modify the current date for testing…

So pay attention to your business domain. If it relies on dates in some sort, you'd better treat them as an external dependency. Then you'll be able to use the lib you want, and even to change it seemlessly if it get oldfashionned.

mercredi 28 octobre 2015

State of my personal projects

I feel like to self promote my free time work.

As you may have seen, I’ve personal projects rotting on my public github repository. Here is a short presentation of them and their status.

what2use4

wat2use4 is a instant poll service. You post a question, send the URL to your fellow friends and let them give you advices. They can vote for a spectific term, prefixing it with a # sign (how, that’s original!). You can see the top 5 popular terms in a result box and the 10 last advices in the result timeline. Results are updated on the poll page at every vote. It’s based on Node, MongoDB and SocketIO. This is my hipster attempt, though it’s based on already old tech!

I created it because I often have a need to ask a question to fellow friends and want a quick feedback, so that I can make a decision quickly.

dotfiles

I store my config files, mainly for vim, zsh and tmux, in dotfiles project, so that I have a quick access to them if I have to use a new computer. There’s nothing sophisticated here, because as I don’t edit them that often, I do not want to scratch my head for hours when I read them.

event_sourcing_example

event_sourcing_example stores the source code that goes with these articles.

theRoofIsOnFire

theRoofIsOnFire is a project I started to train myself with Django. Its goal is to help you to use burndown charts to visualize what the remaining amount of work needed to get your projects done. Physical charts are cumbersome to update daily and MS Excel charts are boring. Here, the chart is rendered in Svg. The UI was fun to make but I guess I had too big expectations regarding my available time. It works by the way.

Initially, I wanted to build a Saas app, gain money with it, but I preferred publish it because I can’t find the energy to make it viable for the moment. And I think it deserves a complete rewrite.

vim-teamforge

vim-teamforge is essentially a syntax file to edit teamforge wiki pages in Vim. I have a problem with link escape using ~ that I did not fix yet.

yapong

I’m so proud of yapong! This is a stupid clone of classical Pong game, made with pygame toolkit. By the way, it make me learned the logic behind 2D video games, and I had a lot of fun programming it. I have a idle project of a shoot them up also, but I have difficulties to make pygame work well on OSX. I think I gonna go into Phaser to do it.

GAabo

GAabo is an application make with Python and WxWidget. I used it to manage subscription for a French magazine called Grandir Autrement. I happened to volunteer for it, so I can say I ate my own dog food after scratching my own itch! Every month, to send the issues, the app generates the listing to produce routing tags.

I’m proud of this one to since it proved myself that I could create an app from A to Z. This particular project made me realize that I am a genuine programmer. OMG, I’m sure that the code is terrible!

That’s it for today. Feel free to play with this stuff if you want to!

mercredi 19 octobre 2011

Github

Rapidos, pour dire que j'ai ajouté mon profil github dans les liens →

samedi 13 août 2011

Mon premier client

Ce qui est bien quand on est développeur, c'est qu'on peut soi même créer un logiciel qui va nous simplifier la vie.

Pour ma part, comme je m'occupe encore de Grandir Autrement, je croulais un peu sous les factures à générer. J'ai donc fait un bout de code qui me permet de le faire à la chaine plus rapidement.

C'est très productif d'être son propre client. Pour pousser la réflexion, on pourrait se dire que chaque développeur devrait travailler un certain temps sur les application qu'il développe. Je suis sûr que ça ferait évoluer les mentalités !

Ce qui est bien pour partager ce genre de chose, c'est d'avoir des sites comme github !

Car ça y est, moi aussi je partage mon code !

samedi 28 novembre 2009

Playing with Perl

I'm a Perl beginner. Actually, what I read here and there let me think Perl is a pretty cool language. It does not need compilation, can be compatible with *nix and Windows. It has a powerful regex engine and quite a simple syntax. Finally, the large amount of modules available on CPAN can extend it in an amazing way.

The first real thing I made with is a tool to convert gmail contact list export file to something I can enter in my .muttrc (yeah, I use mutt with imap to retrieve my mail on gmail).

Data processing was quick to write. But it was hard to make it work. After a couple of hours (gimme a break, I'm a n00b, remember !), I figured out the contact.csv file was encoded in UTF16... Wait, what?! I've never seen this encoding before. Then, I was obliged to use iconv to preprocess the file and everything went fine!
Another trick is to check for unicity of mutt aliases.

If you're interested source is here. Obviously, it needs improvements.