Skip to content

Commit 6f57406

Browse files
author
renancaraujo
committed
add license and api docs
1 parent 412ba5e commit 6f57406

File tree

5 files changed

+51
-26
lines changed

5 files changed

+51
-26
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1+
## [0.1.0]
2+
- Added license and api docs
3+
14
## [0.0.1]
25
- A sun rises

LICENSE

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
TODO: Add your license here.
1+
Copyright 2020 Renan C. Araújo
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Dart Daylight
2+
[![Pub](https://img.shields.io/pub/v/daylight.svg?style=popout)](https://pub.dartlang.org/packages/daylight)
23

34
[![Pub](https://img.shields.io/pub/v/daylight.svg?style=popout)](https://pub.dartlang.org/packages/daylight)
45

lib/daylight.dart

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,35 @@ import 'package:daylight/season.dart';
55
import 'package:intl/intl.dart';
66
import 'package:angles/angles.dart';
77

8+
/// Enum that defines in with scope the zenith time will be calculated.
89
enum EventType { sunrise, sunset }
910

11+
/// Enum that defines which sun events happens in a specific day
1012
enum DayType { sunriseAndSunset, sunriseOnly, sunsetOnly, allDay, allNight }
1113

14+
/// Extension that adds utility comparison methods into [DayType].
1215
extension DayTypeUtils on DayType {
16+
/// See if this is a type that indicates no sun events
1317
bool get isNoChange => this == DayType.allDay || this == DayType.allNight;
1418

19+
/// Define if [DayType] indicates sunrise.
1520
bool get hasSunrise =>
1621
this == DayType.sunriseOnly || this == DayType.sunriseAndSunset;
1722

23+
/// Define if [DayType] indicates sunset.
1824
bool get hasSunset =>
1925
this == DayType.sunsetOnly || this == DayType.sunriseAndSunset;
2026
}
2127

22-
enum Zenith {
23-
astronomical,
24-
nautical,
25-
civil,
26-
official,
27-
golden,
28-
}
28+
/// Enum that defines the sun position in the event.
29+
///
30+
/// Twilights are divided in three phases: [civil], [nautical] and [astronomical].
31+
/// More info at: https://www.timeanddate.com/astronomy/different-types-twilight.html
32+
/// Added two extra positions: [official], for when the sun is crosses the horizon line and [golden]
33+
/// for when the sun is near the horizon.
34+
enum Zenith { astronomical, nautical, civil, official, golden, _custom }
2935

36+
/// Extension that adds [angle] prop to retrieve angle value (in grads) for a specific [Zenith]
3037
extension ZenithAngle on Zenith {
3138
double get angle {
3239
switch (this) {
@@ -49,13 +56,18 @@ extension ZenithAngle on Zenith {
4956
}
5057
}
5158

59+
/// Defines a snapshot result for a daylight calculation.
5260
class DaylightResult {
61+
/// Time of the sunrise
5362
final DateTime sunrise;
63+
64+
/// Time of the sunset
5465
final DateTime sunset;
5566
final DateTime _date;
5667

5768
DaylightResult(this.sunrise, this.sunset, this._date);
5869

70+
/// Define which sun events happens in the snapshot date
5971
DayType get type {
6072
if (sunrise == null) {
6173
if (sunset == null) {
@@ -77,33 +89,35 @@ class DaylightResult {
7789
}
7890
}
7991

92+
/// Class that wraps all daylight calculations
8093
class DaylightCalculator {
94+
/// The specific coordinate location for the calculation.
8195
final Location location;
8296

8397
const DaylightCalculator(this.location);
8498

85-
DaylightResult calculateForDay(DateTime date,
86-
[Zenith zenith = Zenith.official]) {
87-
final lastMidnight = new DateTime(date.year, date.month, date.day);
99+
/// Calculate both sunset and sunrise times for optional [Zenith] and returns in a [DaylightResult]
100+
DaylightResult calculateForDay(
101+
DateTime date, [
102+
Zenith zenith = Zenith.official,
103+
]) {
104+
final sunsetDateTime = calculateEvent(date, zenith, EventType.sunset);
105+
final sunriseDateTime = calculateEvent(date, zenith, EventType.sunrise);
106+
return DaylightResult(sunriseDateTime, sunsetDateTime, date);
107+
}
88108

89-
double sunriseMils = calculateEvent(date, zenith, EventType.sunrise);
90-
DateTime sunriseDateTime;
91-
if (sunriseMils != null) {
92-
int mils = (lastMidnight.millisecondsSinceEpoch + sunriseMils).floor();
93-
sunriseDateTime = DateTime.fromMillisecondsSinceEpoch(mils);
94-
}
109+
/// Calculate the time of an specific sun event
110+
DateTime calculateEvent(DateTime date, Zenith zenith, EventType type) {
111+
final lastMidnight = new DateTime(date.year, date.month, date.day);
95112

96-
double sunsetMils = calculateEvent(date, zenith, EventType.sunset);
97-
DateTime sunsetDateTime;
98-
if (sunsetMils != null) {
99-
int mils = (lastMidnight.millisecondsSinceEpoch + sunsetMils).floor();
100-
sunsetDateTime = DateTime.fromMillisecondsSinceEpoch(mils, isUtc: false);
101-
}
113+
double eventMils = _calculate(date, zenith, EventType.sunrise);
102114

103-
return DaylightResult(sunriseDateTime, sunsetDateTime, date);
115+
if (eventMils == null) return null;
116+
int mils = (lastMidnight.millisecondsSinceEpoch + eventMils).floor();
117+
return DateTime.fromMillisecondsSinceEpoch(mils);
104118
}
105119

106-
double calculateEvent(DateTime time, Zenith zenith, EventType type) {
120+
double _calculate(DateTime time, Zenith zenith, EventType type) {
107121
double baseLongHour = location.long / 15;
108122

109123
double hour = _longToHour(time, type == EventType.sunrise ? 6 : 18);
@@ -199,5 +213,6 @@ double degToRad(double deg) => Angle.fromDegrees(deg).radians;
199213
class Location {
200214
final double lat;
201215
final double long;
216+
202217
Location(this.lat, this.long);
203218
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: daylight
22
description: Get the sunset and sunrise times for a geolocation without having to access any api.
3-
version: 0.0.1
3+
version: 0.1.0
44
homepage: https://github.com/renancaraujo/daylight
55

66
environment:

0 commit comments

Comments
 (0)