• R/O
  • SSH

kink: Commit

Kink runtime


Commit MetaInfo

Revisionf7c87b96522ca7311d0de12ee725e9f73a06e4a3 (tree)
Time2020-01-19 21:57:54
Authormiyakawataku
Commitermiyakawataku

Log Message

add documentation to TIMEZONE

Change Summary

Incremental Difference

diff -r 825340a363f7 -r f7c87b96522c src/main/resources/kink-mods/kink/datetime/TIMEZONE.kn
--- a/src/main/resources/kink-mods/kink/datetime/TIMEZONE.kn Sun Jan 19 20:56:59 2020 +0900
+++ b/src/main/resources/kink-mods/kink/datetime/TIMEZONE.kn Sun Jan 19 21:57:54 2020 +0900
@@ -1,3 +1,6 @@
1+##
2+# Provides timezone type.
3+
14 :JAVA.require_from('kink/host_lang/java/')
25 :EQ.require_from('kink/')
36 :CONF.require_from('kink/conf/')
@@ -7,32 +10,116 @@
710 :ZoneOffset_class <- JAVA.class('java.time.ZoneOffset')
811 :Find_desc <- 'TIMEZONE.find(Name ...$config)'
912
13+## type timezone
14+#
15+# A timezone represents the rule of transition of UTC offset in a specific region.
16+#
17+# The set of timezones are provided by the runtime.
18+:Timezone_trait <- [
19+
20+ ## Tz.name
21+ #
22+ # “name” method returns the name of Tz, such as "Asia/Tokyo".
23+ 'name' {[:Tz]()
24+ Tz.Java_tz.call_method('getId' []).to_kink_str
25+ }
26+
27+ ## Tz.repr
28+ #
29+ # “repr” method returns a str representation of Tz, such as "Timezone(Asia/Tokyo)".
30+ 'repr' {[:Tz]()
31+ 'Timezone({})'.format(Tz.name)
32+ }
33+
34+ ## Tz1 == Tz2, Tz1 != Tz2
35+ #
36+ # Two timezone values are equal when they have the same name.
37+ ... EQ.trait{(:X :Y)
38+ X.name == Y.name
39+ }
40+]
41+
42+## TIMEZONE.find(Name ...$config)
43+#
44+# “find” tries to find the timezone specified by Name.
45+#
46+# Preconditions:
47+#
48+# • Name must be a str
49+#
50+# • $config, if specified, must be a fun which takes a conf val.
51+#
52+# The conf val provides the following methods:
53+#
54+# • C.on_present($present_cont) : uses $present_cont as the present cont.
55+# If the method is not called, CORE.id is used as the default present cont.
56+#
57+# • C.on_absent($absent_cont) : uses $absent_cont as the absent cont.
58+# If the method is not called,
59+# the default absent cont raises an exception when the timezone is not found.
60+#
61+# Result:
62+#
63+# • If the timezone is found, “find” tail-calls the present cont with the timezone.
64+#
65+# • If the timezone is not found, “find” tail-calls the absent cont with no arg.
66+#
67+# Example:
68+#
69+# :TIMEZONE.require_from('kink/datetime/')
70+# :CONTROL.require_from('kink/')
71+#
72+# stdout.print_line(TIMEZONE.find('Asia/Tokyo').repr) # => Timezone(Asia/Tokyo)
73+#
74+# CONTROL.try(
75+# { TIMEZONE.find('No/Such_Tz') }
76+# { raise('doesn''t reach here') }
77+# {(:Msg :Traces) stdout.print_line(Msg) }
78+# )
79+# # => TIMEZONE.find(Name ...$config): timezone not found: "No/Such_Tz"
80+#
81+# Example specifying continuations:
82+#
83+# :TIMEZONE.require_from('kink/datetime/')
84+# :CONTROL.require_from('kink/')
85+#
86+# TIMEZONE.find('Asia/Tokyo'){(:C)
87+# C.on_present{(:Tz) stdout.print_line(Tz.repr) }
88+# }
89+# # => Timezone(Asia/Tokyo)
90+#
91+# TIMEZONE.find('No/Such_Tz'){(:C)
92+# C.on_absent{() stdout.print_line('no such tz') }
93+# }
94+# # => no such tz
1095 :find <- {(:Name :Maybe_config.opt)
1196 STR.str?(Name) || raise('{}: Name must be a str, but got {}'.format(Find_desc Name.repr))
12- [:present_conf :absent_conf] = _read_find_conf(Name Maybe_config.just_or{ {} })
97+ [:present_cont :absent_cont] = _read_find_conf(Name Maybe_config.just_or{ {} })
1398 ZoneId_class.call_static('of' [JAVA.string(Name)]){(:C)
1499 C.on_success{(:Java_tz)
15100 Java_tz.typable_as?(ZoneOffset_class).if_else(
16- { absent_conf }
17- { present_conf(_new_timezone(Java_tz)) }
101+ { absent_cont }
102+ { present_cont(_new_timezone(Java_tz)) }
18103 )
19104 }
20- C.on_error{(:Exc) absent_conf }
105+ C.on_error{(:Exc) absent_cont }
21106 }
22107 }
23108
109+# read conf val of TIMEZONE.find
24110 :_read_find_conf <- {(:Name :config)
25111 :Conf = CONF.read($config){(:C)
26112 C.req_arg('on_present')
27113 C.req_arg('on_absent')
28114 }
29- :present_conf = Conf.get_or('on_present'){ $id }
30- :absent_conf = Conf.get_or('on_absent'){
115+ :present_cont = Conf.get_or('on_present'){ $id }
116+ :absent_cont = Conf.get_or('on_absent'){
31117 { raise('{}: timezone not found: {}'.format(Find_desc Name.repr)) }
32118 }
33- [$present_conf $absent_conf]
119+ [$present_cont $absent_cont]
34120 }
35121
122+# make a timezone with java ZoneId instance
36123 :_new_timezone <- {(:Java_tz)
37124 new_val(
38125 ... Timezone_trait
@@ -40,20 +127,9 @@
40127 )
41128 }
42129
43-:Timezone_trait <- [
44- 'name' {[:Tz]()
45- Tz.Java_tz.call_method('getId' []).to_kink_str
46- }
47-
48- 'repr' {[:Tz]()
49- 'Timezone({})'.format(Tz.name)
50- }
51-
52- ... EQ.trait{(:X :Y)
53- X.name == Y.name
54- }
55-]
56-
130+## TIMEZONE.timezone?(Val)
131+#
132+# timezone? returns whether Val is a timezone.
57133 :timezone? <- {(:Val)
58134 Val:Java_tz.nonempty?
59135 }
Show on old repository browser