Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
What about Temporal in JavaScript
Search
Araya
February 07, 2019
Programming
5
1.5k
What about Temporal in JavaScript
Temporalについてまとめています。
Meguro.es #19のLTでトークした際のスライドです。
Araya
February 07, 2019
Tweet
Share
More Decks by Araya
See All by Araya
Performance and cache strategy at NIKKEI
arayaryoma
0
2.4k
JavaScript Promise API in 2019
arayaryoma
1
820
gotandajs-11-nodejs-recursive-readdir
arayaryoma
2
480
var, let, const and immutable of JavaScript
arayaryoma
0
250
2018-07-31-angular-tips
arayaryoma
1
120
Other Decks in Programming
See All in Programming
スモールスタートで始めるためのLambda×モノリス(Lambdalith)
akihisaikeda
2
390
カオスに立ち向かう小規模チームの装備の選択〜フルスタックTSという装備の強み _ 弱み〜/Choosing equipment for a small team facing chaos ~ Strengths and weaknesses of full-stack TS~
bitkey
1
140
파급효과: From AI to Android Development
l2hyunwoo
0
160
開発者フレンドリーで顧客も満足?Platformの秘密
algoartis
0
200
データと事例で振り返るDevin導入の"リアル" / The Realities of Devin Reflected in Data and Case Studies
rkaga
1
420
個人開発の学生アプリが企業譲渡されるまで
akidon0000
2
1.2k
監視 やばい
syossan27
12
10k
音声プラットフォームのアーキテクチャ変遷から学ぶ、クラウドネイティブなバッチ処理 (20250422_CNDS2025_Batch_Architecture)
thousanda
0
410
flutter_kaigi_mini_4.pdf
nobu74658
0
150
Lambda(Python)の リファクタリングが好きなんです
komakichi
5
260
エンジニア向けCursor勉強会 @ SmartHR
yukisnow1823
3
12k
AWS Summit Hong Kong 2025: Reinventing Programming - How AI Transforms Our Enterprise Coding Approach
dwchiang
0
130
Featured
See All Featured
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
137
33k
Building a Modern Day E-commerce SEO Strategy
aleyda
40
7.2k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Embracing the Ebb and Flow
colly
85
4.7k
Making the Leap to Tech Lead
cromwellryan
133
9.3k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
The Cult of Friendly URLs
andyhume
78
6.3k
Transcript
Temporal について Temporal について あらや @arayaryoma Meguro.es #19 @ oRo
自己紹介 自己紹介 あらや/Ryoma Abe あらや/Ryoma Abe 合同会社ヘマタイト/ フリーランス 合同会社ヘマタイト/ フリーランス
@arayaryoma @arayaryoma JavaScript/TypeScript/Go/Kotlin 好き JavaScript/TypeScript/Go/Kotlin 好き Angular/React/ReactNative/Next.js Angular/React/ReactNative/Next.js
話すこと 話すこと Temporal とはなにか なぜ提案されているのか 使い方 注意点 議論されている内容
Temporal とは Temporal とは ECMAScript に出されているproposal の1 つ(Stage2) 一言でいうとDate よりも日付・時間を扱いやすくするためのもの
Microsoft の方3 名・Bloomberg の方1 名の計4 名 がChampion ISO8601, IANA などの規格に準拠している Proposal: polyfill: https://github.com/tc39/proposal-temporal https://github.com/pipobscure/tc39-proposal-temporal
なぜproposal が出されたか なぜproposal が出されたか JavaScript のDate object が扱いづらい
Date の問題点 Date の問題点 Date object がタイムゾーンをサポートしていない Date の計算が扱いづらい グレゴリオ暦以外のカレンダーがサポートされていない
parser が貧弱かつ無効なdate format 等について標準化されていない mutable である より https://maggiepint.com/2017/04/09/fixingjavascriptdategettingstarted/
Date の問題点 Date の問題点 Date object がタイムゾーンをサポートしていない Date の計算が扱いづらい グレゴリオ暦以外のカレンダーがサポートされていない
parser が貧弱かつ無効なdate format 等について標準化されていない mutable である より https://maggiepint.com/2017/04/09/fixingjavascriptdategettingstarted/ Date への機能追加で対応できる 挙動を変えてしまうため、新しいobject がほしい
おまけ: なんでこんな使いづらいの? おまけ: なんでこんな使いづらいの? より https://maggiepint.com/2017/04/09/fixingjavascriptdategettingstarted/ Brendan Eich がJavaScript を10
日で作り上げた時に、 Java のjava.Util.Date の実装をコピーした ただ、当時のjava.Util.Date は非常に使いづらく、 事実それらのほとんどが2 年後のJava1.1 でdeprecated となった
Temporal の用語 Temporal の用語 Civil Civil Instant Instant UTC と関連性を持たないオブジェクト
UTC と関連性を持つオブジェクト CivilDate CivilTime CivilDateTime Instant ZonedInstant
使い方: 使い方:CivilDate const date = new CivilDate(2019, 2, 7); console.log(date.year,
date.month, date.day); // -> 2019 2 7 console.log(date.dayOfWeek); // -> 4 // Monday - Sunday : 1 - 7 ISO 8601 準拠!! const newDate = date.plus({years: 1, months: 2, days: 30}); console.log(date.year, date.month, date.day); // -> 2019 2 7 console.log(newDate.year, newDate.month, newDate.day); // -> 2020 5 7 console.log(date.toDateString()); // -> "2019-02-07"
使い方: 使い方:CivilTime const time = new CivilTime(22, 45, 0, 0,
0, 0); console.log(time.hour, time.minute, time.second, time.millisecond, time.microsecond, time.nanosecond); // -> 22 45 0 0 0 0 const newTime = time.plus({hours: 1, minutes: 20}); console.log(time.hour, time.minute, time.second); // -> 22 45 0 console.log(newTime.hour, newTime.minute, newTime.second); // -> 0 5 0 console.log(time.toString()); // -> "22:45:00.000000000" const dateTime = time.withDate(new CivilDate(2018, 2, 7)); // CiviDate object を返す
使い方: 使い方:CivilDateTime const dateTime = new CivilDateTime(2019, 2, 7, 22,
45, 0); const newDateTime = dateTime.plus({years: 1, hours: 1}); console.log(dateTime.toString()); // -> "2019-02-07T22:45:00.000000000" console.log(newDateTime.toString()); // -> "2020-02-07T23:45:00.000000000" const zoned = dateTime.withZone('Asia/Tokyo'); // ZonedInstant object
使い方: 使い方:Instant const instant = new Instant(1549547100000) const instantWithNanoSec =
new Instant(1549547100000000000n) // Polifyll のdoc ではnano 秒をBigInt で渡す仕様になっている const zoned = instant.withZone('Asia/Tokyo'); // ZonedInstant object console.log(instant.toString()); // -> "2019-02-07T22:45:00.000000000Z"
使い方: 使い方:ZonedInstant const instant = new Instant(1549547100000) const zoned =
new ZonedInstant(instant, 'Asia/Tokyo'); const date = zoned.toCivilDate(); const time = zoned.toCivilTime(); const dateTime = zoned.toCivilDateTime(); const str = zoned.toString(); // "2019-02-07T22:45:00.000000000+09:00[Asia/Tokyo]" const newZoned = zoned.fromString(str); // fromString はtoString で返されうる文字列のみをサポート
議論されていること 議論されていること https://github.com/tc39/proposal-temporal/tree/master/meetings Temporal のAPI をDate にも登載するようなproposal を出すか否か Temporal にフォーカスすべき(
全会一致) Temporal って名前変えたほうがいい?批判受けたことある? No. どちらかというとCivil prefix への文句が多い。 Civil prefix については議論しましょう。 2 月末に仕様完成させたい。その後@std/temporal としてpolyfill をnpm に publish できるから最高。 入力として有効なものと無効なものを完全に定義したい。
使う際の注意点 使う際の注意点 仕様がまだ定まっていなく、doc の項目がTBD だったり polifyll とspec で合わない箇所がザラにある そもそもproposal が落ちる可能性あり
format を指定して文字列をparse するといったことはできない。 ( 例えばWeb API が日付を"2019/02/07" で返してきた時など) // Go だとこういうことができる t, _ = time.Parse("2006/01/02", "2019/02/07")
まとめ まとめ Temporal はDate の失敗を繰り返さないためのmodule 実用するにはまだ厳しい 気になるproposal のmeeting note などを読むと、策定者たちが何を考えているのか見
られるのでオススメ // import standard library import { CivilDateTime, ZonedInstant } from 'std:Temporal'; const dateTimeTokyo = new CivilDateTime(2019, 2, 7, 22, 45); const instantInTokyo = dateTimeInTokyo.withZone('Asia/Tokyo'); const instantInSydney = new ZonedInstant(instantInTokyo.instant, 'Australia/Sydney'); const dateTimeInSydney = instantInSydney.toCivilDateTime(); dateTimeInTokyo.toString(); // '2019-02-07T22:45:00.000000000' dateTimeInSydney.toString(); // '2019-02-07T20:45:00.000000000' 20xx 年のJavaScript(??)
参考資料 参考資料 https://github.com/tc39/proposal-temporal https://github.com/pipobscure/tc39-proposal-temporal https://maggiepint.com/2017/04/09/fixing-javascript-date-getting-started/ https://en.wikipedia.org/wiki/ISO_8601