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

Extended date format ISO8601 validation - feature #17

Closed
jarekkozak opened this issue Oct 1, 2014 · 2 comments
Closed

Extended date format ISO8601 validation - feature #17

jarekkozak opened this issue Oct 1, 2014 · 2 comments

Comments

@jarekkozak
Copy link
Contributor

Hi again,
I'm wondering why Moment accepts ISO8601 format in limited way, only YYYY-MM-DDTHH:mm:ss?
It would be more useful if Moment would accept full ISO8601. DateTime accepts all ISO formats,
I investigated a bit code and found that isValid method is responsible for that.
I have rewrited it and now it works as I would like it to do. If you find it interesting , you can use it.

protected function isValidDate()
    {
        $rawDateTime = $this->getRawDateTimeString();

        if (strpos($rawDateTime, '-') === false) {
            return true;
        }

        // ----------------------------------
        // time with indicator "T"
        if (strpos($rawDateTime, 'T') !== false) {
            //We remove fraction if any ... DateTime holds only seconds
            $rawDateTime = preg_replace('/\.[0-9][0-9][0-9]/', '', $rawDateTime);
            $rawTimeZone = substr($rawDateTime, 19);
            if ($rawTimeZone && strpos($rawTimeZone, '+') !== FALSE) {
                if (substr_count($rawTimeZone, ':') > 0) {
                    $momentDateTime = $this->format('Y-m-d\TH:i:sP');
                } else {
                    $momentDateTime = $this->format('Y-m-d\TH:i:sO');
                }
            } elseif ($rawTimeZone) {
                $momentDateTime = $this->format('Y-m-d\TH:i:se');
            } else {
                $momentDateTime = $this->format('Y-m-d\TH:i:s');
            }
        } // time without indicator "T"
        elseif (strpos($rawDateTime, ':') !== false) {
            if (substr_count($rawDateTime, ':') === 2) { // with seconds
                $momentDateTime = $this->format('Y-m-d H:i:s');
            } else {
                $momentDateTime = $this->format('Y-m-d H:i');
            }
        } // without time
        else {
            $momentDateTime = $this->format('Y-m-d');
        }

        return $rawDateTime === $momentDateTime;
    }

and tests

    public function testMoment(){
        $data = '1923-12-31 12:30:00';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.000';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.123';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.123+02:00';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0200', $m->format());

        $data = '1923-12-31T12:30:00.123+0200';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0200', $m->format());

        $data = '1923-12-31T12:30:00.123Z';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.123Europe/Warsaw';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0100', $m->format());

        $data = '1923-12-31T12:30:00.123Europe/Warsaw';
        $m = new Moment($data,'UTC');
        $this->assertEquals('1923-12-31T12:30:00+0100', $m->format());

        $data = '1923-12-31T12:30:00.123UTC';
        $m = new Moment($data,'Europe/Warsaw');
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $this->tester->printVar($m->format());
        //$this->assertEquals('1923-12-31T12:30:Z', $m->format());

    }

Best regards,
Jarek

@fightbulc
Copy link
Owner

Hey Jarek. Can u create a pull request? I'll have a look later and merge it
in if all seems fine.

Thanks.

Cheers
Tino

On Wednesday, October 1, 2014, jarekkozak notifications@github.com wrote:

Hi again,
I'm wondering why Moment accepts ISO8601 format in limited way, only
YYYY-MM-DDTHH:mm:ss?
It would be more useful if Moment would accept full ISO8601. DateTime
accepts all ISO formats,
I investigated a bit code and found that isValid method is responsible for
that.
I have rewrited it and now it works as I would like it to do. If you find
it interesting , you can use it.

protected function isValidDate()
{
$rawDateTime = $this->getRawDateTimeString();

    if (strpos($rawDateTime, '-') === false) {
        return true;
    }

    // ----------------------------------
    // time with indicator "T"
    if (strpos($rawDateTime, 'T') !== false) {
        //We remove fraction if any ... DateTime holds only seconds
        $rawDateTime = preg_replace('/\.[0-9][0-9][0-9]/', '', $rawDateTime);
        $rawTimeZone = substr($rawDateTime, 19);
        if ($rawTimeZone && strpos($rawTimeZone, '+') !== FALSE) {
            if (substr_count($rawTimeZone, ':') > 0) {
                $momentDateTime = $this->format('Y-m-d\TH:i:sP');
            } else {
                $momentDateTime = $this->format('Y-m-d\TH:i:sO');
            }
        } elseif ($rawTimeZone) {
            $momentDateTime = $this->format('Y-m-d\TH:i:se');
        } else {
            $momentDateTime = $this->format('Y-m-d\TH:i:s');
        }
    } // time without indicator "T"
    elseif (strpos($rawDateTime, ':') !== false) {
        if (substr_count($rawDateTime, ':') === 2) { // with seconds
            $momentDateTime = $this->format('Y-m-d H:i:s');
        } else {
            $momentDateTime = $this->format('Y-m-d H:i');
        }
    } // without time
    else {
        $momentDateTime = $this->format('Y-m-d');
    }

    return $rawDateTime === $momentDateTime;
}

and tests

public function testMoment(){
    $data = '1923-12-31 12:30:00';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

    $data = '1923-12-31T12:30:00.000';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

    $data = '1923-12-31T12:30:00.123';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

    $data = '1923-12-31T12:30:00.123+02:00';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0200', $m->format());

    $data = '1923-12-31T12:30:00.123+0200';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0200', $m->format());

    $data = '1923-12-31T12:30:00.123Z';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

    $data = '1923-12-31T12:30:00.123Europe/Warsaw';
    $m = new Moment($data);
    $this->assertEquals('1923-12-31T12:30:00+0100', $m->format());

    $data = '1923-12-31T12:30:00.123Europe/Warsaw';
    $m = new Moment($data,'UTC');
    $this->assertEquals('1923-12-31T12:30:00+0100', $m->format());

    $data = '1923-12-31T12:30:00.123UTC';
    $m = new Moment($data,'Europe/Warsaw');
    $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

    $this->tester->printVar($m->format());
    //$this->assertEquals('1923-12-31T12:30:Z', $m->format());

}

Best regards,
Jarek


Reply to this email directly or view it on GitHub
#17.

Tino Ehrich

EFIDES E-SERVICES
web solutions powered by experience

www.efides.com

{ "Beauty doesn't mean to be caught
by the eye but rather to be kept in mind." }

Giorgio Armani

@fightbulc
Copy link
Owner

note to myself: http://en.wikipedia.org/wiki/ISO_8601

fightbulc pushed a commit that referenced this issue Oct 5, 2014
- fixing #16 and #17
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