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
810
gotandajs-11-nodejs-recursive-readdir
arayaryoma
2
470
var, let, const and immutable of JavaScript
arayaryoma
0
240
2018-07-31-angular-tips
arayaryoma
1
120
Other Decks in Programming
See All in Programming
ノーコードツールの裏側につきまとう「20分岐」との戦い
oguemon
0
200
Agentic Applications with Symfony
el_stoffel
2
200
サービスクラスのありがたみを発見したときの思い出 #phpcon_odawara
77web
4
160
gen_statem - OTP's Unsung Hero
whatyouhide
1
190
Coding Experience Cpp vs Csharp - meetup app osaka@9
harukasao
0
640
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
810
タイムゾーンの奥地は思ったよりも闇深いかもしれない
suguruooki
1
240
OpenTelemetryを活用したObservability入門 / Introduction to Observability with OpenTelemetry
seike460
PRO
1
400
Fluent UI Blazor 5 (alpha)の紹介
tomokusaba
0
160
Preact、HooksとSignalsの両立 / Preact: Harmonizing Hooks and Signals
ssssota
1
1.2k
AHC 044 混合整数計画ソルバー解法
kiri8128
0
320
いまさら聞けない生成AI入門: 「生成AIを高速キャッチアップ」
soh9834
14
4.2k
Featured
See All Featured
Six Lessons from altMBA
skipperchong
27
3.7k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.4k
Agile that works and the tools we love
rasmusluckow
328
21k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
25k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
118
51k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
49k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
46
2.4k
4 Signs Your Business is Dying
shpigford
183
22k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Large-scale JavaScript Application Architecture
addyosmani
511
110k
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