Giter VIP home page Giter VIP logo

Comments (2)

Micro-sheep avatar Micro-sheep commented on August 28, 2024

You need extra conversion . Here is an example for converting Beijing time to EST time

from datetime import datetime, timedelta
import efinance as ef


def time_convert(date: str, hours: int, time_format: str = None) -> str:
    """


    Parameters
    ----------
    date : str
        date to be converted
    hours : int
        hours = (dest_time - src_time).hours
    time_format : str, optional
        date format

    Returns
    -------
    str

    Examples
    --------
    >>> date = '2021-01-01 08:00' # Beijing Time 
    >>> date_est = time_convert(date,-13)
    >>> date_est # EST
    '2020-12-31 19:00'

    """    
    if time_format is None:
        formats = [
            '%Y-%m-%d %H:%M:%S',
            '%Y/%m/%d %H:%M:%S',
            '%Y-%m-%d %H:%M',
            '%Y/%m/%d %H:%M',
            '%Y-%m-%d',
            '%Y/%m/%d']
        for idx, format in enumerate(formats):
            try:
                datetime.strptime(date, format)
                time_format = format
                break
            except Exception as e:
                if idx == len(formats)-1:
                    raise e
    return (datetime.strptime(
        date, time_format)+timedelta(hours=hours)).strftime(time_format)


df = ef.stock.get_quote_history('MSFT', klt=5)
# hours = (dest_time - src_time).hours
# Beijing => UTC + 8
# EST => UTC - 5
# so (EST - Beijing).hours = -13
hours = -13
df['日期'] = df['日期'].apply(lambda x: time_convert(x, hours))
# print(df['日期'])

from efinance.

ssqd2022 avatar ssqd2022 commented on August 28, 2024

Thank you so much for your help!!!

from efinance.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.