purescript-contrib/purescript-formatters

format unformat of {12,0} {am,pm}, 24

Closed this issue · 4 comments

I was looking at adjustMeridiem function it seemed a bit strange and then i took look at moment.js behaviour:

moment("0 AM", "h a").format() // "2017-07-14T00:00:00+04:00"
moment("0 PM", "h a").format() // "2017-07-14T12:00:00+04:00"
moment("12 PM", "h a").format() // "2017-07-14T12:00:00+04:00"
moment("12 AM", "h a").format() // "2017-07-14T00:00:00+04:00"

moment("00", "hh").format() // "2017-07-14T00:00:00+04:00"
moment("12", "hh").format() // "2017-07-14T12:00:00+04:00"
moment("24", "hh").format() // "2017-07-15T00:00:00+04:00"

moment("00", "hh").format("h a") // "12 am"
moment("12", "hh").format("h a") // "12 pm"
moment("24", "hh").format("h a") // "12 am"

And our implementation clearly does not match it.

Having range like validateRange 0 59 for minute and seconds matches moment behaviour:

moment("60", "mm").format() // "Invalid date"
moment("60", "ss").format() // "Invalid date"

But as we use validateRange 0 23 and validateRange 0 11 for hour we behave in different way (i.e. can't parse input containing 24 or 12). is this what we want?

If we want to match moment then we should change adjustMeridiem to something like this:

adjustMeridiem  Int  Meridiem  Int
adjustMeridiem 12 AM = 0
adjustMeridiem 12 PM = 12
adjustMeridiem n  PM = n + 12
adjustMeridiem n  AM = n

and change validateRanges to include 24 and 12

<?php
$fmt = "Y-m-d H:i:s"

print(date_create_from_format($fmt , "2009-02-15 24:00:00")->format($fmt)); // 2009-02-16 00:00:00
print(date_create_from_format($fmt , "2009-02-15 24:00:00")->format($fmt)); // 2009-02-16 00:00:00
print(date_create_from_format($fmt , "2009-02-15 00:00:00")->format($fmt)); // 2009-02-15 00:00:00
print(date_create_from_format($fmt , "2009-02-15 01:00:00")->format($fmt)); // 2009-02-15 01:00:00

php also does day shift if hour is 24

garyb commented

I don't think we should do that - it makes many more invalid dates possible, like 24:16:00. Moment rejects that also.

garyb commented

Oh nevermind... if we're just talking about parsing, then it's okay I guess. 24 is an unrepresentable hour component in datetime anyway.

@garyb take a look at #26