File Coverage

blib/lib/STIX/Common/Timestamp.pm
Criterion Covered Total %
statement 30 32 93.7
branch 7 10 70.0
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 47 52 90.3


line stmt bran cond sub pod time code
1             package STIX::Common::Timestamp;
2              
3 26     26   1074 use 5.010001;
  26         90  
4 26     26   164 use strict;
  26         76  
  26         872  
5 26     26   301 use warnings;
  26         82  
  26         1371  
6 26     26   207 use utf8;
  26         91  
  26         146  
7              
8 26     26   908 use Carp;
  26         64  
  26         1722  
9 26     26   15778 use Time::Piece;
  26         376128  
  26         149  
10              
11 26     26   2751 use Moo;
  26         66  
  26         331  
12              
13             around BUILDARGS => sub {
14              
15             my ($orig, $class, @args) = @_;
16              
17             return {value => $args[0]} if @args == 1;
18             return $class->$orig(@args);
19              
20             };
21              
22             has value => (is => 'rw', default => sub { Time::Piece->new }, coerce => sub { _parse($_[0]) });
23              
24             my @PATTERNS = (
25             ['%Y-%m-%dT%H:%M:%S', qr/(\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2})\.\d+Z/],
26             ['%Y-%m-%dT%H:%M:%S', qr/(\d{4}-\d{2}-\d{2}[T]\d{2}:\d{2}:\d{2})/],
27             ['%Y-%m-%d %H:%M:%S', qr/(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})/],
28             ['%Y-%m-%d', qr/(\d{4}-\d{2}-\d{2})/],
29             );
30              
31             sub _parse {
32              
33 808     808   1675 my $datetime = shift;
34              
35 808 100       6024 return $datetime if (ref $datetime eq 'Time::Piece');
36              
37             #return $datetime->value if ($datetime->isa('STIX::Common::Timestamp')); # TODO
38              
39 687 50       1656 return Time::Piece->new unless $datetime;
40              
41 687 50       2935 return Time::Piece->new($datetime) if ($datetime =~ /^([0-9]+)$/);
42 687 50       1702 return Time::Piece->new if ($datetime eq 'now');
43              
44 687         1586 foreach my $pattern (@PATTERNS) {
45 734         1117 my ($format, $regexp) = @{$pattern};
  734         1863  
46 734 100       8696 return Time::Piece->strptime($1, $format) if ($datetime =~ /$regexp/);
47             }
48              
49 0         0 Carp::carp 'Malformed timestamp';
50              
51 0         0 return Time::Piece->new;
52              
53             }
54              
55 210285     210285 1 175117132 sub TO_JSON { shift->value->datetime . '.000Z' }
56              
57             1;
58              
59             =encoding utf-8
60              
61             =head1 NAME
62              
63             STIX::Common::Timestamp - Timestamp type
64              
65             =head1 SYNOPSIS
66              
67             use STIX::Common::Timestamp;
68              
69             my $date = STIX::Common::Timestamp->new(value => '2009-10-14T01:30:00');
70              
71             say $date; # 2009-10-14T01:30:00.000Z
72              
73              
74             =head1 DESCRIPTION
75              
76             The timestamp type defines how dates and times are represented in STIX.
77              
78             =head2 PROPERTIES
79              
80             =over
81              
82             =item value
83              
84             =back
85              
86             =head2 HELPERS
87              
88             =over
89              
90             =item $timestamp->TO_JSON
91              
92             Encode the object in JSON.
93              
94             =item $timestamp->to_string
95              
96             Encode the object in JSON.
97              
98             =back
99              
100              
101             =head1 SUPPORT
102              
103             =head2 Bugs / Feature Requests
104              
105             Please report any bugs or feature requests through the issue tracker
106             at L.
107             You will be notified automatically of any progress on your issue.
108              
109             =head2 Source Code
110              
111             This is open source software. The code repository is available for
112             public review and contribution under the terms of the license.
113              
114             L
115              
116             git clone https://github.com/giterlizzi/perl-STIX.git
117              
118              
119             =head1 AUTHOR
120              
121             =over 4
122              
123             =item * Giuseppe Di Terlizzi
124              
125             =back
126              
127              
128             =head1 LICENSE AND COPYRIGHT
129              
130             This software is copyright (c) 2024 by Giuseppe Di Terlizzi.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =cut