arrow-py / arrow

🏹 Better dates & times for Python
https://arrow.readthedocs.io
Apache License 2.0
8.7k stars 673 forks source link

Day of the week as number 'd' does not format back to day of the week #1072

Open Swizzler121 opened 2 years ago

Swizzler121 commented 2 years ago

Issue Description

When formatting from a date as an integer, it does not always translate correctly.

example code:

#!/usr/bin/env python3

import arrow

d = '3'
d2 = '198911'
wkday = arrow.get(d, 'd')
wkday2 = arrow.get(d2, 'YYYYMD')
print(f'sample 1 Fetched date short, (date should be a Wednesday): {wkday}')
print(f'sample 2 Fetched date long (date should be a Sunday): {wkday2}')
wkday = wkday.format('dddd')
wkday2 = wkday2.format('d')
print(f'sample 1 Formatted long (should be the word Wednesday): {wkday}')
print(f'sample 2 Formatted short (should be 7 for Sunday): {wkday2}')
wkday2 = wkday2.format('dddd')
print(f'sample 2 Formatted long (should be the word Sunday): {wkday2}')

example output:

sample 1 Fetched date short, (date should be a Wednesday): 0001-01-01T00:00:00+00:00
sample 2 Fetched date long (date should be a Sunday): 1989-01-01T00:00:00+00:00
sample 1 Formatted long (should be the word Wednesday): Monday
sample 2 Formatted short (should be 7 for Sunday): 7
sample 2 Formatted long (should be the word Sunday): 7

Sample 1: Single digit input As you can see, when using arrow.get to fetch day of the week as an integer, no matter what number you give it, it instead grabs the date of 0001-01-01 which is a Sunday, but then more confusingly, it formats 0001-01-01 as a Monday, so I assume it's just seeing 1, even though it was originally fed a 3.

Sample 2: Valid date input While you can convert a valid date to day of the week then again down to a number 1-7 Mon-Sun, you cannot then convert back from a number to a day of the week again.

System Info

Swizzler121 commented 2 years ago

Ran the example code on my windows system just to show it's not one particular environment:

sample 1 Fetched date short, (date should be a Wednesday): 0001-01-01T00:00:00+00:00
sample 2 Fetched date long (date should be a Sunday): 1989-01-01T00:00:00+00:00
sample 1 Formatted long (should be the word Wednesday): Monday
sample 2 Formatted short (should be 7 for Sunday): 7
sample 2 Formatted long (should be the word Sunday): 7

System Info

systemcatch commented 2 years ago

Hey @Swizzler121 when I try your second example I get the following result.

>>> dt=arrow.get('198911', 'YYYYMD')
>>> dt
<Arrow [1989-01-01T00:00:00+00:00]>
>>> dt.format("d")
'7'
>>> dt.format("dddd")
'Sunday'

I think wkday2 is being overridden at some point.

For the first example I can see the bug, @jadchaar I think we worked on this at one point?