Git Product home page Git Product logo

Comments (5)

rlxone avatar rlxone commented on June 8, 2024

Hi,

sunrise and sunset returns

time in local time (days)

so we need to convert local time in days to date

// Minsk coordinates
let latitude = 53.893009
let longitude = 27.567444
        
// Get current date
let date = Date()
let calendar = Calendar.current
        
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
        
// Get sunrise and sunset in days with zero timezone
let sunriseDaysTime = Solar.sunrise(lat: latitude, lon: longitude, year: year, month: month, day: day, timezone: 0, dlstime: 0)
let sunsetDaysTime = Solar.sunset(lat: latitude, lon: longitude, year: year, month: month, day: day, timezone: 0, dlstime: 0)
        
// Get date from sunrise and sunset days value
let sunriseDate = Date(timeIntervalSince1970: sunriseDaysTime * 24 * 60 * 60)
let sunsetDate = Date(timeIntervalSince1970: sunsetDaysTime * 24 * 60 * 60)
        
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "hh:mm:ss"
        
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
        
print("Minsk sunrise for \(dateFormatter.string(from: date)): \(timeFormatter.string(from: sunriseDate))")
print("Minsk sunset for \(dateFormatter.string(from: date)): \(timeFormatter.string(from: sunsetDate))")

//Minsk sunrise for Thursday, Oct 4, 2018: 07:16:36
//Minsk sunset for Thursday, Oct 4, 2018: 18:39:22

from solarnoaa.

kbirand avatar kbirand commented on June 8, 2024

I am getting strange results...

latitude: 38.2980421, longitude: -122.2877842
is for Napa, California
and this is the result (first one is sunrise, second one is sunset..)
Thursday, Oct 4, 2018: 16:07:45. should be 07:06
Thursday, Oct 4, 2018: 03:47:13 should be 18:46

latitude: 41.0117968, longitude: 28.9754277
is for Istanbul Turkey
and the result is

Thursday, Oct 4, 2018: 06:03:31
Thursday, Oct 4, 2018: 17:41:32
almost there 1 hour early though...

and to get this result i need to change
timeFormatter.dateFormat = "hh:mm:ss"
to
timeFormatter.dateFormat = "HH:mm:ss"

otherwise I get for Istanbul this :
Thursday, Oct 4, 2018: 06:03:31
Thursday, Oct 4, 2018: 05:41:32

from solarnoaa.

kbirand avatar kbirand commented on June 8, 2024

Just put down your coordinates for Minsk...
this is what i got with :

timeFormatter.dateFormat = "hh:mm:ss"

Minsk sunrise for Thursday, Oct 4, 2018: 06:16:36
Minsk sunset for Thursday, Oct 4, 2018: 05:39:22

if I change it to
timeFormatter.dateFormat = "HH:mm:ss"

Minsk sunrise for Thursday, Oct 4, 2018: 06:16:36
Minsk sunset for Thursday, Oct 4, 2018: 17:39:22

similar to your result with one hour offset..

I am using Xcode10

from solarnoaa.

rlxone avatar rlxone commented on June 8, 2024

You should know timezone for your location if you need local time. Try this:

// Chicago coordinates
let latitude = 41.881832
let longitude = -87.623177
        
// timezone for GMT-5
let timezone = -5
        
// Get current date
let date = Date()
var calendar = Calendar.current
calendar.timeZone = TimeZone(identifier:"GMT")!
        
let year = calendar.component(.year, from: date)
let month = calendar.component(.month, from: date)
let day = calendar.component(.day, from: date)
        
// Get sunrise and sunset in days with zero timezone
let sunriseDaysTime = Solar.sunrise(lat: latitude, lon: longitude, year: year, month: month, day: day, timezone: timezone, dlstime: 0)
let sunsetDaysTime = Solar.sunset(lat: latitude, lon: longitude, year: year, month: month, day: day, timezone: timezone, dlstime: 0)
        
// Get date from sunrise and sunset days value
let sunriseDate = Date(timeIntervalSince1970: sunriseDaysTime * 24 * 60 * 60)
let sunsetDate = Date(timeIntervalSince1970: sunsetDaysTime * 24 * 60 * 60)
        
let timeFormatter = DateFormatter()
timeFormatter.dateFormat = "hh:mm:ss"
timeFormatter.timeZone = TimeZone(identifier:"GMT")
        
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE, MMM d, yyyy"
dateFormatter.timeZone = TimeZone(identifier:"GMT")
        
print("Chicago sunrise for \(dateFormatter.string(from: date)): \(timeFormatter.string(from: sunriseDate))")
print("Chicago sunset for \(dateFormatter.string(from: date)): \(timeFormatter.string(from: sunsetDate))")

from solarnoaa.

kbirand avatar kbirand commented on June 8, 2024

thanks 👍

from solarnoaa.

Related Issues (2)

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.