burmesedate.helpers
Helper Functions
1#/* cSpell:disable */ 2""" 3Helper Functions 4""" 5 6import datetime 7from typing import List 8from typing import Dict 9import pytz 10import requests 11 12 13def unix_to_julian(ut: int) -> float: 14 """ 15 Converts a Unix timestamp to a Julian date. 16 17 Args: 18 ut (int): The Unix timestamp to be converted to a Julian date. 19 20 Returns: 21 float: The Julian date corresponding to the input Unix timestamp. 22 """ 23 # number of seconds from 1970 Jan 1 00:00:00 (UTC) 24 jd = 2440587.5 + ut / 86400.0 25 return jd 26 27 28def julian_to_unix(jdn: float) -> float: 29 """ 30 Converts a Julian date to a Unix timestamp. 31 32 Args: 33 jdn (float): The Julian date to be converted to a Unix timestamp. 34 35 Returns: 36 float: The Unix timestamp corresponding to the input Julian date. 37 """ 38 unix_timestamp = (jdn - 2440587.5) * 86400.0 + 0.5 39 return unix_timestamp 40 41 42def get_local() -> dict: 43 """ 44 Retrieves the current date, time, and other information in the local timezone. 45 46 Returns: 47 dict: A dictionary with the following keys: 48 - 'ofs': the offset in seconds between UTC and the local timezone. 49 - 'ofm': the offset in minutes between UTC and the local timezone. 50 - 'ofh': the offset in hours between UTC and the local timezone. 51 - 'ofstr': the string representation of the local timezone. 52 - 'weekday': the name of the weekday based on the local date. 53 - 'datetime': the formatted date and time in the local timezone. 54 """ 55 # Get the current date and time in UTC 56 utc_now = datetime.datetime.now(pytz.utc) 57 # Convert UTC time to the local timezone 58 local_now = utc_now.astimezone() 59 offset = local_now.utcoffset() 60 offset_seconds = offset.total_seconds() 61 offset_minutes = offset_seconds / 60 62 offset_hours = offset_minutes / 60 63 offset_str = str(local_now.tzinfo) 64 weekday = local_now.weekday() 65 date_time = local_now.strftime("%c") 66 unix = local_now.timestamp() 67 68 return { 69 "ofs": offset_seconds, 70 "ofm": offset_minutes, 71 "ofh": offset_hours, 72 "ofstr": offset_str, 73 "weekday": weekday, 74 "datetime": date_time, 75 "unixtime": unix, 76 } 77 78 79def get_timezones() -> List[str]: 80 """ 81 Retrieves a list of timezones from the World Time API. 82 83 Returns: 84 List[str]: A list of timezones in JSON format. 85 """ 86 response = requests.get("http://worldtimeapi.org/api/timezone", timeout=5) 87 return response.json() 88 89 90def get_tz_info(tz: str) -> Dict[str, str]: 91 """ 92 Retrieves information about a timezone using the World Time API. 93 94 Args: 95 tz (str): The timezone abbreviation. 96 97 Returns: 98 dict: A dictionary containing information about the timezone. 99 """ 100 response = requests.get(f"http://worldtimeapi.org/api/{tz}", timeout=5) 101 info = response.json() 102 103 return { 104 "abbr": info["abbreviation"], 105 "ip": info["client_ip"], 106 "datetime": info["datetime"], 107 "day_of_week": info["day_of_week"], 108 "day_of_year": info["day_of_year"], 109 "dst": info["dst"], 110 "dst_form": info["dst_from"], 111 "dst_util": info["dst_until"], 112 "dst_offset": info["dst_offset"], 113 "raw_offset": info["raw_offset"], 114 "utc_datetime": info["utc_datetime"], 115 "unixtime": info["unixtime"], 116 "week_number": info["week_number"], 117 "utc_offset": info["utc_offset"], 118 } 119 120 121def jd_now() -> float: 122 """ 123 Calculates the Julian date corresponding to the current date and time in the local timezone. 124 125 Returns: 126 float: The Julian date corresponding to the current date and time in the local timezone. 127 """ 128 unix = get_local()["unixtime"] 129 return unix_to_julian(unix)
14def unix_to_julian(ut: int) -> float: 15 """ 16 Converts a Unix timestamp to a Julian date. 17 18 Args: 19 ut (int): The Unix timestamp to be converted to a Julian date. 20 21 Returns: 22 float: The Julian date corresponding to the input Unix timestamp. 23 """ 24 # number of seconds from 1970 Jan 1 00:00:00 (UTC) 25 jd = 2440587.5 + ut / 86400.0 26 return jd
Converts a Unix timestamp to a Julian date.
Args: ut (int): The Unix timestamp to be converted to a Julian date.
Returns: float: The Julian date corresponding to the input Unix timestamp.
29def julian_to_unix(jdn: float) -> float: 30 """ 31 Converts a Julian date to a Unix timestamp. 32 33 Args: 34 jdn (float): The Julian date to be converted to a Unix timestamp. 35 36 Returns: 37 float: The Unix timestamp corresponding to the input Julian date. 38 """ 39 unix_timestamp = (jdn - 2440587.5) * 86400.0 + 0.5 40 return unix_timestamp
Converts a Julian date to a Unix timestamp.
Args: jdn (float): The Julian date to be converted to a Unix timestamp.
Returns: float: The Unix timestamp corresponding to the input Julian date.
43def get_local() -> dict: 44 """ 45 Retrieves the current date, time, and other information in the local timezone. 46 47 Returns: 48 dict: A dictionary with the following keys: 49 - 'ofs': the offset in seconds between UTC and the local timezone. 50 - 'ofm': the offset in minutes between UTC and the local timezone. 51 - 'ofh': the offset in hours between UTC and the local timezone. 52 - 'ofstr': the string representation of the local timezone. 53 - 'weekday': the name of the weekday based on the local date. 54 - 'datetime': the formatted date and time in the local timezone. 55 """ 56 # Get the current date and time in UTC 57 utc_now = datetime.datetime.now(pytz.utc) 58 # Convert UTC time to the local timezone 59 local_now = utc_now.astimezone() 60 offset = local_now.utcoffset() 61 offset_seconds = offset.total_seconds() 62 offset_minutes = offset_seconds / 60 63 offset_hours = offset_minutes / 60 64 offset_str = str(local_now.tzinfo) 65 weekday = local_now.weekday() 66 date_time = local_now.strftime("%c") 67 unix = local_now.timestamp() 68 69 return { 70 "ofs": offset_seconds, 71 "ofm": offset_minutes, 72 "ofh": offset_hours, 73 "ofstr": offset_str, 74 "weekday": weekday, 75 "datetime": date_time, 76 "unixtime": unix, 77 }
Retrieves the current date, time, and other information in the local timezone.
Returns: dict: A dictionary with the following keys: - 'ofs': the offset in seconds between UTC and the local timezone. - 'ofm': the offset in minutes between UTC and the local timezone. - 'ofh': the offset in hours between UTC and the local timezone. - 'ofstr': the string representation of the local timezone. - 'weekday': the name of the weekday based on the local date. - 'datetime': the formatted date and time in the local timezone.
80def get_timezones() -> List[str]: 81 """ 82 Retrieves a list of timezones from the World Time API. 83 84 Returns: 85 List[str]: A list of timezones in JSON format. 86 """ 87 response = requests.get("http://worldtimeapi.org/api/timezone", timeout=5) 88 return response.json()
Retrieves a list of timezones from the World Time API.
Returns: List[str]: A list of timezones in JSON format.
91def get_tz_info(tz: str) -> Dict[str, str]: 92 """ 93 Retrieves information about a timezone using the World Time API. 94 95 Args: 96 tz (str): The timezone abbreviation. 97 98 Returns: 99 dict: A dictionary containing information about the timezone. 100 """ 101 response = requests.get(f"http://worldtimeapi.org/api/{tz}", timeout=5) 102 info = response.json() 103 104 return { 105 "abbr": info["abbreviation"], 106 "ip": info["client_ip"], 107 "datetime": info["datetime"], 108 "day_of_week": info["day_of_week"], 109 "day_of_year": info["day_of_year"], 110 "dst": info["dst"], 111 "dst_form": info["dst_from"], 112 "dst_util": info["dst_until"], 113 "dst_offset": info["dst_offset"], 114 "raw_offset": info["raw_offset"], 115 "utc_datetime": info["utc_datetime"], 116 "unixtime": info["unixtime"], 117 "week_number": info["week_number"], 118 "utc_offset": info["utc_offset"], 119 }
Retrieves information about a timezone using the World Time API.
Args: tz (str): The timezone abbreviation.
Returns: dict: A dictionary containing information about the timezone.
122def jd_now() -> float: 123 """ 124 Calculates the Julian date corresponding to the current date and time in the local timezone. 125 126 Returns: 127 float: The Julian date corresponding to the current date and time in the local timezone. 128 """ 129 unix = get_local()["unixtime"] 130 return unix_to_julian(unix)
Calculates the Julian date corresponding to the current date and time in the local timezone.
Returns: float: The Julian date corresponding to the current date and time in the local timezone.