| 1 | package DateTime::Calendar::TauStation; | ||||||||||||||
| 2 | 6 | $DateTime::Calendar::TauStation::VERSION = '1.182290'; | |||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 6 | ▶ | use vars qw ( $SECS_2_UNITS @EXPORT_OK ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | |||||||||||||||
| 6 | 6 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 6 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 6 | ▶ | use POSIX 'floor'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 6 | ▶ | use parent 'DateTime'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 6 | require DateTime::Duration; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 6 | $SECS_2_UNITS = 0.864; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 6 | @EXPORT_OK = qw( gct2seconds ); | |||||||||||||
| 16 | |||||||||||||||
| 17 | =head1 NAME | ||||||||||||||
| 18 | |||||||||||||||
| 19 | DateTime::Calendar::TauStation - Handle TauStation GCT datetimes | ||||||||||||||
| 20 | |||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||
| 22 | |||||||||||||||
| 23 | use DateTime::Calendar::TauStation; | ||||||||||||||
| 24 | |||||||||||||||
| 25 | my $dt = DateTime::Calendar::TauStation->new( | ||||||||||||||
| 26 | cycle => '001', | ||||||||||||||
| 27 | day => '02', | ||||||||||||||
| 28 | segment => '03', | ||||||||||||||
| 29 | unit => '004', | ||||||||||||||
| 30 | ); | ||||||||||||||
| 31 | |||||||||||||||
| 32 | print $dt->gct_cycle; | ||||||||||||||
| 33 | print $dt->gct_day; | ||||||||||||||
| 34 | print $dt->gct_segment; | ||||||||||||||
| 35 | print $dt->gct_unit; | ||||||||||||||
| 36 | |||||||||||||||
| 37 | Alternatively, combine with L<DateTime::Duration::TauStation> and | ||||||||||||||
| 38 | L<DateTime::Format::TauStation>. | ||||||||||||||
| 39 | |||||||||||||||
| 40 | use DateTime::Calendar::TauStation; | ||||||||||||||
| 41 | use DateTime::Format::TauStation; | ||||||||||||||
| 42 | |||||||||||||||
| 43 | my $dur = DateTime::Format::TauStation->parse_duration( 'D/20:000 GCT' ); | ||||||||||||||
| 44 | |||||||||||||||
| 45 | my $dt = DateTime::Calendar::TauStation->now->add_duration( $dur ); | ||||||||||||||
| 46 | |||||||||||||||
| 47 | print DateTime::Format::TauStation->format_datetime($dt); | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =head1 DESCRIPTION | ||||||||||||||
| 50 | |||||||||||||||
| 51 | L<DateTime> subclass for GCT (Galactic Coordinated Time) datetimes for the | ||||||||||||||
| 52 | online game L<TauStation|https://taustation.space>. | ||||||||||||||
| 53 | |||||||||||||||
| 54 | =head1 METHODS | ||||||||||||||
| 55 | |||||||||||||||
| 56 | =head2 new | ||||||||||||||
| 57 | |||||||||||||||
| 58 | Accepts arguments: | ||||||||||||||
| 59 | |||||||||||||||
| 60 | =over | ||||||||||||||
| 61 | |||||||||||||||
| 62 | =item gct_cycle | ||||||||||||||
| 63 | |||||||||||||||
| 64 | =item gct_day | ||||||||||||||
| 65 | |||||||||||||||
| 66 | =item gct_segment | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =item gct_unit | ||||||||||||||
| 69 | |||||||||||||||
| 70 | =item gct_sign | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =back | ||||||||||||||
| 73 | |||||||||||||||
| 74 | =cut | ||||||||||||||
| 75 | |||||||||||||||
| 76 | 6 | my @gct_fields = qw( gct_sign gct_cycle gct_day gct_segment gct_unit ); | |||||||||||||
| 77 | |||||||||||||||
| 78 | 89 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 79 | 89 | my ( $class, %args ) = @_; | |||||||||||||
| 80 | 89 | my $self; | |||||||||||||
| 81 | |||||||||||||||
| 82 | 89 | ▶ | if ( grep { exists $args{$_} } @gct_fields ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 83 | 11 | $self = $class->catastrophe; | |||||||||||||
| 84 | |||||||||||||||
| 85 | 11 | my $seconds = gct2seconds(%args); | |||||||||||||
| 86 | |||||||||||||||
| 87 | 11 | my $is_negative; | |||||||||||||
| 88 | 11 | ▶ | $is_negative = 1 if $seconds < 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | 11 | $seconds = abs $seconds; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 11 | ▶ | if ( $is_negative ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 1 | $self->subtract( seconds => $seconds ); | |||||||||||||
| 94 | } | ||||||||||||||
| 95 | else { | ||||||||||||||
| 96 | 10 | $self->add( seconds => $seconds ); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | } | ||||||||||||||
| 99 | elsif ( %args ) { | ||||||||||||||
| 100 | 74 | $self = $class->SUPER::new( | |||||||||||||
| 101 | time_zone => 'UTC', | ||||||||||||||
| 102 | %args | ||||||||||||||
| 103 | ); | ||||||||||||||
| 104 | } | ||||||||||||||
| 105 | else { | ||||||||||||||
| 106 | 4 | $self = $class->catastrophe; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 89 | $self->duration_class('DateTime::Duration::TauStation'); | |||||||||||||
| 110 | |||||||||||||||
| 111 | 89 | return $self; | |||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | =head2 catastrophe | ||||||||||||||
| 115 | |||||||||||||||
| 116 | Returns the datetime C<000.00/00:000 GCT>, a.k.a. | ||||||||||||||
| 117 | C<1964-01-22T00:00:27.689615> UTC. | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =cut | ||||||||||||||
| 120 | |||||||||||||||
| 121 | 77 | ▶ | sub catastrophe { | ||||||||||||
|
Subroutine
catastrophe: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 122 | 77 | my ( $self ) = @_; | |||||||||||||
| 123 | |||||||||||||||
| 124 | 77 | my @args = ( | |||||||||||||
| 125 | year => 1964, | ||||||||||||||
| 126 | month => 1, | ||||||||||||||
| 127 | day => 22, | ||||||||||||||
| 128 | hour => 0, | ||||||||||||||
| 129 | minute => 0, | ||||||||||||||
| 130 | second => 27, | ||||||||||||||
| 131 | nanosecond => 689615, | ||||||||||||||
| 132 | time_zone => 'UTC', | ||||||||||||||
| 133 | ); | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 77 | ▶ | return ref $self ? ref($self)->new(@args) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 136 | : $self->SUPER::new(@args); | ||||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | =head2 gct_sign | ||||||||||||||
| 140 | |||||||||||||||
| 141 | Pre-catastrophe dates return a C<-> sign. | ||||||||||||||
| 142 | |||||||||||||||
| 143 | =cut | ||||||||||||||
| 144 | |||||||||||||||
| 145 | 11 | ▶ | sub gct_sign { | ||||||||||||
|
Subroutine
gct_sign: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 146 | 11 | my ( $self ) = @_; | |||||||||||||
| 147 | |||||||||||||||
| 148 | 11 | return $self->_return_gct('sign'); | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | =head2 gct_cycle | ||||||||||||||
| 152 | |||||||||||||||
| 153 | Returns the C<cycle> part of the datetime. | ||||||||||||||
| 154 | |||||||||||||||
| 155 | =cut | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 11 | ▶ | sub gct_cycle { | ||||||||||||
|
Subroutine
gct_cycle: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 158 | 11 | my ( $self ) = @_; | |||||||||||||
| 159 | |||||||||||||||
| 160 | 11 | return $self->_return_gct('cycle'); | |||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | =head2 gct_day | ||||||||||||||
| 164 | |||||||||||||||
| 165 | Returns the C<day> part of the datetime. | ||||||||||||||
| 166 | |||||||||||||||
| 167 | =cut | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 11 | ▶ | sub gct_day { | ||||||||||||
|
Subroutine
gct_day: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 170 | 11 | my ( $self ) = @_; | |||||||||||||
| 171 | |||||||||||||||
| 172 | 11 | return $self->_return_gct('day'); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | =head2 gct_segment | ||||||||||||||
| 176 | |||||||||||||||
| 177 | Returns the C<segment> part of the datetime. | ||||||||||||||
| 178 | |||||||||||||||
| 179 | =cut | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 11 | ▶ | sub gct_segment { | ||||||||||||
|
Subroutine
gct_segment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 182 | 11 | my ( $self ) = @_; | |||||||||||||
| 183 | |||||||||||||||
| 184 | 11 | return $self->_return_gct('segment'); | |||||||||||||
| 185 | } | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head2 gct_unit | ||||||||||||||
| 188 | |||||||||||||||
| 189 | Returns the C<unit> part of the datetime. | ||||||||||||||
| 190 | |||||||||||||||
| 191 | =cut | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | ▶ | sub gct_unit { | ||||||||||||
|
Subroutine
gct_unit: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 194 | 0 | my ( $self ) = @_; | |||||||||||||
| 195 | |||||||||||||||
| 196 | 0 | return $self->_return_gct('unit'); | |||||||||||||
| 197 | } | ||||||||||||||
| 198 | |||||||||||||||
| 199 | =head1 EXPORTED SUBROUTINES | ||||||||||||||
| 200 | |||||||||||||||
| 201 | =head2 gct2seconds | ||||||||||||||
| 202 | |||||||||||||||
| 203 | Accepts the same arguments as L</new>, returns a signed integer. | ||||||||||||||
| 204 | |||||||||||||||
| 205 | =cut | ||||||||||||||
| 206 | |||||||||||||||
| 207 | 19 | ▶ | sub gct2seconds { | ||||||||||||
|
Subroutine
gct2seconds: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 208 | 19 | my ( %args ) = @_; | |||||||||||||
| 209 | |||||||||||||||
| 210 | 19 | ▶ | my $sign = $args{gct_sign} || ""; | ||||||||||||
|
Condition100%2/2
$args{'gct_sign'} || ''
MC/DC100%1/1
$args{'gct_sign'} || ''
$args{'gct_sign'}
Truth table100%2/2
A
A= $args{'gct_sign'}
| |||||||||||||||
| 211 | 19 | ▶ | my $cycle = $args{gct_cycle} || 0; | ||||||||||||
|
Condition100%2/2
$args{'gct_cycle'} || 0
MC/DC100%1/1
$args{'gct_cycle'} || 0
$args{'gct_cycle'}
Truth table100%2/2
A
A= $args{'gct_cycle'}
| |||||||||||||||
| 212 | 19 | ▶ | my $day = $args{gct_day} || 0; | ||||||||||||
|
Condition100%2/2
$args{'gct_day'} || 0
MC/DC100%1/1
$args{'gct_day'} || 0
$args{'gct_day'}
Truth table100%2/2
A
A= $args{'gct_day'}
| |||||||||||||||
| 213 | 19 | ▶ | my $segment = $args{gct_segment} || 0; | ||||||||||||
|
Condition100%2/2
$args{'gct_segment'} || 0
MC/DC100%1/1
$args{'gct_segment'} || 0
$args{'gct_segment'}
Truth table100%2/2
A
A= $args{'gct_segment'}
| |||||||||||||||
| 214 | 19 | ▶ | my $unit = $args{gct_unit} || 0; | ||||||||||||
|
Condition50%1/2
$args{'gct_unit'} || 0
MC/DC0%0/1
$args{'gct_unit'} || 0
$args{'gct_unit'}
Truth table50%1/2
A
A= $args{'gct_unit'}
| |||||||||||||||
| 215 | |||||||||||||||
| 216 | 19 | my $is_negative; | |||||||||||||
| 217 | 19 | ▶ | $is_negative = 1 if "-" eq $sign; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | |||||||||||||||
| 219 | 19 | my $total_units = $unit; | |||||||||||||
| 220 | 19 | $total_units += $segment * 1_000; | |||||||||||||
| 221 | 19 | $total_units += $day * 100 * 1_000; | |||||||||||||
| 222 | 19 | $total_units += $cycle * 100 * 100 * 1_000; | |||||||||||||
| 223 | |||||||||||||||
| 224 | 19 | my $add_secs = $total_units * $SECS_2_UNITS; | |||||||||||||
| 225 | |||||||||||||||
| 226 | # round | ||||||||||||||
| 227 | 19 | $add_secs = int( $add_secs + 0.5 ); | |||||||||||||
| 228 | |||||||||||||||
| 229 | 19 | ▶ | if ( $is_negative ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 230 | 2 | $add_secs = 0-$add_secs; | |||||||||||||
| 231 | } | ||||||||||||||
| 232 | |||||||||||||||
| 233 | 19 | return $add_secs; | |||||||||||||
| 234 | } | ||||||||||||||
| 235 | |||||||||||||||
| 236 | 50 | ▶ | sub _return_gct { | ||||||||||||
|
Subroutine
_return_gct: called CC=9 SCAR=22.0
| |||||||||||||||
| 237 | 50 | my ( $self, $type, $seconds ) = @_; | |||||||||||||
| 238 | |||||||||||||||
| 239 | 50 | my $sign = ""; | |||||||||||||
| 240 | |||||||||||||||
| 241 | 50 | ▶ | if ( defined $seconds ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 242 | 4 | ▶ | $sign = "-" if $seconds < 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 243 | 4 | $seconds = abs $seconds; | |||||||||||||
| 244 | } | ||||||||||||||
| 245 | else { | ||||||||||||||
| 246 | 46 | my $dt1 = $self->catastrophe; | |||||||||||||
| 247 | 46 | $dt1->set_time_zone('UTC'); | |||||||||||||
| 248 | |||||||||||||||
| 249 | 46 | my $self_tz = $self->time_zone; | |||||||||||||
| 250 | 46 | $self->set_time_zone('UTC'); | |||||||||||||
| 251 | |||||||||||||||
| 252 | 46 | my $dur = $self->subtract_datetime_absolute( $dt1 ); | |||||||||||||
| 253 | 46 | $seconds = $dur->seconds; | |||||||||||||
| 254 | |||||||||||||||
| 255 | 46 | ▶ | $sign = "-" if $dur->is_negative; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 256 | |||||||||||||||
| 257 | 46 | $self->set_time_zone($self_tz); | |||||||||||||
| 258 | } | ||||||||||||||
| 259 | |||||||||||||||
| 260 | 50 | ▶ | return $sign if 'sign' eq $type; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 261 | |||||||||||||||
| 262 | 39 | my $delta_units = $seconds / $SECS_2_UNITS; | |||||||||||||
| 263 | |||||||||||||||
| 264 | 39 | my $cycles = floor( $delta_units / 100 / 100 / 1_000 ); | |||||||||||||
| 265 | |||||||||||||||
| 266 | 39 | ▶ | return $cycles if 'cycle' eq $type; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 267 | |||||||||||||||
| 268 | 28 | $delta_units -= ( $cycles * 100 * 100 * 1_000 ); | |||||||||||||
| 269 | |||||||||||||||
| 270 | 28 | my $days = floor( $delta_units / 100 / 1_000 ); | |||||||||||||
| 271 | |||||||||||||||
| 272 | 28 | ▶ | return $days if 'day' eq $type; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 273 | |||||||||||||||
| 274 | 17 | $delta_units -= ( $days * 100 * 1_000 ); | |||||||||||||
| 275 | |||||||||||||||
| 276 | 17 | my $segments = floor( $delta_units / 1_000 ); | |||||||||||||
| 277 | |||||||||||||||
| 278 | 17 | ▶ | return $segments if 'segment' eq $type; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 279 | |||||||||||||||
| 280 | 6 | $delta_units -= ( $segments * 1_000 ); | |||||||||||||
| 281 | |||||||||||||||
| 282 | 6 | my $units = floor( $delta_units ); | |||||||||||||
| 283 | |||||||||||||||
| 284 | 6 | ▶ | return $units if 'unit' eq $type; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 285 | |||||||||||||||
| 286 | 6 | return [ $sign, $cycles, $days, $segments, $units ]; | |||||||||||||
| 287 | } | ||||||||||||||
| 288 | |||||||||||||||
| 289 | =head2 subtract_datetime | ||||||||||||||
| 290 | |||||||||||||||
| 291 | Returns a L<DateTime::Duration::TauStation> object. | ||||||||||||||
| 292 | |||||||||||||||
| 293 | =cut | ||||||||||||||
| 294 | |||||||||||||||
| 295 | 1 | ▶ | sub subtract_datetime { | ||||||||||||
|
Subroutine
subtract_datetime: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 296 | 1 | my ( $dt1, $dt2 ) = @_; | |||||||||||||
| 297 | |||||||||||||||
| 298 | 1 | my $dt1_tz = $dt1->time_zone; | |||||||||||||
| 299 | 1 | my $dt2_tz = $dt2->time_zone; | |||||||||||||
| 300 | |||||||||||||||
| 301 | 1 | $dt1->set_time_zone('UTC'); | |||||||||||||
| 302 | 1 | $dt2->set_time_zone('UTC'); | |||||||||||||
| 303 | |||||||||||||||
| 304 | 1 | my $dur = $dt1->subtract_datetime_absolute($dt2); | |||||||||||||
| 305 | 1 | my $seconds = $dur->seconds; | |||||||||||||
| 306 | |||||||||||||||
| 307 | 1 | $dt1->set_time_zone($dt1_tz); | |||||||||||||
| 308 | 1 | $dt2->set_time_zone($dt2_tz); | |||||||||||||
| 309 | |||||||||||||||
| 310 | 1 | return DateTime::Duration::TauStation->new( | |||||||||||||
| 311 | gct_units => ( $seconds / $SECS_2_UNITS ), | ||||||||||||||
| 312 | ); | ||||||||||||||
| 313 | } | ||||||||||||||
| 314 | |||||||||||||||
| 315 | =head1 AUTHOR | ||||||||||||||
| 316 | |||||||||||||||
| 317 | Carl Franks | ||||||||||||||
| 318 | |||||||||||||||
| 319 | =head1 CREDITS | ||||||||||||||
| 320 | |||||||||||||||
| 321 | Based on code from L<https://github.com/zoffixznet/perl6-Games-TauStation-DateTime>. | ||||||||||||||
| 322 | |||||||||||||||
| 323 | =head1 COPYRIGHT | ||||||||||||||
| 324 | |||||||||||||||
| 325 | Copyright (c) 2018 Carl Franks. | ||||||||||||||
| 326 | |||||||||||||||
| 327 | This program is free software; you can redistribute it and/or modify it under | ||||||||||||||
| 328 | the same terms as Perl itself. | ||||||||||||||
| 329 | |||||||||||||||
| 330 | The full text of the license can be found in the LICENSE file included with | ||||||||||||||
| 331 | this module. | ||||||||||||||
| 332 | |||||||||||||||
| 333 | =head1 SEE ALSO | ||||||||||||||
| 334 | |||||||||||||||
| 335 | L<DateTime::Duration::TauStation>, L<DateTime::Format::TauStation> | ||||||||||||||
| 336 | |||||||||||||||
| 337 | =cut | ||||||||||||||