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

Parsing AM/PM #101

Closed
brewin opened this issue Jul 14, 2020 · 6 comments
Closed

Parsing AM/PM #101

brewin opened this issue Jul 14, 2020 · 6 comments

Comments

@brewin
Copy link
Contributor

brewin commented Jul 14, 2020

Thanks for this useful library! I'm trying to parse a string with AM/PM, but amPm() doesn't seem to have any effect.

val parser = dateTimeParser {
    year(4)
    +'-'
    monthNumber(2)
    +'-'
    dayOfMonth(2)
    +' '
    hourOfDay(2)
    +':'
    minuteOfHour(2)
    +' '
    amPm()
}

val hour = "2020-07-14 03:56 PM".toDateTime(parser).hour.toString()
println(hour)

I expected hour to be 15, but it's 3. Is this a bug or am I missing something?

@erikc5000
Copy link
Owner

You probably want to use clockHourOfAmPm() instead of hourOfDay().

@erikc5000
Copy link
Owner

Oh wait - that's not in yet. I've been working on a major overhaul to formatting/parsing. There's no field associated with any of the other types of hours beyond the hour of the day. Might be able to add a quick workaround in the next patch release.

@brewin
Copy link
Contributor Author

brewin commented Jul 14, 2020

No problem. I'll just add a workaround to my code for now. I'll just go ahead and close this then since you're working on an overhaul.

@brewin brewin closed this as completed Jul 14, 2020
@erikc5000
Copy link
Owner

Workaround: instead of using hourOfDay(2), you can use this code to apply a transform to convert the clock-hour-of-am-pm to hour-of-day:

wholeNumber(2) {
    onParsed { value ->
        val hourOfAmPm = value % 12
        fields[DateTimeField.HOUR_OF_DAY] = if (hourOfAmPm == 0) 12 else hourOfAmPm 
    }
}

@brewin
Copy link
Contributor Author

brewin commented Jul 15, 2020

Thanks. This does the opposite though, right? I need to convert clock-hour-of-am-pm to hour-of-day based on am-pm-of-day. My ugly workaround is to use different parsers based on whether the string contains "AM" or "PM". The PM parser adds 12 to hour-of-day.

@erikc5000
Copy link
Owner

Bah -- I ripped the code for formatting instead of parsing, so that obviously doesn't work. What you're doing sounds right -- at least until the derived fields are handled better as part of the resolving process.

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

2 participants