views:

877

answers:

15

This is probably too much to ask, but is there any language that does a really terrific job of representing time and date operations? I'll grant straight away that it's really hard to write a truly great time library. That said, are there any widespread languages that have one? Basically, I want something that handles time and date as comprehensively as modern regular expression libraries do their jobs. Everything I've seen so far in Python and Java omits one or more pretty important pieces, or makes too many things hard.

At least this should be intuitive to do:

  • find the number of days between two given dates, number of minutes between two given minute periods, etc.
  • add and subtract intervals from timestamps
  • allow simple conversion between timezones, with Daylight Saving Time changes by region automatically accounted for (given that there's an accurate supporting database of regional settings available)
  • get the period that a given timestamp falls into, given period granularity ("what calendar day is this date in?")
  • support very general string-to-date conversions (given a pattern)

Further, if there's a Java-style Calendar/GregorianCalendar setup, the general Calendar class should be accommodating toward subclasses if I need to roll my own Hebrew, Babylonian, Tolkien, or MartianCalendar. (Java Calendars make this pointlessly hard, for example.)

I am completely language-agnostic here. It's fine if the thing chokes on computing ambiguous stuff like "how many minutes are there between 2002 and next Valentine's Day?"

+2  A: 

I like .NET for this. It provides good Date/Time manipulation, with the DateTime and Timespan classes. However, most date/time stuff is fairly simple in any language which will give you a unix timestamp to work with.

J D OConal
.NET is OK, but some of the details are confusing.
EndangeredMassa
What did you find confusing? It was entirely intuitive to me. Maybe post a question here.
fatcat1111
+7  A: 

How about .NET's DateTime? (You can pick your language within the framework)

eed3si9n
+1  A: 

My personal favourite would be Ruby with Rails' ActiveSupport.

start_time = 5.months_ago.at_end_of_week 
end_time =  6.months.since(start_time)

It supports all of the features you've mentioned above with a similar DSL (domain specific language)

Redbeard
+4  A: 

Are you looking for something like PHPs strtotime? That will give you the unix timestamp of almost anything you can throw at it.

From the php site:

<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>

.NET's date classes require much more arcane fiddling with DateTimeFormatInfo and the like to parse date strings that aren't nearly as complicated as strtotime can handle.

PHP provides a DateTime class and a DateTimeZone class as of PHP 5, but they're both quite poorly documented. I still mostly use unix timestamps and the date, time and strtotime functions as I haven't fully come to grips with the new objects.

The following links attempt to flesh out DateTime and DateTimeZone a bit better:

Shabbyrobe
A: 

Aside: I wonder what kind of date data types are used in domains like archaeology! How does one represent historical dates for a time line applet?

Vulcan Eager
+1  A: 

I agree that Java SDK has a terrible implementation of datetime library. Hopefully JSR-310 will fix this problem in Java 7. If you can't wait for Java 7, I would recommend the precursor to JSR-310, Joda Time.

I agree that Ruby on Rails implementation in ActiveSupport is an excellent implementation that gets the basics right.

Alan
A: 

Ruby has excellent support, actually. Check out this page. Really great support for turning strings into dates, dates into strings, doing math on dates, parsing "natural language" strings like "3 months ago this friday at 3:45pm" into an actual date, turning dates into strings so you can do stuff like "Sam last logged in 4 days ago" or whatever...

Very nifty.

Cody Hatch
+3  A: 

For Java, I highly recommend the Joda Date/Time library.

MetroidFan2002
+3  A: 

You might want to check the Date::Manip Perl module on CPAN.

zoul
+2  A: 

PHP is not half bad.

// given two timestamps: $t1, and $t2:

// find the number of days between two given dates, number of minutes
// between two given minute periods, etc.  
$daysBetween = floor(($t2 - $t1) / 86400);  // 86400 = 1 day in seconds
$hoursBetween = floor(($t2 - $t1) / 3600);  // 3600 = 1 hour in seconds

// add and subtract intervals from timestamps  
$newDate = $t1 + $interval;

// allow simple conversion between timezones, with Daylight Saving Time
// changes by region automatically accounted for (given that there's an
// accurate supporting database of regional settings available)

// See PHP's Calendar functions for that
// http://au2.php.net/manual/en/book.calendar.php
// It not only supports basic stuff like timezones and DST, but also
// different types of calendar: French, Julian, Gregorian and Jewish.

// get the period that a given timestamp falls into, given period
// granularity ("what calendar day is this date in?")  
if (date("d", $t1) == 5)    // check if the timestamp is the 5th of the month
if (date("h", $t1) == 16)   // is it 4:00pm-4:59pm ?

// support very general string-to-date conversions (given a pattern) 

// strtotime() is magic for this. you can just type in regular english
// and it figures it out. If your dates are stored in a particular format
// and you want to convert them, you can use strptime()

You gotta give it some kudos for having a function to tell what date Easter is in a given year.

nickf
+2  A: 

For C++, there's Boost.Date_Time.

KTC
+3  A: 

There is a really cool programming language called Frink. It supports pretty much every unit ever invented, every physical or mathematical constant, timezones, bla bla bla …

It even has a web interface and a Java Applet.

Some of your challenges above:

  • find the number of days between two given dates, number of minutes between two given minute periods, etc.
    • How many days till Christmas: # 2008-12-25 # - now[] -> days
    • How long since noon: now[] - # 12:00 # -> minutes
  • add and subtract intervals from timestamps
    • When was my million minutes birthday: # 1979-01-06 # + 1 million minutes
  • allow simple conversion between timezones, with Daylight Saving Time changes by region automatically accounted for (given that there's an accurate supporting database of regional settings available)
    • When did the Beijing Olympics start in London: # 2008-08-08 08:08 PM China # -> London
  • support very general string-to-date conversions (given a pattern)
    1. Define a new date format: ### dd.MM.yyyy ###
    2. Parse: # 18.09.2008 #

Frink integrates nicely with Java: it can be embedded in Java applications and Frink programs can call Java code.

Jörg W Mittag
+1  A: 

I've been quite happy with the PEAR Date class for PHP. It does everything you're asking about, I believe, with the exception of multiple calendars, although there's also Date_Human, which could be a template for that sort of thing.

Also, I haven't worked with it yet, but Zend_Date, also for PHP, looks like it will work well for most of what you want. Zend_Date has the advantage of being based on Unix timestamps and the fact that the bulk of Zend Framework classes are intended to be easy to extend. So most likely you could quickly add support for your other date systems by extending Zend_Date.

Michael Johnson
+1  A: 

Perl's DateTime library is without a doubt the best (as in most correct) library for handling datetime math, and timezones. Everything else is wrong to varying degrees. (and I say this having written the above referenced blog post on PHP's DateTime/DateTimeZone libraries)

kellan
+1  A: 

PHP Date function is fantastic and has lots of helpful functions (link: php.net/date )

.NET is not bad in it's latest releases plus i like the fact you can add a reference to a secondary language and mix and match the code in your project. So you could use C# and VB functions within the same class.

Hope this is useful for you and good luck with your project :)

Andi