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

Add proper support for datetime.time objects/ time-only cell formats #86

Open
fzumstein opened this issue Oct 10, 2014 · 8 comments · May be fixed by #2162
Open

Add proper support for datetime.time objects/ time-only cell formats #86

fzumstein opened this issue Oct 10, 2014 · 8 comments · May be fixed by #2162

Comments

@fzumstein
Copy link
Member

On Mac, this is taken care of by appscript, but on Windows, _datetime_to_com_time doesn't handle this yet.

@fzumstein
Copy link
Member Author

Note that appscript uses current date if only time is given

@fzumstein fzumstein changed the title Win: add support for datetime.time objects Add proper support for datetime.time objects Mar 22, 2015
@fzumstein fzumstein changed the title Add proper support for datetime.time objects Add proper support for datetime.time objects/ time-only cell formats Mar 22, 2015
@fzumstein
Copy link
Member Author

fzumstein commented Mar 22, 2015

Excel to Python returns a float in any case, not a datetime object.

@fzumstein fzumstein added this to the bluesky milestone Jun 30, 2016
@fzumstein fzumstein modified the milestones: bluesky, short list Jul 12, 2016
@fzumstein fzumstein removed this from the short list milestone Aug 11, 2019
@rudolfbyker
Copy link

I'm looking to return datetime.time objects from Python to VBA. I just want to check, is this the right issue to address that?

@xw.func
def test_time():
    return datetime.time(1, 2, 3)

@xw.func
def test_date():
    return datetime.date(2001, 2, 3)

@xw.func
def test_datetime():
    return datetime.datetime(2001, 2, 3, 4, 5, 6)

image

@fzumstein
Copy link
Member Author

Basically, the issue is that Excel doesn't have a time-only type. A time-only cell has the date 1/1/1900, so you should be able to send over a datetime object with 1/1/1900 as the date and then assign the right number_format to format it as time-only.

@rudolfbyker
Copy link

Thanks for the workaround! Any chance of getting that into _datetime_to_com_time as default behaviour? I agree it's weird, but if that's the only way Excel can represent time-only things, then that's probably what we should use.

I'm trying to make a PR but failing miserably so far. I can't figure out where the error originates.

@rudolfbyker
Copy link

I added this to _datetime_to_com_time:

    if type(dt_time) is dt.time:
        # Excel does not have a time-only data type. It stores times with date `1900/01/01`.
        dt_time = dt.datetime(1900, 1, 1, dt_time.hour, dt_time.minute, dt_time.second,
                              tzinfo=win32timezone.TimeZoneInfo.utc())

but it looks like the error is thrown before _datetime_to_com_time is reached.

@fzumstein
Copy link
Member Author

you'll also need to add dt.time to
https://github.com/xlwings/xlwings/blob/main/xlwings/_xlwindows.py#L91

or it's not triggering that conversion in:
https://github.com/xlwings/xlwings/blob/main/xlwings/_xlwindows.py#L459

rudolfbyker added a commit to rudolfbyker/xlwings that referenced this issue Feb 7, 2023
@heetbeet
Copy link

heetbeet commented Aug 9, 2023

This feature will be really helpful, thanks

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

Successfully merging a pull request may close this issue.

3 participants