Not signed in (Sign In)

Vanilla 1.1.8 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthorcsuski
    • CommentTimeAug 27th 2009
     
    Concerning Time.addSeconds(double)

    I have a Time object representing the current time and a whole bunch of other times stored as doubles representing JulianDays. So I have been using addSeconds to change the time, since there is no setTime based on Julian Days. For example:

    Time time = new Time();
    double nextTime = someJulianDay;
    time.addSeconds( (nextTime - time.getJulianDate())*24*60*60);

    and getting a weird results. This is because it is converting it to milliseconds and then changing it to an int.

    1 day = 86,400,000 ms
    The max int value is 2,146,483,647, which means you can never add more than about 24.8 days using addSeconds().

    Something worth noting.
    • CommentAuthorcsuski
    • CommentTimeAug 27th 2009
     
    Heres my fix:

    public void addSeconds(double seconds)
    {
    if(seconds > Integer.MAX_VALUE) {
    Double hours2Add = new Double(seconds / (60 * 60));
    //hours must be less than 2,147,483,647 which is about 89,478,485
    //days, that should be plenty
    currentTime.add(Calendar.HOUR, hours2Add.intValue());
    seconds = seconds - hours2Add.intValue() * 60 * 60;
    }

    int seconds2Add = new Double(seconds).intValue();
    int millis2Add = new Double(Math.round(seconds - seconds2Add)*1000).intValue();
    currentTime.add(Calendar.SECOND, seconds2Add);
    currentTime.add(Calendar.MILLISECOND, millis2Add);

    // update other time formats
    updateTimeMeasures();
    }
    • CommentAuthorcsuski
    • CommentTimeAug 27th 2009
     
    Whoops missed a set of parens, corrected:

    public void addSeconds(double seconds)
    {
    if(seconds > Integer.MAX_VALUE) {
    Double hours2Add = new Double(seconds / (60 * 60));
    //hours must be less than 2,147,483,647 which is about 89,478,485
    //days, that should be plenty
    currentTime.add(Calendar.HOUR, hours2Add.intValue());
    seconds = seconds - hours2Add.intValue() * 60 * 60;
    }

    int seconds2Add = new Double(seconds).intValue();
    int millis2Add = new Double(Math.round((seconds - seconds2Add)*1000)).intValue();
    currentTime.add(Calendar.SECOND, seconds2Add);
    currentTime.add(Calendar.MILLISECOND, millis2Add);

    // update other time formats
    updateTimeMeasures();
    }


    Here is the modified main used to test it:

    public static void main(String args[])
    {
    Time t = new Time(2007,9,1,11,59,0.0);
    System.out.println("Jul Date:" + t.getJulianDate() + ", string:" + t.getDateTimeStr());
    t.addSeconds(120); // add 120 sec
    System.out.println("Jul Date + 120 sec:" + t.getJulianDate()+ ", string:" + t.getDateTimeStr());
    t.addSeconds(60*24*60*60); // add 60 DAYS
    System.out.println("Jul Date + 60 days:" + t.getJulianDate()+ ", string:" + t.getDateTimeStr());
    t.addSeconds(2.5); // add 5 second
    t.addSeconds(2.5);
    System.out.println("Jul Date + 5 seconds:" + t.getJulianDate()+ ", string:" + t.getDateTimeStr());
    t.add(Time.HOUR,12);
    System.out.println("Jul Date + 12 hour:" + t.getJulianDate()+ ", string:" + t.getDateTimeStr());
    }
    • CommentAuthorsgano
    • CommentTimeAug 30th 2009
     
    Nice csuski! Thanks for the fix -- yeah, I had always just added small increments and didn't think about larger increments - but that could cause some big problems.
    • CommentAuthorcsuski
    • CommentTimeSep 3rd 2009
     
    I've also found that a copy constructor to come in very handy in the Time object

    /**
    * Potected copy Constructor
    * @param that a Time object
    */
    protected Time(Time that) {
    this(that.get(YEAR), that.get(MONTH)+1, that.get(DATE),
    that.get(HOUR_OF_DAY), that.get(MINUTE), that.get(SECOND));
    }

    /**
    * Implement cloneable by using the protected copy constructor.
    * @return A deep copy of this Time object.
    */
    @Override
    public Time clone() {
    return new Time(this);
    }