Skip to content

Commit 6ca3c17

Browse files
committed
FEAT: added UNIXTIME codec for conversion of Rebol datetime! to Unix epoch number and back
Example: ``` encode 'unixtime 18-Sep-2019/9:06:45+2:00 ;== 1568790405 decode 'unixtime 1568790405 ;== 18-Sep-2019/9:06:45+2:00 ``` string! and binary! is also supported as an input/output: ``` encode/as 'unixtime 18-Sep-2019/9:06:45+2:00 string! ;== "5D81D785" encode/as 'unixtime 18-Sep-2019/9:06:45+2:00 binary! ;== #{5D81D785} decode 'unixtime "5D81D785" ;== 18-Sep-2019/9:06:45+2:00 decode 'unixtime #{5D81D785} ;== 18-Sep-2019/9:06:45+2:00 ```
1 parent 5cc415c commit 6ca3c17

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

src/mezz/boot-files.r

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ REBOL [
4949
%mezz-crypt.r
5050
%mezz-date.r ; Internet date support
5151
%mezz-tail.r
52+
%codec-unixtime.r
5253
%codec-der.r
5354
%codec-crt.r
5455
%codec-gzip.r

src/mezz/codec-unixtime.r

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
REBOL [
2+
title: "REBOL 3 codec for Unix time stamp"
3+
name: 'codec-unixtime
4+
author: "Oldes"
5+
version: 0.1.0
6+
date: 17-Sep-2019
7+
history: [
8+
0.1.0 17-Sep-2019 "Oldes" {Initial version}
9+
]
10+
urls: [
11+
https://en.wikipedia.org/wiki/Unix_time
12+
https://www.unixtimestamp.com
13+
]
14+
]
15+
16+
register-codec [
17+
name: 'unixtime
18+
title: "Unix time stamp converter"
19+
suffixes: []
20+
21+
decode: function [
22+
"Return date from unix time format"
23+
epoch [number! string! binary!] "Date in unix time format (string is uspposed to be in base-16 format)"
24+
/utc {Will not add time zone}
25+
][
26+
if string? epoch [ epoch: debase/base epoch 16 ]
27+
if binary? epoch [ epoch: to integer! epoch ]
28+
days: to integer! tmp: epoch / 86400
29+
hours: to integer! time: (tmp - days) * 24
30+
minutes: to integer! tmp: (time - hours) * 60
31+
seconds: to integer! 0.5 + ((tmp - minutes) * 60)
32+
time: to time! ((((hours * 60) + minutes) * 60) + seconds)
33+
result: 1-Jan-1970 + days + time
34+
unless utc [
35+
result: result + now/zone
36+
result/zone: now/zone
37+
]
38+
result
39+
]
40+
41+
encode: function [
42+
"Encode unix (epoch) time"
43+
date [date!]
44+
/as type [word! datatype!] {one of: [string! binary! integer!]}
45+
][
46+
time: any [date/time 0:0:0]
47+
unix: ((date - 1-1-1970) * 86400)
48+
+ (time/hour * 3600)
49+
+ (time/minute * 60)
50+
+ time/second
51+
- to integer! (any [date/zone 0])
52+
if as [
53+
type: to word! type
54+
binary/write bin: #{} [ui32 :unix]
55+
switch type [
56+
binary! [ return bin ]
57+
string! [ return enbase/base bin 16]
58+
integer! [ return unix ] ; just for consistency
59+
]
60+
cause-error 'script 'invalid-arg type
61+
]
62+
unix
63+
]
64+
]

src/mezz/sys-codec.r

+6-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ register-codec: function [
3838
decode: function [
3939
{Decodes a series of bytes into the related datatype (e.g. image!).}
4040
type [word!] {Media type (jpeg, png, etc.)}
41-
data [binary! string!] {The data to decode}
41+
data {The data to decode}
4242
][
4343
unless all [
4444
cod: select system/codecs type
@@ -62,7 +62,8 @@ encode: function [
6262
{Encodes a datatype (e.g. image!) into a series of bytes.}
6363
type [word!] {Media type (jpeg, png, etc.)}
6464
data {The data to encode}
65-
/options opts [block!] {Special encoding options}
65+
/as {Special encoding options}
66+
options {Value specific to type of codec}
6667
][
6768
unless all [
6869
cod: select system/codecs type
@@ -74,7 +75,9 @@ encode: function [
7475
do-codec cod/entry 'encode data
7576
][
7677
either function? try [:cod/encode][
77-
cod/encode data
78+
either as [
79+
apply :cod/encode [data 'as options]
80+
][ cod/encode data ]
7881
][
7982
cause-error 'internal 'not-done type
8083
]

src/tests/units/codecs-test.r3

+28
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,32 @@ if find system/codecs 'tar [
171171
system/codecs/tar/verbose: 1
172172
]
173173

174+
if find system/codecs 'unixtime [
175+
===start-group=== "unixtime codec"
176+
date: 18-Sep-2019/8:52:31+2:00
177+
--test-- "encode 32bit unixtime"
178+
--assert 1568789551 = encode 'unixtime date
179+
--assert 1568789551 = system/codecs/unixtime/encode date
180+
--assert "5D81D42F" = encode/as 'unixtime date string!
181+
--assert #{5D81D42F} = encode/as 'unixtime date binary!
182+
--assert 1568789551 = encode/as 'unixtime date integer!
183+
--assert error? try [ encode/as 'unixtime date url! ]
184+
185+
--test-- "decode 32bit unixtime"
186+
--assert date = decode 'unixtime 1568789551
187+
--assert date = decode 'unixtime "5D81D42F"
188+
--assert date = decode 'unixtime #{5D81D42F}
189+
--assert date = system/codecs/unixtime/decode 1568789551
190+
191+
date: 1-1-2056/1:2:3
192+
--test-- "encode 64bit unixtime"
193+
--assert 2713914123 = encode 'unixtime date
194+
--assert "A1C30B0B" = encode/as 'unixtime date string!
195+
196+
--test-- "decode 64bit unixtime"
197+
--assert date = decode 'unixtime 2713914123
198+
--assert date = decode 'unixtime "A1C30B0B"
199+
===end-group===
200+
]
201+
174202
~~~end-file~~~

0 commit comments

Comments
 (0)