php / php/php-src

Add a UTC-normalizing DateTime method for ISO 8601 output

Đang mở
#23,491 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Feature Status: Needs Triage
Ngôn ngữ chính
C
Star
40.4k
Fork
8.1k
Merge trung bình
2 ngày 13 giờ
Pull request đã merge (30 ngày)
96

Mô tả

Update: Based on feedback, this proposal no longer focuses on adding a
format constant. The preferred direction under discussion is a method that
normalizes the represented instant to UTC and returns ISO 8601 output with
millisecond precision and a Z suffix. Check this comment : https://github.com/php/php-src/issues/23491#issuecomment-5464152132

Spoiler old proposal ## Description

PHP provides predefined date format constants such as DATE_ISO8601,
DATE_ATOM, DATE_RFC3339, and DATE_RFC3339_EXTENDED. There is no predefined
format for the UTC timestamp commonly produced by JavaScript:

1970-01-01T00:00:00.000Z

For example:

new Date(0).toISOString();
// "1970-01-01T00:00:00.000Z"

Proposal

Add a predefined format for ISO 8601 timestamps with millisecond precision.

One possible name is:

DATE_ISO8601_MILLISECONDS_UTC
DateTimeInterface::ISO8601_MILLISECONDS_UTC

with the format string:

"Y-m-d\\TH:i:s.v\\Z"

The name and API shape are open for discussion.

An alternative is an offset-preserving format:

DATE_ISO8601_MILLISECONDS
DateTimeInterface::ISO8601_MILLISECONDS

using:

"Y-m-d\\TH:i:s.vP"

Example

$date = new DateTimeImmutable('@0');

echo $date->setTimezone(new DateTimeZone('UTC'))
    ->format(DATE_ISO8601_MILLISECONDS_UTC);

// 1970-01-01T00:00:00.000Z

As with the existing date format constants, formatting does not change the
timezone of the DateTime object. The caller must convert it to UTC before
using the _UTC format.

Existing constant

DATE_RFC3339_EXTENDED already includes milliseconds, but produces an offset:

1970-01-01T00:00:00.000+00:00

This is semantically equivalent to Z for UTC, but is not byte-for-byte
compatible with JavaScript's Date.prototype.toISOString().

Related discussion

PHP issue #14593 discusses the
interpretation of the Z suffix when parsing DateTime values. That issue is
about parsing and timezone representation; this proposal is about predefined
formatting constants in ext/date.

Questions

  • Is a millisecond-precision constant useful in addition to
    DATE_RFC3339_EXTENDED?
  • Should the format preserve the object's offset, or provide the Z form for
    UTC-normalized values?
  • What name and API shape should be used?
  • Would both forms introduce unnecessary overlap with the existing constants?

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng việc xem xét các hằng số định dạng ngày hiện có và hành vi của DateTime trong ext/date, đặc biệt là DATE_RFC3339_EXTENDED và việc chuyển đổi UTC. Đọc bình luận được liên kết để hiểu hướng triển khai phương thức được ưu tiên; công việc được xem là hoàn tất khi hình dạng API đã được thống nhất và phương thức tạo ra đầu ra ISO 8601 được chuẩn hóa theo UTC với độ chính xác đến mili giây và hậu tố Z.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
php
Lĩnh vực
backend
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
45/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.