<commit_before>/*
 * Copyright (c) 2016 by <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.hansolo.tilesfx.weather;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * We use the free REST API from Sunrise Sunset that you can find at
 * 
 *                 http://sunrise-sunset.org/api
 * 
 * It can be used free of charge. But please do not use that API in 
 * a manner that exceeds reasonable request volume, constitutes or
 * abusive usage.
 */

/**
 * Created by hansolo on 28.12.16.
 */
public class Sun {
    private static final String SUNRISE_SUNSET_URL = "http://api.sunrise-sunset.org/json?";


    // ******************** Methods *******************************************
    /**
     * Returns an array of ZonedDateTime objects for sunrise and sunset in UTC. Means there
     * are no daylight saving time adjustments.
     * @param LATITUDE
     * @param LONGITUDE
     * @return an array of ZonedDateTime objects for sunrise and sunset in UTC.
     */
    public static ZonedDateTime[] getSunriseSunsetAt(final double LATITUDE, final double LONGITUDE) {
        StringBuilder response = new StringBuilder();
        try {
            final String URL_STRING = new StringBuilder(SUNRISE_SUNSET_URL).append("lat=").append(LATITUDE)
                                                                           .append("&lng=").append(LONGITUDE)
                                                                           .append("&date=today&formatted=0")
                                                                           .toString();
            final HttpURLConnection CONNECTION = (HttpURLConnection) new URL(URL_STRING).openConnection();
            final BufferedReader    IN         = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
            String                  inputLine;
            while ((inputLine = IN.readLine()) != null) {
                response.append(inputLine).append("\n");
            }
            IN.close();
            return parseJsonData(response.toString());
        } catch (IOException ex) {
            return null;
        }
    }

    private static ZonedDateTime[] parseJsonData(final String JSON_DATA) {
        Object     obj     = JSONValue.parse(JSON_DATA);
        JSONObject jsonObj = (JSONObject) obj;

        // Results
        JSONObject results = (JSONObject) jsonObj.get("results");

        ZonedDateTime sunrise                   = ZonedDateTime.parse(results.get("sunrise").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime sunset                    = ZonedDateTime.parse(results.get("sunset").toString(), DateTimeFormatter.ISO_DATE_TIME);
        /*
        ZonedDateTime solarNoon                 = ZonedDateTime.parse(results.get("solar_noon").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime dayLength                 = ZonedDateTime.parse(results.get("day_length").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime civilTwilightBegin        = ZonedDateTime.parse(results.get("civil_twilight_begin").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime nauticalTwilightBegin     = ZonedDateTime.parse(results.get("nautical_twilight_begin").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime nauticalTwilightEnd       = ZonedDateTime.parse(results.get("nautical_twilight_end").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime astronomicalTwilightBegin = ZonedDateTime.parse(results.get("astronomical_twilight_begin").toString(), DateTimeFormatter.ISO_DATE_TIME);
        ZonedDateTime astronomicalTwilightEnd   = ZonedDateTime.parse(results.get("astronomical_twilight_end").toString(), DateTimeFormatter.ISO_DATE_TIME);
        */
        return new ZonedDateTime[] {sunrise, sunset};
    }
}<commit_msg>Added a possibility to define a ZoneId<commit_after>/*
 * Copyright (c) 2016 by <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.hansolo.tilesfx.weather;

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * We use the free REST API from Sunrise Sunset that you can find at
 * 
 *                 http://sunrise-sunset.org/api
 * 
 * It can be used free of charge. But please do not use that API in 
 * a manner that exceeds reasonable request volume, constitutes or
 * abusive usage.
 */

/**
 * Created by hansolo on 28.12.16.
 */
public class Sun {
    private static final String SUNRISE_SUNSET_URL = "http://api.sunrise-sunset.org/json?";


    // ******************** Methods *******************************************
    /**
     * Returns an array of ZonedDateTime objects for sunrise and sunset in UTC. Means there
     * are no daylight saving time adjustments.
     * @param LATITUDE
     * @param LONGITUDE
     * @return an array of ZonedDateTime objects for sunrise and sunset in UTC.
     */
    public static ZonedDateTime[] getSunriseSunsetAt(final double LATITUDE, final double LONGITUDE) {
        return getSunriseSunsetAt(LATITUDE, LONGITUDE, ZoneId.systemDefault());
    }
    public static ZonedDateTime[] getSunriseSunsetAt(final double LATITUDE, final double LONGITUDE, final ZoneId ZONE_ID) {
        StringBuilder response = new StringBuilder();
        try {
            final String URL_STRING = new StringBuilder(SUNRISE_SUNSET_URL).append("lat=").append(LATITUDE)
                                                                           .append("&lng=").append(LONGITUDE)
                                                                           .append("&date=today&formatted=0")
                                                                           .toString();
            final HttpURLConnection CONNECTION = (HttpURLConnection) new URL(URL_STRING).openConnection();
            final BufferedReader    IN         = new BufferedReader(new InputStreamReader(CONNECTION.getInputStream()));
            String                  inputLine;
            while ((inputLine = IN.readLine()) != null) {
                response.append(inputLine).append("\n");
            }
            IN.close();
            return parseJsonData(response.toString(), ZONE_ID);
        } catch (IOException ex) {
            return null;
        }
    }

    private static ZonedDateTime[] parseJsonData(final String JSON_DATA, final ZoneId ZONE_ID) {
        Object     obj     = JSONValue.parse(JSON_DATA);
        JSONObject jsonObj = (JSONObject) obj;

        // Results
        JSONObject results = (JSONObject) jsonObj.get("results");

        LocalDateTime sunrise                   = LocalDateTime.parse(results.get("sunrise").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime sunset                    = LocalDateTime.parse(results.get("sunset").toString(), DateTimeFormatter.ISO_DATE_TIME);
        /*
        LocalDateTime solarNoon                 = LocalDateTime.parse(results.get("solar_noon").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime dayLength                 = LocalDateTime.parse(results.get("day_length").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime civilTwilightBegin        = LocalDateTime.parse(results.get("civil_twilight_begin").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime nauticalTwilightBegin     = LocalDateTime.parse(results.get("nautical_twilight_begin").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime nauticalTwilightEnd       = LocalDateTime.parse(results.get("nautical_twilight_end").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime astronomicalTwilightBegin = LocalDateTime.parse(results.get("astronomical_twilight_begin").toString(), DateTimeFormatter.ISO_DATE_TIME);
        LocalDateTime astronomicalTwilightEnd   = LocalDateTime.parse(results.get("astronomical_twilight_end").toString(), DateTimeFormatter.ISO_DATE_TIME);
        */

        return new ZonedDateTime[] {ZonedDateTime.of(sunrise, ZONE_ID), ZonedDateTime.of(sunset, ZONE_ID)};
    }
}
{'fraction_non_alphanumeric': 0.10405166128528279, 'fraction_numerical': 0.003124674513071555, 'mean_word_length': 2.7699254024342364, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3742757', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2426, 'n_words': 642}