sunrise

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2020 License: BSD-3-Clause Imports: 2 Imported by: 7

README

sunrise

Calculates sunrise and sunset times

This API is now stable.

Using

import "github.com/keep94/sunrise"

Documentation

Overview

Package sunrise computes sunrises and sunsets using wikipedia article http://en.wikipedia.org/wiki/Sunrise_equation. Testing at my latitude and longitude in California shows that computed sunrises and sunsets can vary by as much as 2 minutes from those that NOAA reports at http://www.esrl.noaa.gov/gmd/grad/solcalc/sunrise.html.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Phase

type Phase int

Phase indicates day or night.

const (
	Day Phase = iota
	Night
)

func DayOrNight

func DayOrNight(latitude, longitude float64, currentTime time.Time) (
	dayOrNight Phase, start, end time.Time)

DayOrNight returns whether currentTime is day or night at the given location. start and end are the start and end of the current day or night. start and end are in the same timezone as currentTime. Latitude is postive for north and negative for south. Longitude is positive for east and negative for west.

type Sunrise

type Sunrise struct {
	// contains filtered or unexported fields
}

Sunrise gives sunrise and sunset times.

Example

Show sunrise and sunset for first 5 days of June in LA

package main

import (
	"fmt"
	"github.com/keep94/sunrise"
	"time"
)

func main() {
	var s sunrise.Sunrise

	// Start time is June 1, 2013 PST
	location, _ := time.LoadLocation("America/Los_Angeles")
	startTime := time.Date(2013, 6, 1, 0, 0, 0, 0, location)

	// Coordinates of LA are 34.05N 118.25W
	s.Around(34.05, -118.25, startTime)

	for s.Sunrise().Before(startTime) {
		s.AddDays(1)
	}

	formatStr := "Jan 2 15:04:05"
	for i := 0; i < 5; i++ {
		fmt.Printf("Sunrise: %s Sunset: %s\n", s.Sunrise().Format(formatStr), s.Sunset().Format(formatStr))
		s.AddDays(1)
	}
}
Output:

Sunrise: Jun 1 05:44:01 Sunset: Jun 1 20:00:42
Sunrise: Jun 2 05:43:45 Sunset: Jun 2 20:01:17
Sunrise: Jun 3 05:43:30 Sunset: Jun 3 20:01:52
Sunrise: Jun 4 05:43:17 Sunset: Jun 4 20:02:25
Sunrise: Jun 5 05:43:05 Sunset: Jun 5 20:02:57

func (*Sunrise) AddDays

func (s *Sunrise) AddDays(numDays int)

AddDays computes the sunrise and sunset numDays after (or before if numDays is negative) the current sunrise and sunset at the same latitude and longitude.

func (*Sunrise) Around

func (s *Sunrise) Around(latitude, longitude float64, currentTime time.Time)

Around computes the sunrise and sunset times for latitude and longitude around currentTime. Generally, the computed sunrise will be no earlier than 24 hours before currentTime and the computed sunset will be no later than 24 hours after currentTime. However, these differences may exceed 24 hours on days with more than 23 hours of daylight. The latitude is positive for north and negative for south. Longitude is positive for east and negative for west.

func (*Sunrise) Sunrise

func (s *Sunrise) Sunrise() time.Time

Sunrise returns the current computed sunrise. Returned sunrise has the same location as the time passed to Around.

func (*Sunrise) Sunset

func (s *Sunrise) Sunset() time.Time

Sunset returns the current computed sunset. Returned sunset has the same location as the time passed to Around.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL