sun

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: BSD-3-Clause Imports: 6 Imported by: 3

README

Package sun implements solar time calculations.

Go Documentation Build status

The package provides a single function that returns the sunrise, sunset and solar noon for a given time and location.

Documentation

Overview

Package sun provides solar time calculations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Times

func Times(date time.Time, lat, lon float64) (rise, noon, set time.Time)

Times returns the times for sun rise, solar noon and sun set for the given time and location based on the formulae provided by the NOAA.

See https://gml.noaa.gov/grad/solcalc/solareqns.PDF

Example
package main

import (
	"fmt"
	"log"
	"time"

	"github.com/kortschak/sun"
)

func main() {
	// Calculate solar times for Paris on 2021-07-30.
	loc, err := time.LoadLocation("CET")
	if err != nil {
		log.Fatal(err)
	}
	date := time.Date(2021, 7, 30, 12, 0, 0, 0, loc)
	rise, noon, set := sun.Times(date, 48.856614, 2.3522219)

	fmt.Printf("Sunrise: %v\nNoon:    %v\nSunset:  %v\n", rise, noon, set)

}
Output:


Sunrise: 2021-07-30 06:20:05 +0200 CEST
Noon:    2021-07-30 13:57:09 +0200 CEST
Sunset:  2021-07-30 21:34:13 +0200 CEST

Types

type Event

type Event int

Event is the set of solar events.

const (
	Sunrise Event = iota - 1
	Noon
	Sunset
)

func (Event) String

func (e Event) String() string

type Parser

type Parser struct {
	// CronParser is the parser used to handle
	// any non-solar cron specs. If it is nil
	// the standard github.com/robfig/cron/v3
	// parser is used.
	CronParser cron.ScheduleParser
}

Parser is a cron spec parser that handles solar event descriptors. These are

  • @sunrise
  • @noon
  • @sunset

Each solar cron spec in in the form

@(sunrise|noon|sunset)([+-]duration)? lat lon
Example
c := cron.New(cron.WithParser(sun.Parser{}))

// Set a reminder to go for a walk each evening.
_, err := c.AddFunc("@sunset-30m 48.856614 2.3522219", func() {
	fmt.Println("Take a walk along the Seine before sunset.")
})
if err != nil {
	log.Fatal(err)
}

c.Start()

select {}
Output:

func (Parser) Parse

func (p Parser) Parse(spec string) (cron.Schedule, error)

Parse returns a schedule representing the given spec.

type Schedule

type Schedule struct {
	// Event is the event type being scheduled.
	Event Event
	// Offset is the relative time offset relative
	// to the solar-time event.
	Offset time.Duration

	// Lat and Lon are the latitude and longitude
	// for the scheduled event.
	Lat, Lon float64

	// Location overrides the schedule location.
	Location *time.Location
}

Schedule is a cron.Schedule that schedules solar-time events.

func (*Schedule) Next

func (s *Schedule) Next(t time.Time) time.Time

Next returns the next time the receiver's event occurs.

Jump to

Keyboard shortcuts

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