LuckyCatLabs

Making code suck less

Release: Sunrise/Sunset Java Library

New from LuckyCat Labs is a Java library that computes the sunrise / sunset using GPS coordinates and today’s date.  To use, simply download the tar or zip file from the GitHub page, or use git to pull the source:

$ git clone git://github.com/mikereedell/sunrisesunsetlib-java

Once you have the source, use the command:

$ ant all

To compile the source, build the bin, src, and test jars, execute the unit tests and generate the JavaDoc for the library.

This code snippet illustrates the main usage pattern:

// Location of sunrise/set, as latitude/longitude.
Location location = new Location("39.9937", "-75.7850");

// Create calculator object with the location and time zone identifier.
SunriseSunsetCalculator calculator = new SunriseSunsetCalculator(location, "America/New_York");

Calendar date = Calendar.getInstance();
String dawn = calculator.getCivilSunriseForDate(date);
String dusk = calculator.getCivilSunsetForDate(date);

The list of supported time zone identifiers is given by:

//Returns String[] of supported tz identifiers.
TimeZone.getAvailableIDs();

Any feedback? Comments? Features? Bugs? Comment below or add to the project wiki.

Released under the Apache License 2.0.

View Comments
  • Croques
    Further to my last post...

    This change to getLocalTimeAsString(...) in class SolarEventCalculator appears to do it:-

    private String getLocalTimeAsString(BigDecimal localTime) {
    String[] timeComponents = localTime.toPlainString().split("\\.");
    int hour = Integer.parseInt(timeComponents[0]);

    BigDecimal minutes = new BigDecimal("0." + timeComponents[1]);
    minutes = minutes.multiply(BigDecimal.valueOf(60)).setScale(0, RoundingMode.HALF_EVEN);
    if (minutes.intValue() == 60) {
    minutes = BigDecimal.ZERO;
    hour += 1;
    }

    if(hour < 0 && minutes == BigDecimal.ZERO) hour += 24;
    if(hour < 0) {
    hour += 23;
    int myMin = minutes.intValue();
    myMin = 60 - myMin;
    minutes = BigDecimal.valueOf(myMin);
    }
    String minuteString = minutes.intValue() < 10 ? "0" + minutes.toPlainString() : minutes.toPlainString();
    String hourString = (hour < 10) ? "0" + String.valueOf(hour) : String.valueOf(hour);
    return hourString + ":" + minuteString;
    }

    I forgot to mention that currently I am in NZ and we are 13 hours ahead of Zulu time.
  • Croques
    In SolarEventCalculator, using getOfficalSunriseForDate(Date (GMT)) and its twin getCivilSunriseForDate(Date (GMT)) at location -43.47, 172.55 results in a -ve value for hour which when is string compounded with zero outputs 0-6.03 or 0-5.35. A test for -ve hour and then add 24 could be the way forward?

    Also note the inconsistent spelling of Official in getOfficalSunriseForDate.

    Croques.
  • Thanks for pointing out the spelling error! It's fixed and available on github.

    As for the error case, can you specify a date that causes the error condition to occur so I can write a test around it?
  • Croques
    Thanks for the spelling correction

    Current date is good for causing the error mentioned.
    Just ran a test now and sunrise reported 0-5:31
    with this code appended to your Test.java :-

    String sunrise = mySunriseCalc.getOfficalSunriseForDate(t);
    System.out.println("Sunrise is: " + sunrise);
    System.out.println("Calendar is: " + t.toString());
    System.out.println("Location is: " + myLoc.getLatitude().toPlainString() + ", " + myLoc.getLongitude().toPlainString());

    produced:-

    Sunset is: 06:50
    Sunrise is: 0-5:34
    Calendar is: java.util.GregorianCalendar[time=1268772592184,areFieldsSet=true,areAllFieldsSet=true,
    lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,
    transitions=0,lastRule=null],firstDayOfWeek=1,
    minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=2,
    WEEK_OF_YEAR=12,WEEK_OF_MONTH=3,DAY_OF_MONTH=16,
    DAY_OF_YEAR=75,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=3,
    AM_PM=1,HOUR=8,HOUR_OF_DAY=20,MINUTE=49,SECOND=52,
    MILLISECOND=184,ZONE_OFFSET=0,DST_OFFSET=0]
    Location is: -43.47, 172.55

    Croques
blog comments powered by Disqus