Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

time_zone problème #629

Open
roshii-11 opened this issue Oct 14, 2023 · 3 comments
Open

time_zone problème #629

roshii-11 opened this issue Oct 14, 2023 · 3 comments

Comments

@roshii-11
Copy link

il y a un problème dans le time_zone de investpy pour les annonces économiques :

time_zone = "GMT +2:00"

A la première exécution du code tous va bien ,
puis dans les autres ça affiche gmt +1
alors que je n'ai rien modifier !

Comment régler ce problème svp

exemple de ce que j'ai dans le CMD :

       id        date   time  ... actual forecast previous
0  483491  17/10/2023  14:30  ...   None     0.1%     0.6%
1  483490  17/10/2023  14:30  ...   None     0.2%     0.6%
2  483567  18/10/2023  14:30  ...   None   1.450M   1.541M
3  484582  18/10/2023  16:30  ...   None     None  10.176M

Et puis d'un coup sans raison et sans modification du code dans la même minute :

       id        date   time  ... actual forecast previous
0  483491  17/10/2023  13:30  ...   None     0.1%     0.6%
1  483490  17/10/2023  13:30  ...   None     0.2%     0.6%
2  483567  18/10/2023  13:30  ...   None   1.450M   1.541M
3  484582  18/10/2023  15:30  ...   None     None  10.176M
@aropele
Copy link

aropele commented Oct 15, 2023

Same here

@roshii-11
Copy link
Author

Pareil ici

merci j'ai réussi a régler le problème , dans le fichier > investpy\utils\constant.py
J'ai modifier le dictionnaire TIMEZONES par ces bonnes valeurs > #331 (comment)

@FrizzTradez
Copy link

If your having trouble with time zones and inconsistent times, you will need to scrape the time zone values off of Investing.com/economic-calendar. I've wrote a script to do exactly that, simply 'pip install BeautifulSoup4' and Run.

`import requests
from bs4 import BeautifulSoup
import re

def scrape_timezones():
url = "https://www.investing.com/economic-calendar/"

headers = {
    'User-Agent': (
        'Mozilla/5.0 (Windows NT 10.0; Win64 x64) '
        'AppleWebKit/537.36 (KHTML, Like Gecko) '
        'Chrome/58.0.3029.110 Safari/537.3'
    )
}

try:
    response = requests.get(url, headers=headers)
    response.raise_for_status()
except requests.exceptions.RequestException as e:
    print(f"Error fetching the page: {e}")
    return
soup = BeautifulSoup(response.content, 'html.parser')

timezone_div = soup.find('div', id='economicCurrentTimePop')
if not timezone_div:
    print("Timezone div with id 'economicCurrentTimePop' not found")
    return 
ul = timezone_div.find('ul', class_='js-scrollable-block')
if not ul:
    print("Unordered list with class 'js-scrollable-block' not found")
    return

TIMEZONES = {}

Iterate over all list items with class 'addRow'

for li in ul.find_all('li', class_='addRow'):
    id_attr = li.get('id', '')
    
    # Extract the number after 'liTz' using regex
    match_id = re.match(r'liTz(\d+)', id_attr)
    if not match_id:
        continue  # Skip if the id does not match the pattern

    tz_number = int(match_id.group(1))

    # Extract the text content of the list item
    text = li.get_text(strip=True)

    # Extract the GMT offset using regex
    match_tz = re.match(r'\(GMT\s*([+-]?\d{1,2}:\d{2})?\)', text)
    if match_tz:
        offset = match_tz.group(1)
        if offset:
            timezone = f"GMT {offset}"
        else:
            timezone = "GMT"
    else:
        # Handle cases where the format might be different
        print(f"Unrecognized timezone format: '{text}'")
        continue

    # Append the tz_number to the corresponding timezone in the dictionary
    if timezone not in TIMEZONES:
        TIMEZONES[timezone] = []
    TIMEZONES[timezone].append(tz_number)

# Sort the dictionary by GMT offset for better readability
sorted_TIMEZONES = dict(sorted(
    TIMEZONES.items(),
    key=lambda item: parse_gmt_offset(item[0])
))

# Print the TIMEZONES dictionary in the desired format
print("TIMEZONES = {")
for tz, numbers in sorted_TIMEZONES.items():
    numbers_sorted = sorted(numbers)
    numbers_str = ', '.join(map(str, numbers_sorted))
    print(f"    '{tz}': [{numbers_str}],")
print("}")

def parse_gmt_offset(tz_str):
"""
Helper function to convert GMT offset string to a sortable value.
For example:
'GMT -11:00' -> -11.0
'GMT +5:30' -> 5.5
'GMT' -> 0
"""
if tz_str == "GMT":
return 0
match = re.match(r'GMT\s*([+-]?)(\d{1,2}):(\d{2})', tz_str)
if not match:
return 0 # Default to 0 if unrecognized
sign = -1 if match.group(1) == '-' else 1
hours = int(match.group(2))
minutes = int(match.group(3))
return sign * (hours + minutes / 60)

if name == "main":
scrape_timezones()`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants