Code Monkey home page Code Monkey logo

moonkit's Introduction

MoonKit

moonkit

GitHub GitHub stars GitHub issues Requires Core Location

MoonKit is a Swift package which uses math and trigonometry to compute several information about the Moon.

Usage

MoonKit only needs a location and the relative time zone to compute every Moon information.
Everything is computed locally, no internet connection is needed.

Creating a Moon

// Creating a CLLocation object with the coordinates you are interested in
let naplesLocation: CLLocation = .init(latitude: 40.84014, longitude: 14.25226)

// Timezone for the location of interest. It's highly recommended to initialize it via identifier
let timeZoneNaples: Timezone = .init(identifier: "Europe/Rome") ?? .current

// Creating the Moon instance which will store all the information you need about Moon events and her position
var myMoon: Moon = .init(location: naplesLocation, timeZone: timeZoneNaples)

Retrieve information

// Creating a Date instance
let myDate: Date = Date() // Your current date

// Setting inside myMoon object the date of interest
myMoon.setDate(myDate)

      // All the following informations are related to the given location for the date that has just been set

// Azimuth of the Moon 
myMoon.azimuth  

// Altitude of the Moon
myMoon.altitude

// Moonrise Date
myMoon.moonRise

// Moonset Date
myMoon.moonSet

// Current Moon Phase
myMoon.currentMoonPhase

// Moon Astrological sign
myMoon.moonSign

// To know all the information you can retrieve go to the **Features** section.

Working with Timezones and Dates

To properly show the Moon Date Events use the following DateFormatter.

 //Creting a DateFormatter
 let dateFormatter =  DateFormatter()
 
 //Properly setting his attributes
 dateFormatter.locale    =  .current
 dateFormatter.timeZone  =  timeZoneNaples  // It shall be the same as the one used to initilize myMoon
 dateFormatter.timeStyle = .full
 dateFormatter.dateStyle = .full
  
 //Printing Moon Date Events with the correct Timezone
  
 print("Moonrise: \(dateFormatter.string(from: myMoon.moonRise))")
    

Features

  • Moon Azimuth
  • Moon Altitude
  • MoonRise Time
  • MoonSet Time
  • Moon Percentage
  • Moon Phase
  • Moon Astrological Sign
  • MoonRise Azimuth
  • MoonSet Azimuth
  • Next Full Moon
  • Next New Moon

References

  • Celestial Calculations: A Gentle Introduction to Computational Astronomy: Link.

SunKit ☀️

Take a look to the other Package, this time about the Sun: SunKit.

moonkit's People

Contributors

davideilmito avatar nickkohrn avatar uevs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

moonkit's Issues

Error during computing moon phase

I found yesterday at 12:27 pm PST at location latitude: 47.588889, longitude: -122.527778 that the package was unable to correctly calculate the new moon phase. The variable ageOfTheMoonInDays is being calculated as -0.06587243342914535 and ageOfTheMoonInDegrees as -0.8030310742046822 so the switch statement was falling through to the error case. Unfortunately I don't understand the math well enough to take a stab at fixing it. Hopefully the provided information is enough to duplicate the state.

present Moonrise azimuth in a view

Hello,

I want to present in a view the azimuth of a moonrise.
Is it possible to help ?

import SwiftUI
import MoonKit

struct SwiftUIView3: View {
    var body: some View {
        
        Text("in \(String(moon.moonriseAzimuth)) degrees")
    }
}

struct SwiftUIView3_Previews: PreviewProvider {
    static var previews: some View {
        SwiftUIView3()
    }
}

Moonset azimuth - on the southern part of the world

Hello,

I am able to calculate the moonrise and moonset azimuth of a place on the Northern part of the world. For example for Rome or Brussels or Amsterdam.

If I change my location to Cape Town South Africa or to Nairobi I have the Moonrise azimuth, but the moonset azimuth is not giving any value.

Could you check?

Updated package?

Would it be possible to get an updated package with the changes that have been made since December? There have been some good bug fixes and I'd rather use the released package than an embedded one. Thanks!

How to calculate the value for setTimeZone()?

I don't think it is possible to write a function to calculate the value to be set in setTimeZone().

I have prepared unit testing that demonstrates the problem.

The calculation is attempted by the TimeZone.offset(date) function, which only works sometimes. I don't think the correct value can be calculated.

The control data is from www.timeanddate.com

The current time zone of the application is "CET" (Prague), calculated for Cupertino, time zone "PST".

(Sorry for the confusion. I wrote down the error once before, but since I thought for a while that I made a testing error, I deleted the entry. After further testing, I verified that the error was indeed there.)

Michal Kus

import XCTest    
import CoreLocation
@testable import MoonKit


final class MoonKitTestTests: XCTestCase {

override func setUpWithError() throws {
    
}

override func tearDownWithError() throws {
    // Put teardown code here. This method is called after the invocation of each test method in the class.
}

func test1() throws {
    guard let timeZone = TimeZone(abbreviation: "PST") else {
        abort()
    }
    
    let location: CLLocation = .init(latitude: 37.317165398, longitude: -122.038499846)

    let moon = Moon.init(location: location, timeZone: 0)
    
    moon.setDate(MoonKit.createDateCurrentTimeZone(day: 13, month: 3, year: 2023, hour: 22, minute: 00, seconds: 00))
    moon.setTimeZone(timeZone.offset(moon.date))
    XCTAssertEqual(moon.timeZone, -7) //The value required to correctly calculate the moonrise and moonset.
    XCTAssertNotNil(moon.moonRise)
    if moon.moonRise != nil {
        XCTAssertEqual(moon.moonRise!.toString(), "03/13, 00:45") //OK
    }
    XCTAssertEqual(moon.moonSet!.toString(), "03/13, 10:37") //OK

    moon.setDate(MoonKit.createDateCurrentTimeZone(day: 12, month: 1, year: 2023, hour: 16, minute: 00, seconds: 00))
    moon.setTimeZone(timeZone.offset(moon.date))
    XCTAssertEqual(moon.timeZone, -9) //The value required to correctly calculate the moonrise and moonset.
    XCTAssertEqual(moon.moonRise!.toString(), "01/12, 21:42")
    XCTAssertEqual(moon.moonSet!.toString(), "01/12, 09:44")
    
    moon.setDate(MoonKit.createDateCurrentTimeZone(day: 26, month: 9, year: 2023, hour: 6, minute: 00, seconds: 00))
    moon.setTimeZone(timeZone.offset(moon.date))
    XCTAssertEqual(moon.timeZone, -7) //The value required to correctly calculate the moonrise and moonset.
    XCTAssertEqual(moon.moonRise!.toString(), "09/26, 17:52") //OK
    XCTAssertEqual(moon.moonSet!.toString(), "09/26, 03:26") //OK

    moon.setDate(MoonKit.createDateCurrentTimeZone(day: 26, month: 11, year: 2023, hour: 6, minute: 00, seconds: 00))
    moon.setTimeZone(timeZone.offset(moon.date))
    XCTAssertEqual(moon.timeZone, -8) //The value required to correctly calculate the moonrise and moonset.
    XCTAssertEqual(moon.moonRise!.toString(), "11/26, 16:19") //OK
    XCTAssertEqual(moon.moonSet!.toString(), "11/26, 06:18") //OK


}

func testPerformanceExample() throws {
    // This is an example of a performance test case.
    measure {
        // Put the code you want to measure the time of here.
    }
}

}


extension Date {

func toString() -> String {
    let df = DateFormatter()
    let custom = DateFormatter.dateFormat(fromTemplate: "MMdd HH:mm",
                                          options: 0,
                                          locale: Locale(identifier: "en"))
    df.dateFormat = custom
    return df.string(from: self)
}


  }


  extension TimeZone {

func offset(_ date: Date) -> Double {
    let res =
        Int(self.secondsFromGMT(for: date))
        + Int(self.daylightSavingTimeOffset(for: date))
        - Int(Calendar.current.timeZone.secondsFromGMT(for: date))
        - Int(Calendar.current.timeZone.daylightSavingTimeOffset(for: date))
    print("\(date.toString())  \(Int(self.secondsFromGMT(for: date)/3600)), \(Int(self.daylightSavingTimeOffset(for: date)/3600)), \(Int(Calendar.current.timeZone.secondsFromGMT(for: date)/3600)), \(Int(Calendar.current.timeZone.daylightSavingTimeOffset(for: date)/3600))")
    return Double(res)/3600.0
    
}

}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.