Skip to content

Commit 4695f4f

Browse files
committed
Extracting Date, Time and Datetime from ruby-hl7
The methods "to_hl7" used to be on ruby-hl7 file. Also, did some refactoring for reducing the code duplication. Before that, I've added some test.
1 parent b3b8769 commit 4695f4f

File tree

6 files changed

+102
-52
lines changed

6 files changed

+102
-52
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ gem 'technicalpickles-jeweler'
88
group :test, :development do
99
gem 'rcov'
1010
gem 'rspec', '< 2.0.0'
11+
gem 'debugger'
1112
end

Gemfile.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ GEM
22
remote: http://gems.github.com/
33
remote: http://rubygems.org/
44
specs:
5+
columnize (0.3.6)
6+
debugger (1.3.0)
7+
columnize (>= 0.3.1)
8+
debugger-linecache (~> 1.1.1)
9+
debugger-ruby_core_source (~> 1.1.7)
10+
debugger-linecache (1.1.2)
11+
debugger-ruby_core_source (>= 1.1.1)
12+
debugger-ruby_core_source (1.1.7)
513
git (1.2.5)
614
json_pure (1.5.0)
715
rake (0.8.7)
@@ -17,6 +25,7 @@ PLATFORMS
1725
ruby
1826

1927
DEPENDENCIES
28+
debugger
2029
rake
2130
rcov
2231
rspec (< 2.0.0)

lib/core_ext/date_time.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
module HL7Time
2+
# Get a HL7 timestamp (type TS) for a Time instance.
3+
#
4+
# fraction_digits:: specifies a number of digits of fractional seconds.
5+
# Its default value is 0.
6+
#
7+
# Time.parse('01:23').to_hl7
8+
# => "20091202012300"
9+
# Time.now.to_hl7(3)
10+
# => "20091202153652.302"
11+
def to_hl7( fraction_digits = 0)
12+
strftime('%Y%m%d%H%M%S') + hl7_fractions(fraction_digits)
13+
end
14+
15+
private
16+
def hl7_fractions(fraction_digits = 0)
17+
return '' unless fraction_digits > 0
18+
time_fraction = hl7_time_fraction
19+
answer = '.' + sprintf('%06d', time_fraction) + '0' * ((fraction_digits - 6)).abs
20+
answer[0, 1 + fraction_digits]
21+
end
22+
23+
def hl7_time_fraction
24+
if respond_to? :usec
25+
usec
26+
else
27+
sec_fraction.to_f * 1000000
28+
end
29+
end
30+
end
31+
32+
class Date
33+
# Get a HL7 timestamp (type TS) for a Date instance.
34+
#
35+
# Date.parse('2009-12-02').to_hl7
36+
# => "20091202"
37+
def to_hl7
38+
strftime('%Y%m%d')
39+
end
40+
end
41+
42+
class Time
43+
include HL7Time
44+
end
45+
46+
class DateTime
47+
include HL7Time
48+
end
49+
File renamed without changes.

lib/ruby-hl7.rb

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -677,58 +677,6 @@ def write_field( name, value ) #:nodoc:
677677

678678
end
679679

680-
class Date
681-
# Get a HL7 timestamp (type TS) for a Date instance.
682-
#
683-
# Date.parse('2009-12-02').to_hl7
684-
# => "20091202"
685-
def to_hl7
686-
strftime('%Y%m%d')
687-
end
688-
end
689-
690-
class Time
691-
# Get a HL7 timestamp (type TS) for a Time instance.
692-
#
693-
# fraction_digits:: specifies a number of digits of fractional seconds. Its default value is 0.
694-
#
695-
# Time.parse('01:23').to_hl7
696-
# => "20091202012300"
697-
# Time.now.to_hl7(3)
698-
# => "20091202153652.302"
699-
def to_hl7( fraction_digits = 0)
700-
strftime('%Y%m%d%H%M%S') +
701-
if fraction_digits == 0
702-
''
703-
elsif fraction_digits <= 6
704-
'.' + sprintf('%06d', usec)[0, fraction_digits]
705-
else
706-
'.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
707-
end
708-
end
709-
end
710-
711-
class DateTime
712-
# Get a HL7 timestamp (type TS) for a Time instance.
713-
#
714-
# fraction_digits:: specifies a number of digits of fractional seconds. Its default value is 0.
715-
#
716-
# Time.parse('01:23').to_hl7
717-
# => "20091202012300"
718-
# Time.now.to_hl7(3)
719-
# => "20091202153652.302"
720-
def to_hl7( fraction_digits = 0)
721-
strftime('%Y%m%d%H%M%S') +
722-
if fraction_digits == 0
723-
''
724-
elsif fraction_digits <= 6
725-
'.' + sprintf('%06d', usec)[0, fraction_digits]
726-
else
727-
'.' + sprintf('%06d', usec) + '0' * (fraction_digits - 6)
728-
end
729-
end
730-
end
731-
732680
# parse an hl7 formatted date
733681
#def Date.from_hl7( hl7_date )
734682
#end

spec/core_ext/date_time_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require File.expand_path('../../../lib/core_ext/date_time', __FILE__)
2+
3+
describe Date do
4+
5+
subject{ Date.parse('2013-12-02').to_hl7 }
6+
7+
it "should respond to the HL7 timestamp" do
8+
subject.should eq "20131202"
9+
end
10+
end
11+
12+
describe "to_hl7 for time related classes" do
13+
14+
let(:formated_time){ time_now.strftime('%Y%m%d%H%M%S') }
15+
let(:fraction_3){ "." + sprintf('%06d', time_now.to_time.usec)[0, 3] }
16+
let(:fraction_9){ "." + sprintf('%06d', time_now.to_time.usec) + ("0" * 3) }
17+
18+
shared_examples "a time to_hl7" do
19+
context "without fraction" do
20+
it { time_now.to_time.to_hl7.should eq formated_time }
21+
end
22+
23+
context "with_fraction" do
24+
it { time_now.to_time.to_hl7(3).should eq formated_time + fraction_3 }
25+
26+
it { time_now.to_time.to_hl7(9).should eq formated_time + fraction_9 }
27+
end
28+
end
29+
30+
describe Time do
31+
let(:time_now){ Time.now }
32+
33+
it_should_behave_like "a time to_hl7"
34+
end
35+
36+
describe DateTime do
37+
let(:time_now){ DateTime.now }
38+
39+
it_should_behave_like "a time to_hl7"
40+
end
41+
42+
end
43+

0 commit comments

Comments
 (0)