Chronos is one of the many Smalltalk-related blogs syndicated on Planet Smalltalk
χρόνος

Discussion of the Essence# programming language, and related issues and technologies.

Blog Timezone: America/Los_Angeles [Winter: -0800 hhmm | Summer: -0700 hhmm] 
Your local time:  
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

2007-09-23

How NOT to market your favorite programming language

My dislike of Java is probably at least as strong as that of Obie Fernandez, but his blog post attacking Java is simply not an acceptable way to debate the matter. [I was alerted to this post by Blaine Buxton].

The author is also flat out wrong on a few major points, most notably the importance of good dev tools. I've found that the better the programmer, the more good tools help: They amplify whatever you've got to give. And I'd take a great programmer using Java over a poor one using any other language. The "dirty little secret" of IT is that quality of personnel matters far more than tools.


2006-01-26

Comparative Examples: Chronos (and Smalltalk) vs. Java's Calendar Class

David Herron blogs about his attempt to use Java to show the local date and time of a widespread set of team members in Santa Clara CA, Beijing China, Bangalore India, Hyderabad India, St. Petersburg Russia and Dublin Ireland. His situation is becoming much more common, especially among IT professionals.

To see his Java code, and the resulting output, click here.

Here's the equivalent code using Chronos (and Smalltalk):


| universalNow printPolicy |
printPolicy := ConfigurableChronosPrintPolicy applying: #(showTimeZoneVerbosely).
universalNow := Timepoint utNow.
#('America/Los_Angeles' 'Asia/Calcutta' 'Europe/Moscow' 'Europe/Prague' 'Europe/Dublin'
'Europe/London' 'Asia/Tokyo' 'Asia/Novosibirsk' 'Asia/Hong_Kong' 'Asia/Shanghai'
'Asia/Seoul' 'America/Denver' 'America/New_York')
do: [:tzKey |
| localTimeNow |
localTimeNow := universalNow >> tzKey.
Transcript
cr;
show: (localTimeNow % printPolicy)]

The code above produces the following output:

2006-01-26T19:51:43.248535-08:00 (PST:America/Los_Angeles)
2006-01-27T09:21:43.248535+05:30 (IST:Asia/Calcutta)
2006-01-27T06:51:43.248535+03:00 (MSK:Europe/Moscow)
2006-01-27T04:51:43.248535+01:00 (CET:Europe/Prague)
2006-01-27T03:51:43.248535+00:00 (GMT:Europe/Dublin)
2006-01-27T03:51:43.248535+00:00 (GMT:Europe/London)
2006-01-27T12:51:43.248535+09:00 (JST:Asia/Tokyo)
2006-01-27T09:51:43.248535+06:00 (NOVT:Asia/Novosibirsk)
2006-01-27T11:51:43.248535+08:00 (HKT:Asia/Hong_Kong)
2006-01-27T11:51:43.248535+08:00 (CST:Asia/Shanghai)
2006-01-27T12:51:43.248535+09:00 (KST:Asia/Seoul)
2006-01-26T20:51:43.248535-07:00 (MST:America/Denver)
2006-01-26T22:51:43.248535-05:00 (EST:America/New_York)


To get output formatted as RFC 2822, just replace "% printPolicy" with "% #rfc2822". To get output formatted according to the user's default preferences, replace "% printPolicy" with "localePrintString".

With the following improvements, we can sort the output by time zone offset, and get much more readable output:


| universalNow printPolicy |
printPolicy := ConfigurableChronosPrintPolicy applying:
#(showDayOfWeekAbbreviation
useDayMonthYearOrder
useMonthAbbreviation
dateSeparator: $
dateAndTimeSeparator: $
hideSubsecondFraction
timeZoneSeparator: $
timeZoneElementSeparator: nil
showTimeZoneVerbosely).
universalNow := Timepoint utNow.
#('America/Los_Angeles' 'Asia/Calcutta' 'Europe/Moscow' 'Europe/Prague' 'Europe/Dublin'
'Europe/London' 'Asia/Tokyo' 'Asia/Novosibirsk' 'Asia/Hong_Kong' 'Asia/Shanghai'
'Asia/Seoul' 'America/Denver' 'America/New_York')
collect: [:tzKey | universalNow >> tzKey])
asSortedCollection:
[:localTimeA :localTimeB | localTimeA offset > localTimeB offset])
do: [:localTime | Transcript cr; show: localTime % printPolicy]


The improved code produces the following output:

Fri, 27 Jan 2006 14:29:43 +0900 (KST: Asia/Seoul)
Fri, 27 Jan 2006 14:29:43 +0900 (JST: Asia/Tokyo)
Fri, 27 Jan 2006 13:29:43 +0800 (CST: Asia/Shanghai)
Fri, 27 Jan 2006 13:29:43 +0800 (HKT: Asia/Hong_Kong)
Fri, 27 Jan 2006 11:29:43 +0600 (NOVT: Asia/Novosibirsk)
Fri, 27 Jan 2006 10:59:43 +0530 (IST: Asia/Calcutta)
Fri, 27 Jan 2006 08:29:43 +0300 (MSK: Europe/Moscow)
Fri, 27 Jan 2006 06:29:43 +0100 (CET: Europe/Prague)
Fri, 27 Jan 2006 05:29:43 +0000 (GMT: Europe/London)
Fri, 27 Jan 2006 05:29:43 +0000 (GMT: Europe/Dublin)
Fri, 27 Jan 2006 00:29:43 -0500 (EST: America/New_York)
Thu, 26 Jan 2006 22:29:43 -0700 (MST: America/Denver)
Thu, 26 Jan 2006 21:29:43 -0800 (PST: America/Los_Angeles)


Perhaps David should have considered using Joda Time, if he's determined to use Java.


2006-01-09

Chronos: A Few Comparative Examples

The home page for the Joda Date/Time Library presents some examples intended to demonstrate the "look and feel" of using Joda-Time. To see the Joda examples, click the link in the previous sentence.

Here are the equivalent examples using Chronos:

daysToNewYear: fromDate [Alternative 1]
    ^CalendarDuration days: (fromDate daysInYear - fromDate dayOfYear + 1)

daysToNewYear: fromDate [Alternative 2]
    ^CalendarDuration days: (fromDate daysUntil: (fromDate nextYear withDayOfYear: 1))

isRentalOverdue: datetimeRented [Alternative 1]
    "Answer whether a rental made on <datetimeRented> is now overdue."
    ^datetimeRented + 2.5 days < DateAndTime now

isRentalOverdue: datetimeRented [Alternative 2]
    "Answer whether a rental made on <datetimeRented> is now overdue."
    ^datetimeRented + (CivilDuration days: 2 hours: 12) < DateAndTime now

joinedInLastThreeMonths: datetimeJoined
    "Answer whether the <datetimeJoined> is within the past three months."
    ^(DateAndTime now withDuration: -3 months) includes: datetimeJoined

getBirthMonthText: dateOfBirth forLocale: localeKey
    "Answer the name of the month of <dateOfBirth> according to the locale identified by the <localeKey>."
    ^dateOfBirth
            forLocale: localeKey
            monthNameIfResolvable:
                [:monthName | monthName]
            ifNot:
                [dateOfBirth monthName]