I was considering this post about the expressiveness of the Ruby Date/Time functions, and was wondering if the same type of syntax could be duplicated in Scala. After about 20 minutes hacking around, here's what I came up with. I'm not sure I'm completely in love with the abstraction, but the method calls look the same.
Output: 2 weeks from now: Sat Jul 26 14:58:44 MST 2008 10 days from now: Tue Jul 22 14:58:44 MST 2008 5 minutes ago: Sat Jul 12 14:53:44 MST 2008 [ July 12, 2008: Message edited by: Garrett Rowe ]
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
I was surprised that the Calendar treats noon as 12 am and midnight as 12 pm.
A good workman is known by his tools.
Joined: Jan 17, 2006
I'll see your extensions and raise you some more sugary sweet syntax.
1 day ago: Sat Jul 12 02:16:38 MST 2008 Yesterday at 3:00: Sat Jul 12 03:00:00 MST 2008 30 days from now at midnight: Tue Aug 12 00:00:00 MST 2008 Tomorrow at 3 AM: Mon Jul 14 03:00:00 MST 2008 30 days from 8 hours from now: Tue Aug 12 10:16:38 MST 2008 30 days + 8 hours from now: Tue Aug 12 10:16:38 MST 2008 [ July 13, 2008: Message edited by: Garrett Rowe ]
Joined: Mar 05, 2008
Pretty slick, Garrett, and Marc. Thanks for showing that. And Garrett - greetings from a former Arizonan.
[Marc]: I was surprised that the Calendar treats noon as 12 am and midnight as 12 pm.
What makes you say that? The documentation for Calendar says otherwise:
"Although historically not precise, midnight also belongs to "am", and noon belongs to "pm", so on the same day, 12:00 am (midnight) < 12:01 am, and 12:00 pm (noon) < 12:01 pm"