MSSQL/Storage/DateTime/Format.pm

filter: Statementstmt 100% 49 / 49 Branchbran 65.0% 13 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 92.2% 83 / 90 CC 11 SCAR 9.8
CC
11
Cov
90%
parse_datetimeoffset
5.3
parse_datetime2
3.1
parse_time
3.1
CRAP 2.7
« MSSQL/SQLMaker.pm ↑ Summary MSSQL/Storage/StopAtFirstError.pm »
1 package DBIO::MSSQL::Storage::DateTime::Format;
2 # ABSTRACT: DateTime parser for MSSQL date, time, datetime, smalldatetime, datetime2 and datetimeoffset columns
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.900007';
8  
9 2 ▶use base 'DBIO::Storage::DateTimeFormat';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # MSSQL adds a third channel beyond the base datetime/date: smalldatetime
12 # (minute precision, no fractional seconds). Core's InflateColumn::DateTime
13 # routes it to parse_smalldatetime/format_smalldatetime when the parser
14 # ->can() them, otherwise falls back to parse_datetime/format_datetime.
15 2 __PACKAGE__->mk_group_accessors('inherited' => qw/
16 smalldatetime_parse_pattern smalldatetime_format_pattern
17 /);
18  
19 # Reading is what the DBD hands over (YYYY-MM-DD hh:mm:ss.mmm). Writing is
20 # YYYYMMDD hh:mm:ss[.mmm]: the dashed form of a datetime/smalldatetime literal
21 # is read by the DATEFORMAT of the session (SET LANGUAGE German/French is dmy:
22 # 2026-10-05 became 2026-05-10, 2026-10-25 failed with Msg 242), the form
23 # without dashes is read the same under every language.
24 2 __PACKAGE__->datetime_parse_pattern('%Y-%m-%d %H:%M:%S.%3N'); # %F %T
25 2 __PACKAGE__->datetime_format_pattern('%Y%m%d %H:%M:%S.%3N');
26 # date: DBD::ODBC hands over YYYY-MM-DD. Written as YYYYMMDD like the others,
27 # so no setting of the session can touch the day and month of a date.
28 2 __PACKAGE__->date_parse_pattern('%Y-%m-%d');
29 2 __PACKAGE__->date_format_pattern('%Y%m%d');
30 2 __PACKAGE__->smalldatetime_parse_pattern('%Y-%m-%d %H:%M:%S');
31 2 __PACKAGE__->smalldatetime_format_pattern('%Y%m%d %H:%M:%S');
32  
33  
34 1 ▶sub parse_smalldatetime {
Subroutine parse_smalldatetime: called CC=1 SCAR=0.0
Pod documented
35 1 my ($class, $value) = @_;
36  
37 1 return $class->_strptime_for(smalldatetime_parse => 'smalldatetime_parse_pattern')
38 ->parse_datetime($value);
39 }
40  
41  
42 1 ▶sub format_smalldatetime {
Subroutine format_smalldatetime: called CC=1 SCAR=0.0
Pod documented
43 1 my ($class, $dt) = @_;
44  
45 1 return $class->_strptime_for(smalldatetime_format => 'smalldatetime_format_pattern')
46 ->format_datetime($dt);
47 }
48  
49 # datetime2, datetimeoffset and time have a channel of their own (core's
50 # InflateColumn::DateTime routes datetime2(7) to parse_datetime2, datetimeoffset(7)
51 # to parse_datetimeoffset, time(7) to parse_time). They are read and written by
52 # hand, not by a strptime pattern: the server gives a fraction of as many digits
53 # as the scale of the column (none for time(0)), and a pattern with a fixed
54 # count of digits would refuse the rest.
55 #
56 # Writing is language independent: YYYYMMDD without dashes, the offset as
57 # +HH:MM after a blank, a time as HH:MM:SS.fffffff. Seven fractional digits are
58 # what the types take at most; the server rounds to the scale of the column.
59  
60 # $dt => 'YYYYMMDD hh:mm:ss.fffffff'
61 7 ▶sub _wall_clock {
Subroutine _wall_clock: called CC=1 SCAR=0.0
62 7 my ($class, $dt) = @_;
63 7 return sprintf('%04d%02d%02d %02d:%02d:%02d.%07d',
64 $dt->year, $dt->month, $dt->day, $dt->hour, $dt->minute, $dt->second,
65 int($dt->nanosecond / 100));
66 }
67  
68 # fraction digits as nanoseconds
69 14 ▶sub _frac_ns {
Subroutine _frac_ns: called CC=2 SCAR=6.9
70 14 my ($class, $frac) = @_;
71 14 ▶ return defined $frac ? substr($frac . '000000000', 0, 9) + 0 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $frac ? : 12 2 14
72 }
73  
74 # 'YYYY-MM-DD hh:mm:ss[.f]' (DBD::ODBC) or with a T (DBD::Sybase from TDS 7.3)
75 2 my $DATETIME_RE = qr/(\d{4})-(\d\d)-(\d\d)[ T](\d\d):(\d\d):(\d\d)(?:\.(\d{1,9}))?/;
76  
77  
78 5 ▶sub parse_datetime2 {
Subroutine parse_datetime2: called CC=3 SCAR=11.4
Pod documented
79 5 my ($class, $value) = @_;
80  
81 5 ▶ my @m = defined $value ? $value =~ /\A\s*$DATETIME_RE Z?\s*\z/xi : ()
Branch: 2 branches
BranchTrueFalseTotal
my(@m) = defined $value ? $value =~ /\A\s*$DATETIME_RE Z?\s*\z/xi : () or die "Unreadable datetime2 value\n" 5 0 5
defined $value ? : 5 0 5
82 or die "Unreadable datetime2 value\n";
83  
84 5 require DateTime;
85 5 return DateTime->new(
86 year => $m[0], month => $m[1], day => $m[2],
87 hour => $m[3], minute => $m[4], second => $m[5],
88 nanosecond => $class->_frac_ns($m[6]),
89 );
90 }
91  
92  
93 2 ▶sub format_datetime2 {
Subroutine format_datetime2: called CC=1 SCAR=0.0
Pod documented
94 2 my ($class, $dt) = @_;
95 2 return $class->_wall_clock($dt);
96 }
97  
98  
99 5 ▶sub parse_datetimeoffset {
Subroutine parse_datetimeoffset: called CC=5 SCAR=16.7
Pod documented
100 5 my ($class, $value) = @_;
101  
102 5 ▶ my @m = defined $value
Branch: 2 branches
BranchTrueFalseTotal
my(@m) = defined $value ? $value =~ /\A\s*$DATETIME_RE\s*(?:(Z)|([+-])(\d\d):?(\d\d))?\s*\z/i : () or die "Unreadable datetimeoffset value\n" 5 0 5
defined $value ? : 5 0 5
103 ? $value =~ /\A\s*$DATETIME_RE\s*(?:(Z)|([+-])(\d\d):?(\d\d))?\s*\z/i : ()
104 or die "Unreadable datetimeoffset value\n";
105  
106 5 ▶ my $zone = defined $m[7] ? 'UTC'
Branch: 2 branches
BranchTrueFalseTotal
defined $m[7] ? : 1 4 5
defined $m[8] ? : 4 0 4
107 : defined $m[8] ? sprintf('%s%02d%02d', @m[8..10])
108 : 'UTC';
109  
110 5 require DateTime;
111 5 return DateTime->new(
112 year => $m[0], month => $m[1], day => $m[2],
113 hour => $m[3], minute => $m[4], second => $m[5],
114 nanosecond => $class->_frac_ns($m[6]),
115 time_zone => $zone,
116 );
117 }
118  
119  
120 5 ▶sub format_datetimeoffset {
Subroutine format_datetimeoffset: called CC=2 SCAR=6.9
Pod documented
121 5 my ($class, $dt) = @_;
122  
123 5 my $off = $dt->offset;
124 5 ▶ my $sign = $off < 0 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$off < 0 ? : 1 4 5
125 5 $off = abs $off;
126  
127 5 return $class->_wall_clock($dt)
128 . sprintf(' %s%02d:%02d', $sign, int($off / 3600), int(($off % 3600) / 60));
129 }
130  
131  
132 4 ▶sub parse_time {
Subroutine parse_time: called CC=3 SCAR=11.4
Pod documented
133 4 my ($class, $value) = @_;
134  
135 4 ▶ my @m = defined $value ? $value =~ /\A\s*(\d\d):(\d\d):(\d\d)(?:\.(\d{1,9}))?\s*\z/ : ()
Branch: 2 branches
BranchTrueFalseTotal
my(@m) = defined $value ? $value =~ /\A\s*(\d\d):(\d\d):(\d\d)(?:\.(\d{1,9}))?\s*\z/ : () or die "Unreadable time value\n" 4 0 4
defined $value ? : 4 0 4
136 or die "Unreadable time value\n";
137  
138 4 require DateTime;
139 4 return DateTime->new(
140 year => 1970, month => 1, day => 1,
141 hour => $m[0], minute => $m[1], second => $m[2],
142 nanosecond => $class->_frac_ns($m[3]),
143 );
144 }
145  
146  
147 2 ▶sub format_time {
Subroutine format_time: called CC=1 SCAR=0.0
Pod documented
148 2 my ($class, $dt) = @_;
149 2 return sprintf('%02d:%02d:%02d.%07d', $dt->hour, $dt->minute, $dt->second,
150 int($dt->nanosecond / 100));
151 }
152  
153 2 1;
154  
155 __END__
« MSSQL/SQLMaker.pm ↑ Summary MSSQL/Storage/StopAtFirstError.pm »