Page Menu
Home
GRNET
Search
Configure Global Search
Log In
Files
F449144
iso8601_duration.rb
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Apr 24, 3:29 PM
Size
1 KB
Mime Type
text/x-ruby
Expires
Sat, Apr 26, 3:29 PM (1 d, 16 h)
Engine
blob
Format
Raw Data
Handle
212566
Attached To
rWEBDNS WebDNS (edet4)
iso8601_duration.rb
View Options
require
'application_helper'
module
Iso8601Duration
extend
self
TIME_PERIODS
=
ApplicationHelper
::
TIME_PERIODS
.
invert
def
to_seconds
(
duration
)
total
=
0
atomize
(
duration
)
.
each
{
|
k
,
val
|
next
unless
secs
=
TIME_PERIODS
[
k
.
to_s
]
total
+=
secs
.
to_i
*
val
}
total
end
def
atomize
(
input
)
duration
=
parse
(
input
)
components
=
parse_tokens
(
duration
)
components
.
delete
(
:time
)
# clean time capture
components
end
def
parse_tokens
(
tokens
)
components
=
tokens
.
names
.
zip
(
tokens
.
captures
)
.
map!
do
|
k
,
v
|
value
=
v
.
nil?
?
v
:
v
.
tr
(
','
,
'.'
)
[
k
.
to_sym
,
value
.
to_i
]
end
Hash
[
components
]
end
def
parse
(
input
)
input
.
match
(
/^
(?<sign>\+|-)?
P(?:
(?:
(?:(?<year>\d+(?:[,.]\d+)?)Y)?
(?:(?<month>\d+(?:[.,]\d+)?)M)?
(?:(?<day>\d+(?:[.,]\d+)?)D)?
(?<time>T
(?:(?<hour>\d+(?:[.,]\d+)?)H)?
(?:(?<minute>\d+(?:[.,]\d+)?)M)?
(?:(?<second>\d+(?:[.,]\d+)?)S)?
)?
) |
(?<week>\d+(?:[.,]\d+)?W)
) # Duration
$/x
)
end
end
Event Timeline
Log In to Comment