def test_as_datetime(self):
     date = USNO_Data.as_datetime(self.year, 2, 22, '2153')
     assert date.year == self.year
     assert date.month == 2
     assert date.day == 22
     assert date.hour == 21
     assert date.minute == 53
     assert date.second == 0
     assert date.utcoffset() is None
 def setup(self):
     self.lat = 51.0 + 32.0/60.0
     self.lng = -22.0/60.0
     self.year = 2015
     self.usno_data = USNO_Data(self.lat, self.lng)
     self.recorder = Betamax(self.usno_data.session, default_cassette_options={
         'record_mode': 'once',
         'match_requests_on': ['method', 'uri', 'headers'],
         'preserve_exact_body_bytes': True
     })
Beispiel #3
0
 def __init__(self, lat, lng, start_year, end_year):
     self.lat = lat
     self.lng = lng
     self.start_year = start_year
     self.end_year = end_year
     self.usno_data = USNO_Data(lat, lng)
Beispiel #4
0
class RajabRoza:
    def __init__(self, lat, lng, start_year, end_year):
        self.lat = lat
        self.lng = lng
        self.start_year = start_year
        self.end_year = end_year
        self.usno_data = USNO_Data(lat, lng)

    def get_roza_durations_for_year(self, hijri_year):
        durations = (timedelta.max, timedelta.min)

        start_date = HijriDate(hijri_year, 7, 1).to_gregorian()
        self.usno_data.get_data(start_date.year)
        for day in range(0, 30):
            date = start_date + timedelta(day)
            if date.year != start_date.year:
                self.usno_data.get_data(date.year)

            sunrise = self.usno_data.sunrise(date.month, date.day)
            sunset = self.usno_data.sunset(date.month, date.day)

            duration = sunset - sunrise
            durations = (
                min(durations[0], duration), max(durations[1], duration)
            )

        return durations

    def get_roza_durations(self):
        self.durations = [
            self.get_roza_durations_for_year(year)
            for year in range(self.start_year, self.end_year + 1)
        ]

    def save_to_yaml(self, filename):
        stream = open(filename, 'w')
        yaml.safe_dump({
            'lat': self.lat,
            'lng': self.lng,
            'start_year': self.start_year,
            'end_year': self.end_year,
            'durations': [
                [duration[0].total_seconds(), duration[1].total_seconds()]
                for duration in self.durations
            ]
        }, stream, allow_unicode=True)
        stream.close()

    @staticmethod
    def load_from_yaml(filename):
        stream = open(filename, 'r')
        data = yaml.load(stream)
        stream.close()

        rajab_roza = RajabRoza(
            data['lat'], data['lng'], data['start_year'], data['end_year']
        )
        rajab_roza.durations = [
            (timedelta(seconds=duration[0]), timedelta(seconds=duration[1]))
            for duration in data['durations']
        ]

        return rajab_roza
class TestUSNO_Data:
    def setup(self):
        self.lat = 51.0 + 32.0/60.0
        self.lng = -22.0/60.0
        self.year = 2015
        self.usno_data = USNO_Data(self.lat, self.lng)
        self.recorder = Betamax(self.usno_data.session, default_cassette_options={
            'record_mode': 'once',
            'match_requests_on': ['method', 'uri', 'headers'],
            'preserve_exact_body_bytes': True
        })

    def test_angle_components(self):
        (direction, degrees, minutes) = USNO_Data.angle_components(self.lat)
        assert direction == 1
        assert degrees == 51
        assert minutes == 32

    def test_parameters(self):
        expected_parameters = {
            "FFX": "2",
            "xxy": "2015",
            "type": "0",
            "place": "",
            "xx0": "-1",
            "xx1": "0",
            "xx2": "22",
            "yy0": "1",
            "yy1": "51",
            "yy2": "32",
            "zz0": "1",
            "zz1": "0",
            "ZZZ": "END"
        }
        self.usno_data.year = 2015
        assert self.usno_data.parameters() == expected_parameters

    def test_as_datetime(self):
        date = USNO_Data.as_datetime(self.year, 2, 22, '2153')
        assert date.year == self.year
        assert date.month == 2
        assert date.day == 22
        assert date.hour == 21
        assert date.minute == 53
        assert date.second == 0
        assert date.utcoffset() is None

    def test_day_of_year(self):
        assert USNO_Data.day_of_year(2015, 2, 22) == 53
        assert USNO_Data.day_of_year(2016, 4, 1) == 92

    def test_get_data(self):
        with self.recorder.use_cassette('get_data'):
            self.usno_data.get_data(self.year)
            assert self.usno_data.sunrises[-1] == USNO_Data.as_datetime(self.year, 12, 31, '0807')
            assert self.usno_data.sunsets[-1] == USNO_Data.as_datetime(self.year, 12, 31, '1601')

    def test_sunrise(self):
        with self.recorder.use_cassette('get_sunrise'):
            self.usno_data.get_data(self.year)
            sunrise = self.usno_data.sunrise(2, 22)
            assert sunrise.hour == 7
            assert sunrise.minute == 2

    def test_sunset(self):
        with self.recorder.use_cassette('get_sunset'):
            self.usno_data.get_data(self.year)
            sunset = self.usno_data.sunset(2, 22)
            assert sunset.hour == 17
            assert sunset.minute == 29
 def test_get_data(self):
     with self.recorder.use_cassette('get_data'):
         self.usno_data.get_data(self.year)
         assert self.usno_data.sunrises[-1] == USNO_Data.as_datetime(self.year, 12, 31, '0807')
         assert self.usno_data.sunsets[-1] == USNO_Data.as_datetime(self.year, 12, 31, '1601')
 def test_day_of_year(self):
     assert USNO_Data.day_of_year(2015, 2, 22) == 53
     assert USNO_Data.day_of_year(2016, 4, 1) == 92
 def test_angle_components(self):
     (direction, degrees, minutes) = USNO_Data.angle_components(self.lat)
     assert direction == 1
     assert degrees == 51
     assert minutes == 32