Write a Python function to Get last NYSE open hour timestamp in unix milliseconds
To get the last New York Stock Exchange(NYSE) open hour timestamp in Unix milliseconds, we need to account for the NYSE trading hours and holidays. The NYSE typically operates from 9:30AM to 4:00PM Eastern Time(ET) Monday through Friday, but it's also closed on certain holidays.
Here's a Python function that calculates the last NYSE open hour timestamp in Unix milliseconds:/// python
from datetime import datetime, timedelta
import pytz
import holidays
def get_last_nyse_open_hour_timestamp():
# Define the NYSE timezone
eastern = pytz.timezone('US/Eastern')
# Get the current date and time inNYSE timezone
now = datetime.now(eastern)
# Define NYSE trading hours
market_open_hour =9
market_open_minute =30
market_close_hour =16
market_close_minute =0
# Fetch US holidays
us_holidays = holidays.US()
# Adjust the current time if the market is closed
whileTrue:if now.weekday()>=5 or now.date()inus_holidays: # NYSE is closed on weekends and certain holidays
now -=timedelta(days=1)
now = now.replace(hour=market_close_hour -1, minute=0, second=0, microsecond=0)
elif now.time()< datetime.time(now.replace(hour=market_open_hour, minute=market_open_minute, second=0)) or now.time()> datetime.time(now.replace(hour=market_close_hour, minute=market_close_minute, second=0)):
now = now.replace(hour=market_close_hour -1, minute=0, second=0, microsecond=0)else:break
# Convert the datetime back to Unix milliseconds
unix_timestamp =int(now.timestamp()*1000)return unix_timestamp
# Example usage
print(get_last_nyse_open_hour_timestamp())/// **Explanation:**1.**Timezone Handling**: We use `pytz` to handle timezone conversion to Eastern Time.2.**Trading Hours**: Define standard trading hours, keeping in mind holidays and weekends.3.**Holidays**: Use the `holidays` library to check if today is a US holiday when the market is closed.4.**Timestamp Calculation**: Adjust the datetime object as necessary, then convert it back to Unix milliseconds.**Dependencies**:- You’ll need to install the `pytz` and `holidays`libraries:/// shell
pip install pytz holidays
///
This function ensures that you always get the last NYSE open hour timestamp considering weekends, holidays, and trading hours.
Write a Python function to get the file size from any url
You can use the `requests` library in Python to get the file size from any URL. Below is a function which does this:/// pythonimport requests
def get_file_size(url):
response = requests.head(url) # Only get headers, not content
file_size =int(response.headers.get('content-length',-1)) # Get file size from headers
if file_size ==-1:return'Could not get the file size.'else:return file_size
///
This function performs a `HEAD` request to the URL, which retrieves metadata about the file without actually downloading the file content. It then retrieves the `Content-Length` header field, which indicates the file size in bytes.
Please note that not all servers provide the `Content-Length` header. In such cases, you will not be able to determine the file size using this method.
You can call the functionasfollows:/// pythonprint(get_file_size('https://www.example.com/path/to/file'))///
This will print out the size of the file in bytes. If the `Content-Length` header is not available, it will print `Could not get the file size.`