daylight 3.1.0 daylight: ^3.1.0 copied to clipboard
Get the sunset and sunrise times for a geolocation without having to access any remote api.
import 'package:intl/intl.dart';
import 'package:daylight/daylight.dart';
void main() {
const london = const DaylightLocation(51.5072, 0.1276);
final december = DateTime.utc(2024, 12, 1);
// Create berlin calculator
const berlinSunCalculator = const DaylightCalculator(london);
// calculate for sunrise on civil twilight
final civilSunrise = berlinSunCalculator.calculateEvent(
december,
Zenith.civil,
EventType.sunrise,
);
print((
"Civil sunrise:",
civilSunrise?.formatStandard(),
)); // utc: 07:03:16
// calculate for sunrise and sunset on astronomical twilight
final astronomicalEvents = berlinSunCalculator.calculateForDay(
december,
Zenith.astronomical,
);
print((
"sunset",
astronomicalEvents.sunset?.formatStandard(),
)); // utc: 17:55:39
print((
"sunrise",
astronomicalEvents.sunrise?.formatStandard(),
)); // utc: 05:40:52
print(astronomicalEvents.type); // DayType.sunriseAndSunset
}
extension on DateTime {
String formatStandard() {
return DateFormat("HH:mm:ss").format(this.toLocal());
}
}