Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit 5ac33ea

Browse files
committed
feat: add sunrise and sunset localtime to output.
1 parent b24239c commit 5ac33ea

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

projects/API Based Weather Report/API Based Weather Report.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import requests
22
from datetime import datetime
3-
from Util_Functions import wind_degree_to_direction
3+
from Util_Functions import wind_degree_to_direction, unix_timestamp_to_localtime
44

55

66
# Function to fetch weather data from OpenWeatherMap API
@@ -33,9 +33,10 @@ def write_to_file(location, weather_data):
3333
date_time = datetime.now().strftime("%d %b %Y | %I:%M:%S %p")
3434

3535
# Writing header information to the file
36-
f.write("-------------------------------------------------------------\n")
37-
f.write(f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} | {date_time}\n")
38-
f.write("-------------------------------------------------------------\n")
36+
if "name" in weather_data and "sys" in weather_data and "country" in weather_data["sys"]:
37+
f.write("-------------------------------------------------------------\n")
38+
f.write(f"Weather Stats for - {weather_data['name']} | {weather_data['sys']['country']} | {date_time}\n")
39+
f.write("-------------------------------------------------------------\n")
3940

4041
# Writing temperature information to the file
4142
if "main" in weather_data and "temp" in weather_data["main"]:
@@ -75,6 +76,18 @@ def write_to_file(location, weather_data):
7576
"\tCurrent wind direction : " +
7677
wind_degree_to_direction(weather_data["wind"]["deg"]) + " \n")
7778

79+
# Writing sunrise local time to the file
80+
if "sys" in weather_data and "sunrise" in weather_data["sys"] and "timezone" in weather_data:
81+
f.write(
82+
"\tToday's sunrise time : " +
83+
unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]) + " \n")
84+
85+
# Writing sunset local time to the file
86+
if "sys" in weather_data and "sunset" in weather_data["sys"] and "timezone" in weather_data:
87+
f.write(
88+
"\tToday's sunset time : " +
89+
unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]) + " \n")
90+
7891
# Printing confirmation message after writing to file
7992
print("Weather information written to weatherinfo.txt")
8093

@@ -116,6 +129,10 @@ def main():
116129
print("Current Humidity :", weather_data["main"]["humidity"], "%")
117130
print("Current wind speed :", weather_data["wind"]["speed"], "kmph")
118131
print("Current wind direction:", wind_degree_to_direction(weather_data["wind"]["deg"]))
132+
print("Today's sunrise time :",
133+
unix_timestamp_to_localtime(weather_data["sys"]["sunrise"], weather_data["timezone"]))
134+
print("Today's sunset time :",
135+
unix_timestamp_to_localtime(weather_data["sys"]["sunset"], weather_data["timezone"]))
119136
else:
120137
# Printing error message if weather data fetching fails
121138
print("Failed to fetch weather data. Please check your input and try again.")

projects/API Based Weather Report/Util_Functions.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from datetime import datetime, timedelta
2+
3+
14
def wind_degree_to_direction(str_wind_degree):
25
"""
36
Convert wind degree to wind direction.
@@ -17,3 +20,24 @@ def wind_degree_to_direction(str_wind_degree):
1720
]
1821
index = int((wind_degree + 11.25) // 22.5) % 16
1922
return directions[index]
23+
24+
25+
def unix_timestamp_to_localtime(str_unix_timestamp, str_timezone_offset_seconds):
26+
"""
27+
Convert wind degree to wind direction.
28+
29+
:param str_unix_timestamp: str, Unix timestamp (e.g., "1717715516")
30+
:param str_timezone_offset_seconds: str, timezone offset in second (e.g., "28800" represents UTC+8)
31+
:return: local_time (e.g., "2024-06-07 07:11:56")
32+
"""
33+
# Convert strings to integers
34+
unix_timestamp = int(str_unix_timestamp)
35+
timezone_offset_seconds = int(str_timezone_offset_seconds)
36+
37+
# Convert Unix timestamp to UTC datetime
38+
utc_time = datetime.utcfromtimestamp(unix_timestamp)
39+
40+
# Apply timezone offset
41+
local_time = utc_time + timedelta(seconds=timezone_offset_seconds)
42+
43+
return local_time.strftime('%Y-%m-%d %H:%M:%S')

0 commit comments

Comments
 (0)