"Sven Bauhan" <svenbauhan / web.de> schrieb im Newsbeitrag news:3650khF4tato3U1 / individual.net... > Hi, > > I want to add a given time difference to an Time object. My first idea was > to calculate the number of seconds of this time difference. This works for > values like hours or days, but when I want to add a month, the number of > seconds depends on the number of days in the given month. > My second idea was to use a vector to add the time: > > @@sec = Vector[ 0, 0, 0, 0, 0, 1 ] > @@min = Vector[ 0, 0, 0, 0, 1, 0 ] > @@hour = Vector[ 0, 0, 0, 1, 0, 0 ] > @@day = Vector[ 0, 0, 1, 0, 0, 0 ] > @@week = Vector[ 0, 0, 7, 0, 0, 0 ] > @@month = Vector[ 0, 1, 0, 0, 0, 0 ] > @@year = Vector[ 1, 0, 0, 0, 0, 0 ] > > def next! ( distance = @@year ) > return @next if ( 0 == distance ) > now = Time.now() > values = Vector[ @next.year, @next.mon, @next.day, > @next.hour, @next.min, @next.sec ] > while ( @next.to_i() < now.to_i() ) > values += distance > @next = Time.local( *values.to_a ) > end > return @next > end > > But this can result in an "argument out of range" error. > > Has someone a simple solution for this problem? You can use the array representation of Time for some of the manipulations: >> t = Time.now => Sun Jan 30 23:47:24 GMT+1:00 2005 >> a = t.to_a => [24, 47, 23, 30, 1, 2005, 0, 30, false, "GMT+1:00"] >> a[4] += 1 => 2 >> a => [24, 47, 23, 30, 2, 2005, 0, 30, false, "GMT+1:00"] >> t2 = Time.local *a => Wed Mar 02 23:47:24 GMT+1:00 2005 Regards robert