| 1 | package DateTime::Format::TauStation; | ||||||||||||||
| 2 | 6 | $DateTime::Format::TauStation::VERSION = '1.182290'; | |||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 6 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 6 | ▶ | use DateTime::Calendar::TauStation; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use DateTime::Duration::TauStation; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use parent 'DateTime::Format::Builder'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | =head1 NAME | ||||||||||||||
| 11 | |||||||||||||||
| 12 | DateTime::Format::TauStation - Parse and format TauStation GCT datetimes | ||||||||||||||
| 13 | |||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||
| 15 | |||||||||||||||
| 16 | use DateTime::Format::TauStation; | ||||||||||||||
| 17 | |||||||||||||||
| 18 | my $dt = DateTime::Format::TauStation->parse_datetime( '90.28/44:001 GCT' ); | ||||||||||||||
| 19 | |||||||||||||||
| 20 | # 90.28/44:001 GCT | ||||||||||||||
| 21 | DateTime::Format::TauStation->format_datetime($dt); | ||||||||||||||
| 22 | |||||||||||||||
| 23 | my $dur = DateTime::Format::TauStation->parse_duration( 'D/20:000 GCT' ); | ||||||||||||||
| 24 | |||||||||||||||
| 25 | # D/20:000 GCT | ||||||||||||||
| 26 | DateTime::Format::TauStation->format_duration($dur); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | |||||||||||||||
| 29 | =head1 DESCRIPTION | ||||||||||||||
| 30 | |||||||||||||||
| 31 | Parse and format GCT (Galactic Coordinated Time) strings for the online game | ||||||||||||||
| 32 | L<TauStation|https://taustation.space>. | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =cut | ||||||||||||||
| 35 | |||||||||||||||
| 36 | 6 | my @gct_fields = qw( gct_cycle gct_day gct_segment gct_unit ); | |||||||||||||
| 37 | |||||||||||||||
| 38 | 6 | my $sign = qr! ( [-]? ) !x; | |||||||||||||
| 39 | 6 | my $cycle = qr! ( [0-9]+ ) !x; | |||||||||||||
| 40 | 6 | my $day = qr! ( [0-9]{1,2} ) !x; | |||||||||||||
| 41 | 6 | my $segment = qr! ( [0-9]{1,2} ) !x; | |||||||||||||
| 42 | 6 | my $unit = qr! ( [0-9]{1,3} ) !x; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 6 | my $date = qr! $cycle \. $day !x; | |||||||||||||
| 45 | 6 | my $time = qr! $segment : $unit !x; | |||||||||||||
| 46 | |||||||||||||||
| 47 | 6 | my $gct_datetime = qr! ^ $sign $date / $time [ ] GCT $ !x; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 6 | my $gct_duration_cdsu = qr! D $sign $date / $time [ ] GCT $ !x; | |||||||||||||
| 50 | 6 | my $gct_duration_dsu = qr! D $sign $day / $time [ ] GCT $ !x; | |||||||||||||
| 51 | 6 | my $gct_duration_su = qr! D $sign / $time [ ] GCT $ !x; | |||||||||||||
| 52 | |||||||||||||||
| 53 | my $parse_datetime = { | ||||||||||||||
| 54 | regex => $gct_datetime, | ||||||||||||||
| 55 | params => ['gct_sign', @gct_fields], | ||||||||||||||
| 56 | 11 | ▶ | constructor => sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 57 | 11 | my ( $parser, %args ) = @_; | |||||||||||||
| 58 | 11 | my %gct = map { $_ => $args{$_} } 'gct_sign', @gct_fields; | |||||||||||||
| 59 | |||||||||||||||
| 60 | 11 | return DateTime::Calendar::TauStation->new(%gct); | |||||||||||||
| 61 | }, | ||||||||||||||
| 62 | 6 | }; | |||||||||||||
| 63 | |||||||||||||||
| 64 | 7 | ▶ | my $duration_constructor = sub { | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | 7 | my ( $parser, %args ) = @_; | |||||||||||||
| 66 | 28 | my %gct = map { $_ => $args{$_} } | |||||||||||||
| 67 | 7 | map { "${_}s" } | |||||||||||||
| 68 | @gct_fields; | ||||||||||||||
| 69 | |||||||||||||||
| 70 | # this field isn't pluralized | ||||||||||||||
| 71 | 7 | $gct{gct_sign} = $args{gct_sign}; | |||||||||||||
| 72 | |||||||||||||||
| 73 | 7 | return DateTime::Duration::TauStation->new(%gct); | |||||||||||||
| 74 | 6 | }; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 6 | my @parse_duration = ( | |||||||||||||
| 77 | { | ||||||||||||||
| 78 | regex => $gct_duration_cdsu, | ||||||||||||||
| 79 | params => [qw( gct_sign gct_cycles gct_days gct_segments gct_units )], | ||||||||||||||
| 80 | constructor => $duration_constructor, | ||||||||||||||
| 81 | }, | ||||||||||||||
| 82 | { | ||||||||||||||
| 83 | regex => $gct_duration_dsu, | ||||||||||||||
| 84 | params => [qw( gct_sign gct_days gct_segments gct_units )], | ||||||||||||||
| 85 | constructor => $duration_constructor, | ||||||||||||||
| 86 | }, | ||||||||||||||
| 87 | { | ||||||||||||||
| 88 | regex => $gct_duration_su, | ||||||||||||||
| 89 | params => [qw( gct_sign gct_segments gct_units )], | ||||||||||||||
| 90 | constructor => $duration_constructor, | ||||||||||||||
| 91 | }, | ||||||||||||||
| 92 | ); | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 6 | DateTime::Format::Builder->create_class | |||||||||||||
| 95 | ( | ||||||||||||||
| 96 | parsers => { | ||||||||||||||
| 97 | parse_datetime => [$parse_datetime], | ||||||||||||||
| 98 | parse_duration => \@parse_duration, | ||||||||||||||
| 99 | }, | ||||||||||||||
| 100 | ); | ||||||||||||||
| 101 | |||||||||||||||
| 102 | =head1 METHODS | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =head2 parse_datetime | ||||||||||||||
| 105 | |||||||||||||||
| 106 | Requires a full datetime string such as C<000.01/02:003>, where C<000> is the | ||||||||||||||
| 107 | cycle, C<01> is the day, C<02> is the segment, and C<003> is the unit. | ||||||||||||||
| 108 | |||||||||||||||
| 109 | Returns a L<DateTime::Calendar::TauStation> object. | ||||||||||||||
| 110 | |||||||||||||||
| 111 | =head2 parse_duration | ||||||||||||||
| 112 | |||||||||||||||
| 113 | Supports the following forms of duration strings: C<D4.3/02:001 GCT>, | ||||||||||||||
| 114 | C<D3/02:001 GCT> and C<D/02:001 GCT>. | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Returns a L<DateTime::Calendar::TauStation> object. | ||||||||||||||
| 117 | |||||||||||||||
| 118 | =head2 format_datetime | ||||||||||||||
| 119 | |||||||||||||||
| 120 | =cut | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 2 | ▶ | sub format_datetime { | ||||||||||||
|
Subroutine
format_datetime: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 123 | 2 | my ( $self, $dt ) = @_; | |||||||||||||
| 124 | |||||||||||||||
| 125 | 2 | my ( $sign, $cycles, $days, $segments, $units ) = @{ $dt->_return_gct }; | |||||||||||||
| 126 | |||||||||||||||
| 127 | 2 | ▶ | $sign = "" unless "-" eq $sign; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | |||||||||||||||
| 129 | 2 | return sprintf "%s%.3d.%.2d/%.2d:%.3d GCT", | |||||||||||||
| 130 | $sign, | ||||||||||||||
| 131 | $cycles, | ||||||||||||||
| 132 | $days, | ||||||||||||||
| 133 | $segments, | ||||||||||||||
| 134 | $units; | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | |||||||||||||||
| 137 | =head2 format_duration | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =head2 format_interval | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Given a C<DateTime::Duration::TauStation> object, this method returns a GCT | ||||||||||||||
| 142 | formatted duration string. | ||||||||||||||
| 143 | |||||||||||||||
| 144 | =cut | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 4 | ▶ | sub format_duration { | ||||||||||||
|
Subroutine
format_duration: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||
| 147 | 4 | my ( $self, $dur ) = @_; | |||||||||||||
| 148 | |||||||||||||||
| 149 | 4 | my $seconds = $dur->delta_seconds; | |||||||||||||
| 150 | |||||||||||||||
| 151 | my ( $sign, $cycles, $days, $segments, $units ) = | ||||||||||||||
| 152 | 4 | @{ DateTime::Calendar::TauStation->new->_return_gct( undef, $seconds ) }; | |||||||||||||
| 153 | |||||||||||||||
| 154 | 4 | ▶ | $sign = "" unless "-" eq $sign; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 155 | |||||||||||||||
| 156 | 4 | ▶ | if ( $cycles ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 157 | 2 | return sprintf "D%s%d.%d/%.2d:%.3d GCT", | |||||||||||||
| 158 | $sign, | ||||||||||||||
| 159 | $cycles, | ||||||||||||||
| 160 | $days, | ||||||||||||||
| 161 | $segments, | ||||||||||||||
| 162 | $units; | ||||||||||||||
| 163 | } | ||||||||||||||
| 164 | elsif ( $days ) { | ||||||||||||||
| 165 | 1 | return sprintf "D%s%d/%.2d:%.3d GCT", | |||||||||||||
| 166 | $sign, | ||||||||||||||
| 167 | $days, | ||||||||||||||
| 168 | $segments, | ||||||||||||||
| 169 | $units; | ||||||||||||||
| 170 | } | ||||||||||||||
| 171 | else { | ||||||||||||||
| 172 | 1 | return sprintf "D%s/%.2d:%.3d GCT", | |||||||||||||
| 173 | $sign, | ||||||||||||||
| 174 | $segments, | ||||||||||||||
| 175 | $units; | ||||||||||||||
| 176 | } | ||||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 6 | *format_interval = \&format_duration; | |||||||||||||
| 180 | |||||||||||||||
| 181 | 6 | 1; | |||||||||||||
| 182 | |||||||||||||||
| 183 | __END__ | ||||||||||||||