amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Create amp-date-display

Open aghassemi opened this issue 9 years ago • 31 comments

Similar components: amp-date-countdown https://github.com/ampproject/amphtml/issues/10385 See presentation for a summary.

Requirements

  • Ability to specify a UTC date (ISO string or timestamp in ms or timestamp in seconds)
  • Ability to specify a local date (including now)
  • Ability to convert between utc/local
  • Ability to offset the date by x seconds
  • Ability to use CSS to style each part (number and text) of the date being display
  • Locale support

Examples

UTC

<amp-date-display datetime=”2017-08-02T15:15:15.000Z”>
// Wed Aug 02 2017 15:15:15 (UTC)

<amp-date-display datetime=”2017-08-02T15:15:15.000+01:00”>
// Wed Aug 02 2017 14:15:15 (UTC)

<amp-date-display datetime=”2017-08-02T15:15:15.000Z” display-in=”local”>
// Wed Aug 02 2017 08:15:15 (PDT)

Local

<amp-date-display datetime=”2017-08-02T15:15:15.000 ”>
// Wed Aug 02 2017 15:15:15 (PDT)

<amp-date-display datetime=”now”>
// Wed Aug 02 2017 13:10:10 (PDT) 

<amp-date-display datetime=”2017-08-03 ”>
// Wed Aug 03 2017 13:10:10 (PDT)

<amp-date-display datetime=”2017-08-02T15:15:15.000 ” display-in=”utc”>
// Wed Aug 02 2017 22:15:15 GMT-0700 (UTC)

Offset

<amp-date-display datetime=”now” offset-seconds=”172800”> // 2-days
// Wed Aug 04 2017 13:10:10 (PDT) (assume it is 1:30 PM right now)

<amp-date-display datetime=”2017-08-02T15:15:15.000Z” offset-seconds=”172800”> // 2-days
// Wed Aug 04 2017 15:15:15 (UTC)

<amp-date-display datetime=”2017-08-02T15:15:15.000Z” offset-seconds=”172800” display-in=”local”> // 2-days

// Wed Aug 04 2017 08:15:15 (PDT)

Format Design

<amp-date-display 
  datetime=”ISO with optional time/timezone offset”
  timestamp-ms="POSIX epoch in ms"
  timestamp-seconds="POSIX epoch in seconds" 
  display-in=”local|utc”
  offset-seconds=”seconds”
  locale=”en-US”
  layout="OnlySizedDefinedLayouts">
 <template type="amp-mustache">
    <div>{{dayName}}, {{monthNameShort}} {{day}} {{year}}</div>
    <div>{{hour12TwoDigit}}:{{minuteTwoDigit}}:{{secondTwoDigit}} {{dayPeriod}}</div>
 </template>
</amp-countdown>

possible outputs

Wednesday, Aug  2017
01:15:15 PM

Template Variables

This proposal handles "don't want to show 0" easily using mustache sections (e.g. {{#day}}) and does not need anything for grouping which are big advantages.

Variables Meaning
year 2017
yearTwoDigit 17
month month - 0, 1, 2, 3..., 12
monthTwoDigits month - 00, 01, 02, 03...12
monthName January - i18n string for long month name
monthNameShort JAN - i18n string for short month name
<Similar for day> ....
hour 0-23
hourTwoDigit 00-23
hour12 1-12
hour12TwoDigit 01-12
<Similar for minute, seconds> ...
dayPeriod AM - i18n string for AM/PM

Attributes

datetime

  • One of now or <ISO datetime> with OPTIONAL time and timezone offset
  • If no time/timezone specified, assumes local otherwise utc

timestamp-ms

  • POSIX epoch value in milliseconds - will be assumed to be UTC

timestamp-seconds

  • POSIX epoch value in seconds - will be assumed to be UTC (essentially Unix time: https://en.wikipedia.org/wiki/Unix_time)

Note: One of datetime, timestamp-ms, timestamp-seconds is required.

offset-seconds

  • Negative or poissive number of seconds to add/subtract from datetime

display-in (Defaults to local)

  • One of utc or local. _ Converts the date to either locale or utc

Open questions

  • This scheme does not support locale-aware formatting e.g. DD/MM/YY vs MM/DD/YY
  • Can we rely on JS IntlAPI? How good is the browser locale support? How big is the polyfill?
  • If we use JS Intl, should we also expose {{localeStandardDateShort}} {{localeStandardDateLong}} that would resolve to locale-aware formatting? (e.g. {{localeStandardDateShort}} -> 3/20/2017 for en-US or 20/3/2017 for en-CA)

aghassemi avatar Aug 08 '17 19:08 aghassemi

/cc @choumx @robinpokorny @ericlindley-g

aghassemi avatar Aug 08 '17 19:08 aghassemi

@robinpokorny Thanks for taking this on! Couple of resources to get you started:

aghassemi avatar Aug 08 '17 22:08 aghassemi

@aghassemi After some initial investigation, I found a small problem. If we want to use the native Date implementation–which is otherwise sufficient and convinient–it would be difficult to distinguish if the original input was in UTC or not.

So I would suggest using the default behaviour of Date.parse and showing all dates in local by default and use the attribute to show it in UTC (or the other way around).

robinpokorny avatar Aug 09 '17 19:08 robinpokorny

Can we allow the date to be specified in epoch time (seconds and ms versions). The reason is that a lot of the existing API endpoints we deal with send dates that way and support for epoch time would allow us to render them via amp-list without changing the endpoint.

jpettitt avatar Aug 09 '17 19:08 jpettitt

@choumx @robinpokorny (RE: https://github.com/ampproject/amphtml/issues/10385#issuecomment-321381124) I looked at moment.js and several date format libraries in JS and also JS's internalization API. Unfortunately JS's Intl API (which I was hoping to use for this component rather than including a 3P library) doesn't support format strings.

Intl API dooes however support options that can be used to construct locale-aware named formats such as fullDate shortDate longDate etc.. I think exposing parts and also these pre-formatted locale-aware formats as variables should cover most cases.

aghassemi avatar Aug 10 '17 16:08 aghassemi

@jpettitt Added timestamp-ms="POXIS epoch in ms" timestamp-seconds="POXIS epoch in seconds" to the spec. Thanks for bringing it up importance of supporting different backends.

@robinpokorny Changed convert-to to display-in

If I've missed any of your suggestions from the review meeting, please let me know.

aghassemi avatar Aug 10 '17 16:08 aghassemi

@aghassemi in this comment the name for ms version is just timestamp. What do you think is better?

robinpokorny avatar Aug 10 '17 16:08 robinpokorny

@robinpokorny Changed to timestamp-ms everywhere. Initially had it as timestamp and timestamp-seconds but realized timestamp is ambiguous (well, Unix timestamp is in seconds, but JS getTime is in ms)

aghassemi avatar Aug 10 '17 17:08 aghassemi

I think you mean POSIX not POXIS - you may also want to link to this https://en.wikipedia.org/wiki/Unix_time

jpettitt avatar Aug 11 '17 17:08 jpettitt

@jpettitt updated.

@robinpokorny When we endup using JS Intl API to allow for locale-aware formats like localeStandardDateShort ( e.g. 3/20/2017 for en-US or 20/3/2017 for en-CA ), another feature we can add to make life easier is this:

Attribute format: One of template| localeStandardDateShort | localeStandardDateLong | etc..

This way, user does not need to use amp-mustache in all cases and for simple date displays where the whole thing can be a single string in a single DOM node, they can use format instead.

I wish JS Intl API had the concept of format strings like moment.js and, well, other languages though :(

aghassemi avatar Aug 11 '17 17:08 aghassemi

If you are interested I have a WIP implementation in this branch: https://github.com/robinpokorny/amphtml/tree/add-amp-date-display.

robinpokorny avatar Aug 15 '17 22:08 robinpokorny

@robinpokorny Just wanted to touch base and see how this is progressing.

aghassemi avatar Sep 19 '17 17:09 aghassemi

@aghassemi I have a working branch, I got stuck with the tests. I will continue with it.

robinpokorny avatar Sep 26 '17 08:09 robinpokorny

another feature request for this: https://github.com/ampproject/amphtml/issues/12036

aghassemi avatar Nov 14 '17 18:11 aghassemi

screenshot 2017-11-14 21 23 54


@aghassemi, this is the screenshot of the code I have.

What is missing:

  • [ ] validator-amp-date-display.protoascii (all)
  • [ ] amp-date-display.md (all)
  • [ ] test/test-amp-date-display.js (all)

robinpokorny avatar Nov 14 '17 20:11 robinpokorny

Hi,

This looks very interesting and I was asking about this kind of thing in https://github.com/ampproject/amphtml/issues/12036 and pointed to https://github.com/ampproject/amp-by-example/issues/719 as a workaround for the short term.

Obviously this solution being proposed would be a better way to solve the problem I have is there anything I can do to help?

My issue is just to be able to take a time and covert it to local time for the user based on their system local time the time is simply 3pm UTC (4pm CET / 10am EST)

The question I have is how efficient will this approach be if I have 200 times on a page all of which need to be converted - where there are probably on 20 different actual times. This is for start times of matches where many matches start at the same time and the possible start times are limited.

QES avatar Nov 15 '17 09:11 QES

@QES this component will be at least a few months out. The workaround with with amp-list does batching, so if multiple amp-lists with the same src get fired at once, only a single call happens. This means you can create a single endpoint and pass TIMEZONE to it and have it return a JSON with all the 20 so dates converted. Each amp-list then can target a specific key in that JSON result to render the converted date.

aghassemi avatar Nov 15 '17 23:11 aghassemi

@aghassemi thanks - will take a look at that. It might be a possible work around.

QES avatar Nov 16 '17 13:11 QES

Hi, any updates on this component? Thanks!

mcmd avatar Apr 03 '18 15:04 mcmd

@mcmd, no, there's been no progress. I believe the component functionality is implemented. I couldn't get pass some docs-related issues.

robinpokorny avatar Apr 09 '18 11:04 robinpokorny