| 1 | package SBOM::CycloneDX::Timestamp; | ||||||||||
| 2 | |||||||||||
| 3 | 16 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 16 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 16 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 16 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 16 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 16 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 16 | ▶ | use overload '""' => \&to_string, fallback => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 16 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 0 | ▶ | around BUILDARGS => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 16 | |||||||||||
| 17 | 0 | my ($orig, $class, @args) = @_; | |||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | return {value => $args[0]} if @args == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 20 | 0 | return $class->$orig(@args); | |||||||||
| 21 | |||||||||||
| 22 | 16 | }; | |||||||||
| 23 | |||||||||||
| 24 | 0 | ▶ | has value => (is => 'rw', default => sub { Time::Piece->gmtime }, coerce => sub { _parse($_[0]) }); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||
| 25 | |||||||||||
| 26 | 16 | my @PATTERNS = ( | |||||||||
| 27 | ['%Y-%m-%dT%H:%M:%S', qr/(\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2})\.\d+Z/], | ||||||||||
| 28 | ['%Y-%m-%dT%H:%M:%S', qr/(\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2})/], | ||||||||||
| 29 | ['%Y-%m-%d %H:%M:%S', qr/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/], | ||||||||||
| 30 | ['%Y-%m-%d', qr/(\d{4}-\d{2}-\d{2})/], | ||||||||||
| 31 | ); | ||||||||||
| 32 | |||||||||||
| 33 | 0 | ▶ | sub _parse { | ||||||||
|
Subroutine
_parse: not called CC=8 SCAR=42.8
| |||||||||||
| 34 | |||||||||||
| 35 | 0 | my $datetime = shift; | |||||||||
| 36 | |||||||||||
| 37 | 0 | ▶ | return $datetime if (ref $datetime eq 'Time::Piece'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | |||||||||||
| 39 | 0 | ▶ | return $datetime->value if ($datetime->isa('SBOM::CycloneDX::Timestamp')); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | |||||||||||
| 41 | 0 | ▶ | return Time::Piece->gmtime unless $datetime; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | |||||||||||
| 43 | 0 | ▶ | return Time::Piece->gmtime($datetime) if ($datetime =~ /^([0-9]+)$/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 0 | ▶ | return Time::Piece->gmtime if ($datetime eq 'now'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | |||||||||||
| 46 | 0 | foreach my $pattern (@PATTERNS) { | |||||||||
| 47 | 0 | my ($format, $regexp) = @{$pattern}; | |||||||||
| 48 | 0 | ▶ | return Time::Piece->strptime($1, $format) if ($datetime =~ /$regexp/); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | 0 | Carp::carp 'Malformed timestamp'; | |||||||||
| 52 | |||||||||||
| 53 | 0 | return Time::Piece->gmtime; | |||||||||
| 54 | |||||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 0 | ▶ | sub to_string { shift->TO_JSON } | ||||||||
|
Subroutine
to_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 58 | |||||||||||
| 59 | 0 | ▶ | sub TO_JSON { shift->value->datetime . 'Z' } | ||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 60 | |||||||||||
| 61 | 16 | 1; | |||||||||
| 62 | |||||||||||
| 63 | =encoding utf-8 | ||||||||||
| 64 | |||||||||||
| 65 | =head1 NAME | ||||||||||
| 66 | |||||||||||
| 67 | SBOM::CycloneDX::Timestamp - Timestamp representation for CycloneDX | ||||||||||
| 68 | |||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||
| 70 | |||||||||||
| 71 | SBOM::CycloneDX::Timestamp->new('2025-01-01'); | ||||||||||
| 72 | |||||||||||
| 73 | SBOM::CycloneDX::Timestamp->new('2025-01-01 00:00:00'); | ||||||||||
| 74 | |||||||||||
| 75 | SBOM::CycloneDX::Timestamp->new('2025-01-01T00:00:00'); | ||||||||||
| 76 | |||||||||||
| 77 | SBOM::CycloneDX::Timestamp->new(Time::Piece->gmtime); | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | =head1 DESCRIPTION | ||||||||||
| 81 | |||||||||||
| 82 | L<SBOM::CycloneDX::Timestamp> provides represents the timestamp for L<SBOM::CycloneDX>. | ||||||||||
| 83 | |||||||||||
| 84 | =head2 METHODS | ||||||||||
| 85 | |||||||||||
| 86 | =over | ||||||||||
| 87 | |||||||||||
| 88 | =item SBOM::CycloneDX::Timestamp->new( %PARAMS ) | ||||||||||
| 89 | |||||||||||
| 90 | =item $ts->value | ||||||||||
| 91 | |||||||||||
| 92 | Return L<Time::Piece> object. | ||||||||||
| 93 | |||||||||||
| 94 | =item $ts->to_string | ||||||||||
| 95 | |||||||||||
| 96 | =item $ts->TO_JSON | ||||||||||
| 97 | |||||||||||
| 98 | Return timestamp in JSON format. | ||||||||||
| 99 | |||||||||||
| 100 | =back | ||||||||||
| 101 | |||||||||||
| 102 | =head1 SUPPORT | ||||||||||
| 103 | |||||||||||
| 104 | =head2 Bugs / Feature Requests | ||||||||||
| 105 | |||||||||||
| 106 | Please report any bugs or feature requests through the issue tracker | ||||||||||
| 107 | at L<https://github.com/giterlizzi/perl-SBOM-CycloneDX/issues>. | ||||||||||
| 108 | You will be notified automatically of any progress on your issue. | ||||||||||
| 109 | |||||||||||
| 110 | =head2 Source Code | ||||||||||
| 111 | |||||||||||
| 112 | This is open source software. The code repository is available for | ||||||||||
| 113 | public review and contribution under the terms of the license. | ||||||||||
| 114 | |||||||||||
| 115 | L<https://github.com/giterlizzi/perl-SBOM-CycloneDX> | ||||||||||
| 116 | |||||||||||
| 117 | git clone https://github.com/giterlizzi/perl-SBOM-CycloneDX.git | ||||||||||
| 118 | |||||||||||
| 119 | |||||||||||
| 120 | =head1 AUTHOR | ||||||||||
| 121 | |||||||||||
| 122 | =over 4 | ||||||||||
| 123 | |||||||||||
| 124 | =item * Giuseppe Di Terlizzi <gdt@cpan.org> | ||||||||||
| 125 | |||||||||||
| 126 | =back | ||||||||||
| 127 | |||||||||||
| 128 | |||||||||||
| 129 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 130 | |||||||||||
| 131 | This software is copyright (c) 2025-2026 by Giuseppe Di Terlizzi. | ||||||||||
| 132 | |||||||||||
| 133 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 134 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 135 | |||||||||||
| 136 | =cut | ||||||||||