blib/lib/Langertha/Moment.pm

filter: Statementstmt 65.6% 44 / 67 Branchbran 45.4% 20 / 44 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 80.0% 12 / 15 Podpod 100% 3 / 3 totaltotal 58.9% 82 / 139 CC 25 SCAR 24.1
CC
25
Cov
56%
from_wire
20.1
from_wire
20.1
_compare_numeric
6.7
CRAP 11.2
« blib/lib/Langertha/Virtual/Server.pm ↑ Summary blib/lib/Langertha/Role/Capabilities.pm »
1 package Langertha::Moment;
2 # ABSTRACT: Instant on the wire - a Time::Moment that numifies to its Unix epoch
3 197 our $VERSION = '0.505';
4 197 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 197 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 197 ▶use parent 'Time::Moment';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 197 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # NOT a Moose class, and deliberately so — the one place in this distribution
10 # that departs from the "Moose exclusively / always make_immutable" house rule.
11 #
12 # Time::Moment is XS. Its instances are blessed SCALARs holding an opaque
13 # struct, and every constructor (from_string, from_epoch, now, ...) as well as
14 # every derivation (plus_days, with_offset_same_instant, ...) blesses into the
15 # invocant's class from inside XS, never through Moose::Object::new. A Moose
16 # metaclass laid over that would describe an object system nothing here uses:
17 # make_immutable would inline a constructor that no Time::Moment code path
18 # calls, and Moose's default hash-based meta-instance does not fit a blessed
19 # SCALAR at all.
20 #
21 # MooseX::NonMoose (already a dependency, used by Langertha::Request::HTTP)
22 # was considered and rejected for the same reason. It exists to give a Moose
23 # class Moose *attributes* on top of a foreign parent, by routing
24 # Moose::Object::new through FOREIGNBUILDARGS into the parent's new. This
25 # class adds no attributes, and the constructors that actually matter here
26 # (from_string / from_epoch / from_wire) bypass new entirely — so NonMoose
27 # would add a metaclass, a wrapped new nobody calls, and a hash-instance
28 # assumption that is false, in exchange for nothing.
29 #
30 # What this class *is* is the overload set: subclassing is the only way to
31 # attach one to Time::Moment without polluting every other consumer of it in
32 # the process.
33 use overload
34 # The back-compat contract. Langertha::Response->created was a Unix
35 # timestamp Int for its whole life; numeric context must keep yielding
36 # exactly that number. Note this is not inherited: Time::Moment overloads
37 # '""' but not '0+', so a plain Time::Moment numifies through its string
38 # form and evaluates to its *year*.
39 54 ▶ '0+' => sub { $_[0]->epoch },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
40 # Same as the inherited '""', declared explicitly: the pair of "'0+' is the
41 # epoch, '""' is the full ISO-8601 stamp" is the contract of this class, and
42 # it should not silently follow a change in the parent.
43 14 ▶ '""' => sub { $_[0]->to_string },
Subroutine __ANON__: called CC=1 SCAR=0.0
44 '<=>' => \&_compare_numeric,
45 'cmp' => \&_compare_string,
46 # An instant that exists is true, the same call Langertha::Response makes for
47 # itself (karr #100). Declared rather than left to autogeneration: perl
48 # derives bool from '0+' here, which makes the epoch-0 moment -- a perfectly
49 # real instant -- false, and which overload it picks is not something to
50 # depend on across perl versions.
51 6 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 197 fallback => 1;
53  
54  
55 4 ▶sub TO_JSON { return 0 + $_[0]->epoch }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 7 ▶sub _compare_numeric {
Subroutine _compare_numeric: called CC=4 SCAR=19.0
59 0 ▶ my ( $self, $other, $swapped ) = @_;
Subroutine _compare_numeric: not called CC=4 SCAR=19.0
60 0 ▶ my $result = ( blessed($other) && $other->isa('Time::Moment') )
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($other) && $other->isa('Time::Moment') ? : 3 4 7
builtin::blessed($other) && $other->isa('Time::Moment') ? : 0 0 0
Condition33%2/6
builtin::blessed($other) and $other->isa("Time::Moment")
!ll&&!rl&&r
403
builtin::blessed($other) and $other->isa("Time::Moment")
!ll&&!rl&&r
000
MC/DC50%1/2
builtin::blessed($other) and $other->isa("Time::Moment")
builtin::blessed($other) $other->isa("Time::Moment")
MC/DC0%0/2
builtin::blessed($other) and $other->isa("Time::Moment")
builtin::blessed($other) $other->isa("Time::Moment")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("Time::Moment")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($other)B= $other->isa("Time::Moment")
ABresult
0X0
100
111
61 ? $self->compare($other)
62 : ( $self->epoch <=> $other );
63 0 ▶ return $swapped ? -$result : $result;
Branch: 2 branches
BranchTrueFalseTotal
$swapped ? : 1 6 7
$swapped ? : 0 0 0
64 }
65  
66 2 ▶sub _compare_string {
Subroutine _compare_string: called CC=2 SCAR=10.5
67 0 ▶ my ( $self, $other, $swapped ) = @_;
Subroutine _compare_string: not called CC=2 SCAR=10.5
68 0 my $result = $self->to_string cmp "$other";
69 0 ▶ return $swapped ? -$result : $result;
Branch: 2 branches
BranchTrueFalseTotal
$swapped ? : 0 2 2
$swapped ? : 0 0 0
70 }
71  
72 345 ▶sub from_wire {
Subroutine from_wire: called CC=9 SCAR=30.0
Pod documented
73 0 ▶ my ( $class, $value ) = @_;
Subroutine from_wire: not called CC=9 SCAR=30.0
Pod documented
74 0 ▶ return undef unless defined $value;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $value 1 344 345
unless defined $value 0 0 0
75  
76 0 my $moment;
77 0 ▶ if ( blessed($value) ) {
Branch: 4 branches
BranchTrueFalseTotal
if (builtin::blessed($value)) { } 86 258 344
elsif ($value =~ /\A [-+]? [0-9]+ (?: \. [0-9]+ )? \z/x) { } 208 50 258
if (builtin::blessed($value)) { } 0 0 0
elsif ($value =~ /\A [-+]? [0-9]+ (?: \. [0-9]+ )? \z/x) { } 0 0 0
78 0 ▶ return undef unless $value->isa('Time::Moment');
Branch: 2 branches
BranchTrueFalseTotal
unless $value->isa("Time::Moment") 0 86 86
unless $value->isa("Time::Moment") 0 0 0
79 0 ▶ return $value if $value->isa($class);
Branch: 2 branches
BranchTrueFalseTotal
if $value->isa($class) 85 1 86
if $value->isa($class) 0 0 0
80 # A plain Time::Moment (or a foreign subclass): re-parse its own canonical
81 # string. That carries nanoseconds and offset across without assuming
82 # anything about how the other class was built.
83 0 $moment = eval { $class->from_string( $value->to_string ) };
84 }
85 elsif ( $value =~ /\A [-+]? [0-9]+ (?: \. [0-9]+ )? \z/x ) {
86 # The OpenAI-compatible wire sends an epoch integer, and Ollama-compatible
87 # shims sometimes send epoch seconds where Ollama itself sends RFC3339.
88 #
89 # Seconds and milliseconds are told apart by magnitude, not by any wire
90 # flag: Time::Moment's whole representable range (through year 9999) tops
91 # out at epoch 253402300799 -- 12 digits -- so a genuine seconds epoch is
92 # always below 1e12. A 13-digit value is out of range as seconds but is a
93 # sane milliseconds epoch (1e12 ms is year 2001), so scale it down instead
94 # of letting from_epoch reject it and silently drop the field. The division
95 # keeps the sub-second part. A normal ~10-digit seconds epoch is untouched
96 # (karr #171).
97 0 my $epoch = 0 + $value;
98 0 ▶ $epoch /= 1000 if abs($epoch) >= 1e12;
Branch: 2 branches
BranchTrueFalseTotal
if abs $epoch >= "1000000000000" 3 205 208
if abs $epoch >= "1000000000000" 0 0 0
99 0 $moment = eval { $class->from_epoch( $epoch ) };
100 }
101 else {
102 # lenient => 1 is chosen, not a default: it is what makes this accept the
103 # same set the hand-rolled RFC3339 regex it replaced accepted — a space
104 # instead of the T, an offset written without its colon, lowercase t/z.
105 # It does not widen the door to non-timestamps: bare '2026' and
106 # '2026-02-22' are still rejected, with or without it.
107 0 $moment = eval { $class->from_string( "$value", lenient => 1 ) };
108 }
109  
110 0 ▶ return undef unless defined $moment;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $moment 12 247 259
unless defined $moment 0 0 0
111 # Ollama emits "0001-01-01T00:00:00Z" — Go's zero time.Time — as its "no
112 # timestamp" sentinel, and it is a perfectly valid RFC3339 string that
113 # Time::Moment parses without complaint. Treat the whole pre-1000 band as
114 # absent rather than as a real instant; nothing an LLM provider reports
115 # belongs there (karr #92).
116 0 ▶ return undef if $moment->year < 1000;
Branch: 2 branches
BranchTrueFalseTotal
if $moment->year < 1000 4 243 247
if $moment->year < 1000 0 0 0
117 0 return $moment;
118 }
119  
120  
121  
122 197 1;
123  
124 __END__
« blib/lib/Langertha/Virtual/Server.pm ↑ Summary blib/lib/Langertha/Role/Capabilities.pm »