Adding Ansi/Integer Date Support to Javascript

A very common way to store and work with dates is the usages of Integers as dates. In the integer the date is stored as YYYYMMDD. Converting from an between the different date formats is not nice if you are using normal functions. Also the Dutch date end user date format is "dd/mm/YYYY"

Well javascript prototypes to the rescue!
Inspired by the "Prototype Library" and Ruby ( on Rails ), I've managed to add nicer conversion support between different types.

Some examples of usages

20070212.to_date().to_s()  // Results in  12-02-2006
"24-02-2006".to_date()  // Results in a normal date object

var d = Date.create( 20060102 )   // Creates a Date object with the given
d.add( { months: 5, days: 2 } ).to_s() // Results in 04-06-2006

// add custom formatters with
Date.FORMATS[ 'period' ]  = function(d) {  return d.getFullYear() + "-" + d.getMonth(); }
( new Date() ).to_s( "period" )     // Results in  2007-08

var date1 = Date.create( 20070101 )
date1.diff( Date.now(), 'months')    // 8
... etc ...

Adding extra methods to the Date, String and Number objects can be done like this:

// for example the "to_s" method
Date.prototype.to_s = function( format ) {
if( !format ) format = 'default'
if( !Date.FORMATS[ format ] ) throw "Date format niet gevonden!";
return Date.FORMATS[ format ]( this );
}

// method for converting a number to a date
Number.prototype.to_date = function() {
var v = this.valueOf();
if( v == 0 ) return null;
return Date.new( v );
}

Some problems and frustrations I've encountered:

  • Stupid IE, doesn't allow to define setter en getter properties. The ECMA standard defines the following method for defining setters. The code below works in Mozilla Firefox. You can get the 'integer' date by using ( new Date() ).ansidate
    Date.prototype.ansidate getter function() { return this.getFullYear() * 10000 + (this.getMonth()+1) * 100 + this.getDate() } ;
    

    Firefox also supports the following legacy method for defining getters. This support has been there for quite a long time.

    Date.prototype.__defineGetter__( "ansidate", function() { return this.getFullYear() * 10000 + (this.getMonth()+1) * 100 + this.getDate() } )
    
  • null isn't an object so I cannot define a "to_date()" method for this object.

On request I will send you the code of the Date library. Maybe I will release it, but I needs a LOT of refinement!

Rails: unit test fixture_path NameError.

Last day of my Holiday :(
Trying to write a unit test for my Rails application, I constantly got the error message below:


NameError: undefined local variable or method `fixture_path' for #<LessonTest:0x9a611f8>
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.13.3/lib/action_controller/test_process.rb:452:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:593:in `load_fixtures'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:538:in `setup_with_fixtures'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:575:in `setup'

Very strange: the Unit Test I've written was generated and exactly the same as shown in the example in the "Agile Web Development with Rails 2nd Edition" book.


require File.dirname(__FILE__) + '/../test_helper'
class LessonTest < Test::Unit::TestCase
def test_truth
assert true
end
end

Well I found a solution, but I still haven't found the reason WHY this is happening. You can see this is as a temporary hack.

In your: "/test/test_helper.rb" add the following method:


def fixture_path
File.dirname(__FILE__) + "/fixtures/"
end

Ruby on Rails automatic reload of libraries

Currently I'm busy developing a Rails Application. I'm busy developing some general classes that aren't part of a model/controller or view. I placing these classes in the 'lib' directory.
Things are fine ... well not quite ...

The problem while developing is the libraries aren't reloaded on each request. This is very frustrating when you are trying to write a library.

Some things I found (but don't seam to work):

Rest the application in the console. Well the application reset, but not the libraries.

$ ./script.console
Loading development environment...
&gt;&gt; Dispatcher.reset_application!

I assume the libraries directory is loaded in the ruby booting class loader. ( I'm still thinking Java ). So when Webrick starts these libs are loaded.
The models and controllers etc. are loaded by the rails class loader.

Somehow I need to reload the library in the rails class loader space.... Doesn somebody have a solution!?
Perhaps I should find another directory...

I will keep you informed if I find a solutions.

---

1 august 2007: Well found a solution for the problem, I even wonder if it really was a problem..... (shame on me)
If I place the class "MyOwnClass" in the file "lib/my_own_class.rb" without including the source file. (This isn't required, because Rails automatically includes it) it seems to be refreshed every time I do a request...

Nice.. !

Researching Mantis Anonymous Bug Reporting

I've decided to use Mantis Bugtracker as the bugtracking tool for libhttpd. This is the tool I already was using before placing it on sourceforge. And I've got pretty good experience with it.

There's one problem, I would like to make others post bug reports directly into Mantis. The problem is that Mantis doesn't have a Captcha plugin for submitting bug reports.

For the time being It's not allowed to sent bugreports directly without registering.

You can find the bugtracker (currently empty) at http://bugs.gamecreatures.com/

Issue tracker Sourceforge isn’t very Nice

I don't like the issue tracker that's available at sourceforge. Some problems it has:

  • Very short Title. Much to short for a descent description.
  • Deleting categories isn't possible.
  • Very limiting in descriptions etc.
  • I'm missing the features mantis has..

I need to setup a bugtracking system for libhttpd myself