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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Araya
February 07, 2019
Programming
5
1.6k
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.6k
JavaScript Promise API in 2019
arayaryoma
1
860
gotandajs-11-nodejs-recursive-readdir
arayaryoma
2
520
var, let, const and immutable of JavaScript
arayaryoma
0
290
2018-07-31-angular-tips
arayaryoma
1
130
Other Decks in Programming
See All in Programming
Claude Codeログ基盤の構築
giginet
PRO
7
3.5k
CS教育のDX AIによる育成の効率化
niftycorp
PRO
0
150
Fundamentals of Software Engineering In the Age of AI
therealdanvega
2
290
ふつうの Rubyist、ちいさなデバイス、大きな一年
bash0c7
0
1.1k
AIに任せる範囲を安全に広げるためにやっていること
fukucheee
0
150
AI時代のシステム設計:ドメインモデルで変更しやすさを守る設計戦略
masuda220
PRO
6
1.1k
安いハードウェアでVulkan
fadis
0
730
Kubernetesでセルフホストが簡単なNewSQLを求めて / Seeking a NewSQL Database That's Simple to Self-Host on Kubernetes
nnaka2992
0
170
最初からAWS CDKで技術検証してもいいんじゃない?
akihisaikeda
4
160
new(1.26) ← これすき / kamakura.go #8
utgwkk
0
2.6k
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
1.1k
PHPで TLSのプロトコルを実装してみる
higaki_program
0
380
Featured
See All Featured
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
160
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
800
Deep Space Network (abreviated)
tonyrice
0
94
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.8k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
980
Are puppies a ranking factor?
jonoalderson
1
3.1k
Designing for Timeless Needs
cassininazir
0
170
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
We Are The Robots
honzajavorek
0
200
Paper Plane
katiecoart
PRO
0
48k
WENDY [Excerpt]
tessaabrams
9
37k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
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