1 | # Copyright (c) 2005 Norman Timmler (inlet media e.K., Hamburg, Germany)
|
---|
2 | # All rights reserved.
|
---|
3 | #
|
---|
4 | # Redistribution and use in source and binary forms, with or without
|
---|
5 | # modification, are permitted provided that the following conditions
|
---|
6 | # are met:
|
---|
7 | # 1. Redistributions of source code must retain the above copyright
|
---|
8 | # notice, this list of conditions and the following disclaimer.
|
---|
9 | # 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | # notice, this list of conditions and the following disclaimer in the
|
---|
11 | # documentation and/or other materials provided with the distribution.
|
---|
12 | # 3. The name of the author may not be used to endorse or promote products
|
---|
13 | # derived from this software without specific prior written permission.
|
---|
14 | #
|
---|
15 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
16 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
17 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
18 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
19 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
20 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
21 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
22 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
24 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
25 |
|
---|
26 |
|
---|
27 | class Time
|
---|
28 | alias :to_str :to_s
|
---|
29 | DAY_NAME = [
|
---|
30 | 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
|
---|
31 | ]
|
---|
32 | MONTH_NAME = [
|
---|
33 | 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
|
---|
34 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
|
---|
35 | ]
|
---|
36 | def to_s
|
---|
37 | sprintf('%s %s %d %02d:%02d:%02d GMT',
|
---|
38 | DAY_NAME[wday],
|
---|
39 | MONTH_NAME[mon-1], day,
|
---|
40 | hour, min, sec, year) +
|
---|
41 | (
|
---|
42 | off = Time.now.gmtoff
|
---|
43 | sign = off < 0 ? '-' : '+'
|
---|
44 | sprintf('%s%02d%02d', sign, *(off.abs / 60).divmod(60))
|
---|
45 | ) +
|
---|
46 | (
|
---|
47 | sprintf(' %d', year)
|
---|
48 | )
|
---|
49 | end
|
---|
50 | def to_iso
|
---|
51 | offset = Time.now.gmtoff
|
---|
52 | strftime("%Y-%m-%dT%H:%m:%S#{sprintf('%s%02d:%02d', (offset < 0 ? '-' : '+'), *(offset.abs / 60).divmod(60))}")
|
---|
53 | end
|
---|
54 | end
|
---|
55 |
|
---|
56 | class Float
|
---|
57 | alias :to_str :to_s
|
---|
58 | def to_s
|
---|
59 | to_f % 1 == 0 ? to_i.to_s : to_str
|
---|
60 | end
|
---|
61 | end
|
---|
62 |
|
---|
63 | class IO
|
---|
64 | def read__UI8(position = nil)
|
---|
65 | seek position unless position.nil?
|
---|
66 | readchar
|
---|
67 | end
|
---|
68 |
|
---|
69 | def read__UI16(position = nil)
|
---|
70 | seek position unless position.nil?
|
---|
71 | (readchar << 8) + readchar
|
---|
72 | end
|
---|
73 |
|
---|
74 | def read__UI24(position = nil)
|
---|
75 | seek position unless position.nil?
|
---|
76 | (readchar << 16) + (readchar << 8) + readchar
|
---|
77 | end
|
---|
78 |
|
---|
79 | def read__UI32(position = nil)
|
---|
80 | seek position unless position.nil?
|
---|
81 | (readchar << 24) + (readchar << 16) + (readchar << 8) + readchar
|
---|
82 | end
|
---|
83 |
|
---|
84 | def read__STRING(length, position = nil)
|
---|
85 | seek position unless position.nil?
|
---|
86 | read length
|
---|
87 | end
|
---|
88 |
|
---|
89 |
|
---|
90 | def write__UI8(value, position = nil)
|
---|
91 | seek position unless position.nil?
|
---|
92 | write [value].pack('C')
|
---|
93 | end
|
---|
94 |
|
---|
95 | def write__UI24(value, position = nil)
|
---|
96 | seek position unless position.nil?
|
---|
97 | write [value >> 16].pack('c')
|
---|
98 | write [(value >> 8) & 0xff].pack('c')
|
---|
99 | write [value & 0xff].pack('c')
|
---|
100 | end
|
---|
101 |
|
---|
102 | def write__UI32(value, position = nil)
|
---|
103 | seek position unless position.nil?
|
---|
104 | write [value].pack('N')
|
---|
105 | end
|
---|
106 |
|
---|
107 | def write__STRING(string, position = nil)
|
---|
108 | seek position unless position.nil?
|
---|
109 | write string
|
---|
110 | end
|
---|
111 | end
|
---|
112 |
|
---|
113 | class ARGFWrapper
|
---|
114 | def readchar
|
---|
115 | ARGF.readchar
|
---|
116 | end
|
---|
117 |
|
---|
118 | def read(length)
|
---|
119 | ARGF.read(length)
|
---|
120 | end
|
---|
121 |
|
---|
122 | def read__UI8
|
---|
123 | readchar
|
---|
124 | end
|
---|
125 |
|
---|
126 | def read__UI16
|
---|
127 | (readchar << 8) + readchar
|
---|
128 | end
|
---|
129 |
|
---|
130 | def read__UI24
|
---|
131 | (readchar << 16) + (readchar << 8) + readchar
|
---|
132 | end
|
---|
133 |
|
---|
134 | def read__UI32
|
---|
135 | (readchar << 24) + (readchar << 16) + (readchar << 8) + readchar
|
---|
136 | end
|
---|
137 |
|
---|
138 | def read__STRING(length)
|
---|
139 | read length
|
---|
140 | end
|
---|
141 | end
|
---|