Calendar Systems in 4.6

So what's new in 4.6 for Calendar Systems?  Well, not much visible really.  I had hoped to have the astronomical  calendars (Chinese, Islamic, etc) done for 4.6, but I'm still in requirements gathering for that so it's bumped to 4.7. Instead I've done some stuff to make date localization easy.  If you're looking for bling, you may as well skip to the next post (I'm sure someone has some nice Lenovo eye-candy to show off :-), but if you're an app coder tired of how awkward date localization can be then stick around.

Still here? Right, app coders, date localization in KDE is not exactly easy to use, is it? We provide Calendar System localization, which Qt doesn't, so anytime you want to use or display a date you should be passing the QDate through the KDE global locale calendar to get the correct results. That's easy enough for simple readDate() and formatDate() calls, but anything else is more obscure and long-winded, so a lot of apps don't bother. Take this simple KDE code for example:

QDate myDate = QDate::currentDate();
int dayNumber = KGlobal::locale()->calendar()->day( myDate );
QString dateString = KGlobal::locale()->formatDate( myDate, KLocale::IsoDate );

Compare that to the same calls in QDate:

QDate myDate = QDate::currentDate();
int dayNumber =;
QString dateString = myDate.toString( Qt::ISODate );

A far more natural way of doing it, right? So how about we make it that easy in KDE as well? Enter the new KLocalizedDate class:

KLocalizedDate myDate = QDate::currentDate();
int dayNumber =;
QString dateString = myDate.formatDate(
KLocale::IsoDate );

Way nicer.  It's basically a drop-in replacement for QDate whenever you need to localize the date, just change your QDate type to KLocalizedDate and most of your existing QDate api calls should just work (it's not derived from QDate, so you can't pass it around as a QDate or cast it). There's a couple of calls that have different names from QDate to emphasise that they work differently, the main ones being toString/fromString which are replaced by formatDate/readDate, but that's been standard in KLocale for a long time anyway.  A lot of the more useful KCalendarSystem api is there too, like the date maths, with many clean-ups and simplifications.  You can also change the Calendar System or Locale from the default global one.  Have a look, the apidox is still a work-in-progress but I'm making it as comprehensive as possible, and try it out in your code. Let me know if you have any issues with the API while we still have time to change it.

Just a note that I don't really intend for this class to be used in public library api for passing date values around, that's best left to a normal QDate for compatability, but once you have a QDate I hope localization is easier using KLocalizedDate.

In 4.7 I hope to find time for a KLocalizedDateTime that merges KLocalizedDate and KDateTime as a drop-in replacement for QDateTime. Longer term, I've been looking at QDate itself, and I can't see any BC problems that would prevent adding Calendar System support in Qt4 so I may have a crack at implementing it and see if Qt is interested (they should be, Win and Mac both support it, and it will help sell handsets into some of Nokia's strongest markets).

Also new in the api are some simple calls for firstDayOfMonth(), lastDayOfMonth(), firstDayOfYear() and lastDayOfYear(), easy enough dates to figure out in Gregorian, but very easy to get wrong when trying to localize to any Calendar System.

My thanks to Sebastian Trueg for pushing me to do this, I'd been thinking about it for a while before he showed me just how hard it was for the not-calendar-obsessed-like-me to get things right when it shouldn't be.

Right, that's it for Calendars, next blog Localization.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

great to hear, that kde is

great to hear, that kde is getting better from the inside, it's not only about eye candy! kde need to focus on stability and hardware requirements (speed!) i'm not saying kde is unstable and slow but there is a lot room to make improvements, and the biggest thing to do: finish "core" desktop, basic things have to just work (e.g. bluetooth wasn't good until bluedevil arived, we are waiting for new kdepim,  ark need developers because it have just on or two and a lot of bugs, activities someday will be usable, etc. etc.), i love kde but i really like news about improvments under the hood not about another useles kwin effect (kwin also would be better if gets faster)


so keep rockin' and can't wait to see beta1 today hopefuly :)

I only wanted to copy and paste the current date...

I'm having a great usability issue with the calendar. There is an option to copy the current date to the clipboard, but when I select it, I get instead a extra-large list of a myriad of different calendar systems, and my date gets relegated to a corner. I think your support for different calendar systems is great, beyond anything that has been known, but... can you make easier the most common use case for that function (copying the correct date according to user's preference), instead of trying to feed me the less common use case (copy multiple calendar formats for international publications or corner-cases)?

Thanks in advance.

Good point.

Good point, the explosion in Calendar Systems has made that feature a bit unweildy.  I'll try add a sub-menu for the other calendars, but I'll have to ask for an exceptionto teh translation freeze to do so.




I've fixed this for KDE SC 4.6, thanks for your input!



Thank you for this great work, i have a question: how can i get events list (i.g: holidays) for a specific country or religion ?

Best regards.


If it's just holidays you are after, then KHolidays in kdepimlibs is what you want, for some reason the api is midssing from but the file is at, in particular look at HolidayRegion::holidays() methods.  At the moment it will give you all the holidays for a country.  In 4.7 I hope to complete my work in adding holiday categories so you can request just religous or civil holidays.

It it's all calendar events you are after, then Akonadi is what youwant, and thats not my area :-)  If using plasma, then the calendar dataengine will give you everythign you need.

Good luck!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.