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

support AM/PM in date parsing/printing #32308

Merged
merged 11 commits into from
Jun 16, 2019
Merged

support AM/PM in date parsing/printing #32308

merged 11 commits into from
Jun 16, 2019

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Jun 12, 2019

Support AM/PM specifier (p dateformat code, and I code for 12-hour clock) similar to strftime/strptime. Closes #23870.

Follows the quasi-standard convention of 12am=midnight (of the following day), 12pm=noon (apparently followed by all other popular libraries and languages, as discussed in #23870).

julia> Time("8:10pm", dateformat"I:MMp")
20:10:00

julia> Dates.format(ans, dateformat"II:MM p")
"08:10 PM"

@stevengj stevengj added dates Dates, times, and the Dates stdlib module needs tests Unit tests are required for this change needs news A NEWS entry is required for this change labels Jun 12, 2019
@stevengj stevengj requested a review from quinnj June 12, 2019 20:33
@galenlynch
Copy link
Contributor

12am=midnight (of the following day)

I don't understand what you mean by "of the following day". Wouldn't "2019-03-22 12:00:00 AM" be the first second of 2019-03-22?

@stevengj
Copy link
Member Author

stevengj commented Jun 13, 2019

I don't understand what you mean by "of the following day". Wouldn't "2019-03-22 12:00:00 AM" be the first second of 2019-03-22?

Yes.

julia> DateTime("2019-03-22 12:00 AM", dateformat"yyyy-mm-dd II:MM p")
2019-03-22T00:00:00

By the "following" day I mean that 12am belongs to the day after midnight, not to the day before, so it is equivalent to 00:00 in a 24-hour clock for the same date (not 24:00).

@stevengj stevengj removed needs news A NEWS entry is required for this change needs tests Unit tests are required for this change labels Jun 13, 2019
@@ -161,6 +173,11 @@ for (tok, fn) in zip("uU", [monthabbr, monthname])
end
end

function format(io, d::DatePart{'p'}, dt, locale)
ampm = hour(dt) < 12 ? "AM" : "PM" # fixme: locale-specific?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell from some googling, there are only a handful of english-speaking countries that use AM/PM, so yeah, I'm not quite sure how critical it is to support locale here. For reference (https://www.timeanddate.com/time/am-and-pm.html)

@stevengj stevengj added this to the 1.3 milestone Jun 15, 2019
@stevengj stevengj added the triage This should be discussed on a triage call label Jun 15, 2019
@stevengj
Copy link
Member Author

Should be good to merge?

@fredrikekre fredrikekre added the needs compat annotation Add !!! compat "Julia x.y" to the docstring label Jun 15, 2019
@quinnj quinnj merged commit 092d695 into master Jun 16, 2019
@quinnj quinnj deleted the sgj/ampm branch June 16, 2019 02:51
@omus
Copy link
Member

omus commented Jul 22, 2019

It would have been good to use a as the specifier for AM/PM which is used in the Unicode Technical Standard #35. I'll probably open a PR for this.

@stevengj
Copy link
Member Author

stevengj commented Jul 22, 2019

@omus, the p comes from the POSIX strftime, which seems more consistent with our other specifiers. e.g. we use M for minute and m for month ala POSIX, whereas UTS35 is the reverse.

@omus
Copy link
Member

omus commented Aug 1, 2019

Completely reasonable. UTS35 has a standard on what using multiple specifiers in a row does which is a bit different from strftime. Following POSIX here is probably the right thing to do for now.

@Keno Keno removed the triage This should be discussed on a triage call label Feb 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dates Dates, times, and the Dates stdlib module needs compat annotation Add !!! compat "Julia x.y" to the docstring
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add am/pm to DateFormat
6 participants