| 1 | package Crop::DateTime; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Crop::DateTime - General purpose Date and Time class for Crop framework | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Crop::DateTime; | ||
| 12 | my $dt = Crop::DateTime->new(); | ||
| 13 | my $epoch_ms = $dt->epoch_milli; | ||
| 14 | my $timestamp = $dt->timestamp; | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | Crop::DateTime provides date and time handling for the Crop framework, including high-resolution time, time zone support, and formatting utilities. By default, the time zone is set to Moscow time (UTC+3). | ||
| 19 | |||
| 20 | =head1 CONSTANTS | ||
| 21 | |||
| 22 | =over 4 | ||
| 23 | |||
| 24 | =item * DEFAULT_TIMEZONE | ||
| 25 | The default time zone (C<03:00>, Moscow time). | ||
| 26 | |||
| 27 | =back | ||
| 28 | |||
| 29 | =head1 ATTRIBUTES | ||
| 30 | |||
| 31 | =over 4 | ||
| 32 | |||
| 33 | =item * sec | ||
| 34 | Seconds since the epoch (read-only). | ||
| 35 | |||
| 36 | =item * usec | ||
| 37 | Microseconds (read-only). | ||
| 38 | |||
| 39 | =item * tz | ||
| 40 | Time zone (defaults to C<03:00>). | ||
| 41 | |||
| 42 | =back | ||
| 43 | |||
| 44 | =head1 METHODS | ||
| 45 | |||
| 46 | =head2 new([$time]) | ||
| 47 | |||
| 48 | my $dt = Crop::DateTime->new(); | ||
| 49 | my $dt = Crop::DateTime->new('2025-05-23 12:34:56.123456+03'); | ||
| 50 | |||
| 51 | Creates a new Crop::DateTime object. If C<$time> is provided (in Postgres format), it is used; otherwise, the current time is used. | ||
| 52 | |||
| 53 | =head2 epoch_milli | ||
| 54 | |||
| 55 | my $ms = $dt->epoch_milli; | ||
| 56 | |||
| 57 | Returns the epoch time in milliseconds as a string. | ||
| 58 | |||
| 59 | =head2 timestamp | ||
| 60 | |||
| 61 | my $ts = $dt->timestamp; | ||
| 62 | |||
| 63 | Returns the time in the format C<'TIMESTAMP(6) WITH TIME ZONE'>. | ||
| 64 | |||
| 65 | =head1 DEPENDENCIES | ||
| 66 | |||
| 67 | =over 4 | ||
| 68 | |||
| 69 | =item * Time::HiRes | ||
| 70 | =item * Date::Parse | ||
| 71 | |||
| 72 | =back | ||
| 73 | |||
| 74 | =head1 AUTHORS | ||
| 75 | |||
| 76 | Euvgenio (Core Developer) | ||
| 77 | |||
| 78 | Alex (Contributor) | ||
| 79 | |||
| 80 | =head1 COPYRIGHT AND LICENSE | ||
| 81 | |||
| 82 | Apache 2.0 | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | use base qw/ Crop::Object /; | ||
| 87 | |||
| 88 | =begin nd | ||
| 89 | Class: Crop::DateTime | ||
| 90 | General purpose Date and Time. | ||
| 91 | =cut | ||
| 92 | |||
| 93 | use v5.14; | ||
| 94 | use warnings; | ||
| 95 | |||
| 96 | use Time::HiRes qw/ gettimeofday /; | ||
| 97 | use Date::Parse; | ||
| 98 | |||
| 99 | use Crop::Error; | ||
| 100 | |||
| 101 | use Crop::Debug; | ||
| 102 | |||
| 103 | =begin nd | ||
| 104 | Constant: DEFAULT_TIMEZONE | ||
| 105 | By default is the Moscow time (UTC+3). | ||
| 106 | =cut | ||
| 107 | use constant DEFAULT_TIMEZONE => '03:00'; | ||
| 108 | |||
| 109 | =begin nd | ||
| 110 | Variable: our %Attributes | ||
| 111 | Class attributes: | ||
| 112 | |||
| 113 | sec - seconds, | ||
| 114 | usec - microseconds the left aligned! | ||
| 115 | tz - time zone, | ||
| 116 | =cut | ||
| 117 | our %Attributes = ( | ||
| 118 | sec => {mode => 'read'}, | ||
| 119 | usec => {mode => 'read'}, | ||
| 120 | tz => {default => DEFAULT_TIMEZONE}, | ||
| 121 | ); | ||
| 122 | |||
| 123 | =begin nd | ||
| 124 | Constructor: new ($time) | ||
| 125 | Init exemplar with the $time data. | ||
| 126 | |||
| 127 | Parameters: | ||
| 128 | $time - in postgres format; optional; | ||
| 129 | unless $time is given, the current time is used | ||
| 130 | |||
| 131 | Returns: | ||
| 132 | $self | ||
| 133 | =cut | ||
| 134 | sub new { | ||
| 135 | my ($class, $time) = @_; | ||
| 136 | |||
| 137 | my ($second, $usecond); | ||
| 138 | my $tz = DEFAULT_TIMEZONE; | ||
| 139 | |||
| 140 | if (defined $time) { | ||
| 141 | $second = str2time $time; | ||
| 142 | $second =~ s/\.\d+//; | ||
| 143 | ($usecond) = $time =~ /\.(\d+)/; | ||
| 144 | |||
| 145 | my ($sign, $zone) = $time =~ /(\+|-)(\d{1,2})$/; | ||
| 146 | $tz = "$sign$zone"; | ||
| 147 | } else { | ||
| 148 | ($second, $usecond) = gettimeofday; | ||
| 149 | } | ||
| 150 | |||
| 151 | $class->SUPER::new( | ||
| 152 | sec => $second, | ||
| 153 | usec => $usecond || 0, | ||
| 154 | tz => $tz, | ||
| 155 | ); | ||
| 156 | } | ||
| 157 | |||
| 158 | =begin nd | ||
| 159 | Method: epoch_milli ( ) | ||
| 160 | The 'epoch' time in milliseconds | ||
| 161 | |||
| 162 | Returns: | ||
| 163 | 1652907999123 string where last 123 are milliseconds | ||
| 164 | =cut | ||
| 165 | sub epoch_milli { | ||
| 166 | my $self = shift; | ||
| 167 | |||
| 168 | my $result = $self->{sec} * 1000; # in milliseconds without mantissa | ||
| 169 | |||
| 170 | my ($usec) = $self->{usec} =~ /^(\d{0,3})/; # milliseconds part | ||
| 171 | |||
| 172 | my $left = 3 - length $usec; | ||
| 173 | $usec *= 10**$left if $left; | ||
| 174 | |||
| 175 | $result + $usec; | ||
| 176 | } | ||
| 177 | |||
| 178 | =begin nd | ||
| 179 | Method: timestamp ( ) | ||
| 180 | Time in the format of 'TIMESTAMP(6) WITH TIME ZONE'. | ||
| 181 | =cut | ||
| 182 | sub timestamp { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | return warn 'TIME: Time is not initialized' unless defined $self->{sec} and defined $self->{usec}; | ||
| 186 | |||
| 187 | my ($second, $minute, $hour, $monthday, $month, $year, $weekday, $yearday, $isdst) = localtime $self->{sec}; | ||
| 188 | my $usecond = $self->{usec}; | ||
| 189 | my $timezone = $self->{tz}; | ||
| 190 | |||
| 191 | sprintf("%04d-%02d-%02d %02d:%02d:%02d.%06d+%s", $year+1900, $month+1, $monthday, $hour, $minute, $second, $usecond, $timezone); | ||
| 192 | } | ||
| 193 | |||
| 194 | 1; |