| 1 | package SPVM::Time::Piece; | ||
| 2 | |||
| 3 | 1 | our $VERSION = "0.012"; | |
| 4 | |||
| 5 | 1 | 1; | |
| 6 | |||
| 7 | =head1 Name | ||
| 8 | |||
| 9 | SPVM::Time::Piece - Dates and Times | ||
| 10 | |||
| 11 | =head1 Description | ||
| 12 | |||
| 13 | Time::Piece class in L<SPVM> manipulates dates and times. | ||
| 14 | |||
| 15 | =head1 Usage | ||
| 16 | |||
| 17 | use Time::Piece; | ||
| 18 | |||
| 19 | my $tp = Time::Piece->localtime; | ||
| 20 | say "Time is " . $tp->cdate; | ||
| 21 | say "Year is " . $tp->year; | ||
| 22 | |||
| 23 | =head2 Interfaces | ||
| 24 | |||
| 25 | =over 2 | ||
| 26 | |||
| 27 | =item L<Cloneable|SPVM::Cloneable> | ||
| 28 | |||
| 29 | =back | ||
| 30 | |||
| 31 | =head1 Fields | ||
| 32 | |||
| 33 | =head2 is_localtime | ||
| 34 | |||
| 35 | C<has is_localtime : byte;> | ||
| 36 | |||
| 37 | The flag if the L</"tm"> field is interpreted as a local time. | ||
| 38 | |||
| 39 | =head2 tm | ||
| 40 | |||
| 41 | C<has tm : L<Sys::Time::Tm|SPVM::Sys::Time::Tm>;> | ||
| 42 | |||
| 43 | Time information. | ||
| 44 | |||
| 45 | =head2 epoch | ||
| 46 | |||
| 47 | C<has epoch : ro long;> | ||
| 48 | |||
| 49 | Seconds from epoch time. | ||
| 50 | |||
| 51 | =head1 Class Methods | ||
| 52 | |||
| 53 | =head2 localtime | ||
| 54 | |||
| 55 | C<static method localtime : L<Time::Piece|SPVM::Time::Piece> ($epoch : long = -1, $allow_minus : int = 0);> | ||
| 56 | |||
| 57 | Creates a bew L<Time::Piece|SPVM::Time::Piece> object given the epoch time, and returns it. | ||
| 58 | |||
| 59 | This instance represents the user's specified timezone. | ||
| 60 | |||
| 61 | If $allow_minus is 0 and $epoch is less than 0, $epoch is set to the current epoch time. | ||
| 62 | |||
| 63 | The L</"epoch">, L</"tm">, and L</"is_localtime"> fields are set to appropriate values. | ||
| 64 | |||
| 65 | =head2 localtime_tp | ||
| 66 | |||
| 67 | C<static method localtime_tp : L<Time::Piece|SPVM::Time::Piece> ($tp : L<Time::Piece|SPVM::Time::Piece>);> | ||
| 68 | |||
| 69 | Creates a new L<Time::Piece|SPVM::Time::Piece> object by interpreting $tp as the user's specified timezone, and returns it. | ||
| 70 | |||
| 71 | This instance represents the user's specified timezone, | ||
| 72 | |||
| 73 | The L</"epoch">, L</"tm">, and L</"is_localtime"> fields are set to appropriate values. | ||
| 74 | |||
| 75 | =head2 gmtime | ||
| 76 | |||
| 77 | C<static method gmtime : L<Time::Piece|SPVM::Time::Piece> ($epoch : long = -1, $allow_minus : int = 0);> | ||
| 78 | |||
| 79 | Creates a new L<Time::Piece|SPVM::Time::Piece> object given the epoch time, and returns it. | ||
| 80 | |||
| 81 | This instance represents UTC timezone, | ||
| 82 | |||
| 83 | If $allow_minus is 0 and $epoch is less than 0, $epoch is set to the current epoch time. | ||
| 84 | |||
| 85 | The L</"epoch">, L</"tm">, and L</"is_localtime"> fields are set to appropriate values. | ||
| 86 | |||
| 87 | =head2 gmtime_tp | ||
| 88 | |||
| 89 | C<static method gmtime_tp : L<Time::Piece|SPVM::Time::Piece> ($tp : L<Time::Piece|SPVM::Time::Piece>);> | ||
| 90 | |||
| 91 | Creates a new L<Time::Piece|SPVM::Time::Piece> object by interpreting $tp as UTC timezone, and returns it. | ||
| 92 | |||
| 93 | This instance represents UTC timezone, | ||
| 94 | |||
| 95 | The L</"epoch">, L</"tm">, and L</"is_localtime"> fields are set to appropriate values. | ||
| 96 | |||
| 97 | =head2 strptime | ||
| 98 | |||
| 99 | C<static method strptime : L<Time::Piece|SPVM::Time::Piece> ($string : string, $format : string);> | ||
| 100 | |||
| 101 | Parses the string $string according to the format $format. | ||
| 102 | |||
| 103 | This method calls L<std::get_time|https://en.cppreference.com/w/cpp/io/manip/get_time> in C++. | ||
| 104 | |||
| 105 | See the L<std::get_time|https://en.cppreference.com/w/cpp/io/manip/get_time> function about input field descriptors such as C<%Y>, C<%m>, C<%d>, C<%H>, C<%M>, C<%S>. | ||
| 106 | |||
| 107 | Exceptions: | ||
| 108 | |||
| 109 | $string must be defined. Otherwise an exception is thrown. | ||
| 110 | |||
| 111 | $format must be defined. Otherwise an exception is thrown. | ||
| 112 | |||
| 113 | If std::get_time failed, an exception is thrown. | ||
| 114 | |||
| 115 | =head1 Instance Methods | ||
| 116 | |||
| 117 | =head2 sec | ||
| 118 | |||
| 119 | C<method sec : int ();> | ||
| 120 | |||
| 121 | Returns the second. This is the value of L<tm_sec|SPVM::Sys::Time::Tm/"tm_sec"> in the Sys::Time::Tm class. | ||
| 122 | |||
| 123 | =head2 second | ||
| 124 | |||
| 125 | C<method second : int ();> | ||
| 126 | |||
| 127 | The same as the L</"sec"> method. | ||
| 128 | |||
| 129 | =head2 min | ||
| 130 | |||
| 131 | C<method min : int ();> | ||
| 132 | |||
| 133 | Returns the minute. This is the value of L<tm_min|SPVM::Sys::Time::Tm/"tm_min"> in the Sys::Time::Tm class. | ||
| 134 | |||
| 135 | =head2 minute | ||
| 136 | |||
| 137 | C<method minute : int ();> | ||
| 138 | |||
| 139 | The same as the L</"min"> method. | ||
| 140 | |||
| 141 | =head2 hour | ||
| 142 | |||
| 143 | C<method hour : int ();> | ||
| 144 | |||
| 145 | Returns the hour. This is the value of L<tm_hour|SPVM::Sys::Time::Tm/"tm_hour"> in the Sys::Time::Tm class. | ||
| 146 | |||
| 147 | =head2 mday | ||
| 148 | |||
| 149 | C<method mday : int ();> | ||
| 150 | |||
| 151 | Returns the day of the month. This is the value of L<tm_mday|SPVM::Sys::Time::Tm/"tm_mday"> in the Sys::Time::Tm class. | ||
| 152 | |||
| 153 | =head2 day_of_month | ||
| 154 | |||
| 155 | C<method day_of_month : int ();> | ||
| 156 | |||
| 157 | =head2 mon | ||
| 158 | |||
| 159 | C<method mon : int ();> | ||
| 160 | |||
| 161 | Returns the month. This is the value of L<tm_mon|SPVM::Sys::Time::Tm/"tm_mon"> plus 1 in the Sys::Time::Tm class. | ||
| 162 | |||
| 163 | =head2 _mon | ||
| 164 | |||
| 165 | C<method _mon : int ();> | ||
| 166 | |||
| 167 | Returns the value of L<tm_mon|SPVM::Sys::Time::Tm/"tm_mon"> in the Sys::Time::Tm class. | ||
| 168 | |||
| 169 | =head2 monname | ||
| 170 | |||
| 171 | C<method monname : string ($mon_list : string[] = undef);> | ||
| 172 | |||
| 173 | Returns the month name given $mon_list. | ||
| 174 | |||
| 175 | The default $mon_list: | ||
| 176 | |||
| 177 | ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] | ||
| 178 | |||
| 179 | =head2 month | ||
| 180 | |||
| 181 | C<method month : string ($mon_list : string[] = undef);> | ||
| 182 | |||
| 183 | The same as the L</"monname"> method. | ||
| 184 | |||
| 185 | =head2 fullmonth | ||
| 186 | |||
| 187 | C<method fullmonth : string ($mon_list : string[] = undef);> | ||
| 188 | |||
| 189 | Returns the full month name given $mon_list. | ||
| 190 | |||
| 191 | The default $mon_list: | ||
| 192 | |||
| 193 | ["January", "February", "March", "April", "May", "June", "July", | ||
| 194 | "August", "September", "October", "November", "December"] | ||
| 195 | =head2 year | ||
| 196 | |||
| 197 | C<method year : int ();> | ||
| 198 | |||
| 199 | Returns the year. This is the value of L<tm_mday|SPVM::Sys::Time::Tm/"tm_mday"> plus 1900 in the Sys::Time::Tm class. | ||
| 200 | |||
| 201 | =head2 _year | ||
| 202 | |||
| 203 | C<method _year : int ();> | ||
| 204 | |||
| 205 | Returns the value of L<tm_year|SPVM::Sys::Time::Tm/"tm_year"> in the Sys::Time::Tm class. | ||
| 206 | |||
| 207 | =head2 yy | ||
| 208 | |||
| 209 | C<method yy : int ();> | ||
| 210 | |||
| 211 | Returns the the last two digits of L</"year">. | ||
| 212 | |||
| 213 | =head2 wday | ||
| 214 | |||
| 215 | C<method wday : int ();> | ||
| 216 | |||
| 217 | Returns the week number, interpreting Sunday as 1. This is the value of L<tm_wday|SPVM::Sys::Time::Tm/"tm_wday"> plus 1 in the Sys::Time::Tm class. | ||
| 218 | |||
| 219 | =head2 _wday | ||
| 220 | |||
| 221 | C<method _wday : int ();> | ||
| 222 | |||
| 223 | The same as the L</"day_of_week"> method. | ||
| 224 | |||
| 225 | =head2 day_of_week | ||
| 226 | |||
| 227 | C<method day_of_week : int ();> | ||
| 228 | |||
| 229 | Returns the week number, interpreting Sunday as 0. This is the value of L<tm_wday|SPVM::Sys::Time::Tm/"tm_wday"> in the Sys::Time::Tm class. | ||
| 230 | |||
| 231 | =head2 wdayname | ||
| 232 | |||
| 233 | C<method wdayname : string ($day_list : string[] = undef);> | ||
| 234 | |||
| 235 | Returns the name of the day of the week given $day_list. | ||
| 236 | |||
| 237 | The default $day_list: | ||
| 238 | |||
| 239 | ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] | ||
| 240 | |||
| 241 | =head2 day | ||
| 242 | |||
| 243 | C<method day : string ($day_list : string[] = undef);> | ||
| 244 | |||
| 245 | The same as the L</"wdayname"> method. | ||
| 246 | |||
| 247 | =head2 fullday | ||
| 248 | |||
| 249 | C<method fullday : string ($day_list : string[] = undef);> | ||
| 250 | |||
| 251 | Returns the full name of the day of the week given $day_list. | ||
| 252 | |||
| 253 | The default $day_list: | ||
| 254 | |||
| 255 | ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] | ||
| 256 | |||
| 257 | =head2 yday | ||
| 258 | |||
| 259 | C<method yday : int ();> | ||
| 260 | |||
| 261 | Returns the day in the year. This is the value of L<tm_yday|SPVM::Sys::Time::Tm/"tm_yday"> in the Sys::Time::Tm class. | ||
| 262 | |||
| 263 | =head2 day_of_year | ||
| 264 | |||
| 265 | C<method day_of_year : int ();> | ||
| 266 | |||
| 267 | The same as the L</"yday"> method. | ||
| 268 | |||
| 269 | =head2 isdst | ||
| 270 | |||
| 271 | C<method isdst : int ();> | ||
| 272 | |||
| 273 | Returns the flag thats indicate daylight saving time. This is the value of L<tm_isdst|SPVM::Sys::Time::Tm/"tm_isdst"> in the Sys::Time::Tm class. | ||
| 274 | |||
| 275 | =head2 daylight_savings | ||
| 276 | |||
| 277 | C<method daylight_savings : int ();> | ||
| 278 | |||
| 279 | The same as the L</"isdst"> method. | ||
| 280 | |||
| 281 | =head2 hms | ||
| 282 | |||
| 283 | C<method hms : string ($sep : string = undef);> | ||
| 284 | |||
| 285 | Formats the hour, the minute, and the second into a two-digit string respectively, and concatenates them with the given separator $sep, and returns it. | ||
| 286 | |||
| 287 | If $sep is not defined, $sep is set to C<:>. | ||
| 288 | |||
| 289 | =head2 time | ||
| 290 | |||
| 291 | C<method time : string ($sep : string = undef);> | ||
| 292 | |||
| 293 | The same as the L</"hms"> method. | ||
| 294 | |||
| 295 | =head2 ymd | ||
| 296 | |||
| 297 | C<method ymd : string ($sep : string = undef);> | ||
| 298 | |||
| 299 | Formats the year, the month, and the day of the month into a four-digit string, a two-digit string, a two-digit string respectively, and concatenates them with the given separator $sep, and returns it. | ||
| 300 | |||
| 301 | If $sep is not defined, $sep is set to C<->. | ||
| 302 | |||
| 303 | =head2 date | ||
| 304 | |||
| 305 | C<method date : string ($sep : string = undef);> | ||
| 306 | |||
| 307 | The same as the L</"ymd"> method. | ||
| 308 | |||
| 309 | =head2 mdy | ||
| 310 | |||
| 311 | C<method mdy : string ($sep : string = undef);> | ||
| 312 | |||
| 313 | Formats the month, the day of the month, and the year into a two-digit string, a two-digit string, a four-digit string respectively, and concatenates them with the given separator $sep, and returns it. | ||
| 314 | |||
| 315 | If $sep is not defined, $sep is set to C<->. | ||
| 316 | |||
| 317 | =head2 dmy | ||
| 318 | |||
| 319 | C<method dmy : string ($sep : string = undef);> | ||
| 320 | |||
| 321 | Formats the day of the month, the month, and the year into a two-digit string, a two-digit string, a four-digit string respectively, and concatenates them with the given separator $sep, and returns it. | ||
| 322 | |||
| 323 | If $sep is not defined, $sep is set to C<->. | ||
| 324 | |||
| 325 | =head2 datetime | ||
| 326 | |||
| 327 | C<method datetime : string ();> | ||
| 328 | |||
| 329 | Formats the time information into ISO 8601 format like C<2000-02-29T12:34:56>, and returns it. | ||
| 330 | |||
| 331 | =head2 tzoffset | ||
| 332 | |||
| 333 | C<method tzoffset : L<Time::Seconds|SPVM::Time::Seconds> ();> | ||
| 334 | |||
| 335 | Calculates the timezone offset, and returns it. | ||
| 336 | |||
| 337 | =head2 julian_day | ||
| 338 | |||
| 339 | C<method julian_day : double ();> | ||
| 340 | |||
| 341 | Calculates the number of days since Julian period began, and returns it. | ||
| 342 | |||
| 343 | =head2 mjd | ||
| 344 | |||
| 345 | C<method mjd : double ();> | ||
| 346 | |||
| 347 | Calculates the modified Julian date (JD minus 2400000.5 days). | ||
| 348 | |||
| 349 | =head2 week | ||
| 350 | |||
| 351 | C<method week : int ();> | ||
| 352 | |||
| 353 | Calculate the week number (ISO 8601), and returns it. | ||
| 354 | |||
| 355 | =head2 is_leap_year | ||
| 356 | |||
| 357 | C<method is_leap_year : int ();> | ||
| 358 | |||
| 359 | If the year is a leap year, returns 1, otherwise returns 0. | ||
| 360 | |||
| 361 | =head2 month_last_day | ||
| 362 | |||
| 363 | C<method month_last_day : int ();> | ||
| 364 | |||
| 365 | Returns the last day of the month. | ||
| 366 | |||
| 367 | =head2 cdate | ||
| 368 | |||
| 369 | C<method cdate : string ();> | ||
| 370 | |||
| 371 | Formats the time information into the string like C<Tue Feb 29 12:34:56 2000>. | ||
| 372 | |||
| 373 | =head2 strftime | ||
| 374 | |||
| 375 | C<method method strftime : string ($format : string = undef);> | ||
| 376 | |||
| 377 | Formats the time information into a string according to the format $format, and returns it. | ||
| 378 | |||
| 379 | See the L<strftime|https://linux.die.net/man/3/strftime> function about conversion specifications such as C<%Y>, C<%m>, C<%d>, C<%H>, C<%M>, C<%S>. | ||
| 380 | |||
| 381 | If $format is not defined, it is set to C<%a, %d %b %Y %H:%M:%S %Z>. | ||
| 382 | |||
| 383 | Exceptions: | ||
| 384 | |||
| 385 | The length of $format must be greater than 1. Otherwise an exception is thrown. | ||
| 386 | |||
| 387 | If too many memory is allocated, an exception is thrown. | ||
| 388 | |||
| 389 | =head2 add | ||
| 390 | |||
| 391 | C<method add : L<Time::Piece|SPVM::Time::Piece> ($tsec : L<Time::Seconds|SPVM::Time::Seconds>);> | ||
| 392 | |||
| 393 | Creates a new L<Time::Piece|SPVM::Time::Piece> object with the given seconds $tsec added, and returns it. | ||
| 394 | |||
| 395 | =head2 subtract | ||
| 396 | |||
| 397 | C<method subtract : L<Time::Piece|SPVM::Time::Piece> ($tsec : L<Time::Seconds|SPVM::Time::Seconds>);> | ||
| 398 | |||
| 399 | Creates a new L<Time::Piece|SPVM::Time::Piece> object with the given seconds $tsec subtracted, and returns it. | ||
| 400 | |||
| 401 | =head2 subtract_tp | ||
| 402 | |||
| 403 | C<method subtract_tp : L<Time::Seconds|SPVM::Time::Seconds> ($tp : L<Time::Piece|SPVM::Time::Piece>);> | ||
| 404 | |||
| 405 | Creates a new L<Time::Seconds|SPVM::Time::Seconds> object given the seconds that is L</"seconds"> fields to the L</"epoch"> field of this instance munus the L</"epoch"> field of $tp. | ||
| 406 | |||
| 407 | =head2 compare | ||
| 408 | |||
| 409 | C<method compare : int ($tp : L<Time::Piece|SPVM::Time::Piece>);> | ||
| 410 | |||
| 411 | If the L</"epoch"> field of this instance is greater than the L</"epoch"> field of $tp, returns 1. | ||
| 412 | |||
| 413 | If the L</"epoch"> field of this instance is less than the L</"epoch"> field of $tp, returns -1. | ||
| 414 | |||
| 415 | If the L</"epoch"> field of this instance is equal to the L</"epoch"> field of $tp, returns 0. | ||
| 416 | |||
| 417 | =head2 add_months | ||
| 418 | |||
| 419 | C<method add_months : L<Time::Piece|SPVM::Time::Piece> ($num_months : int);> | ||
| 420 | |||
| 421 | Returns a new L<Time::Piece|SPVM::Time::Piece> object with the month added by $num_months. | ||
| 422 | |||
| 423 | =head2 add_years | ||
| 424 | |||
| 425 | C<method add_years : L<Time::Piece|SPVM::Time::Piece> ($years : int);> | ||
| 426 | |||
| 427 | Returns a new L<Time::Piece|SPVM::Time::Piece> object with the year added by $years. | ||
| 428 | |||
| 429 | =head2 truncate | ||
| 430 | |||
| 431 | C<method truncate : L<Time::Piece|SPVM::Time::Piece> ($options : object[]);> | ||
| 432 | |||
| 433 | Calling the truncate method returns a copy of the object but with the time truncated to the start of the supplied unit C<to>. | ||
| 434 | |||
| 435 | Options: | ||
| 436 | |||
| 437 | =over 2 | ||
| 438 | |||
| 439 | =item C<to> : string | ||
| 440 | |||
| 441 | "year", "quarter", "month", "day", "hour", "minute", and "second". | ||
| 442 | |||
| 443 | =back | ||
| 444 | |||
| 445 | Excamples: | ||
| 446 | |||
| 447 | $tp = $tp->truncate({to => "day"}); | ||
| 448 | |||
| 449 | =head2 clone | ||
| 450 | |||
| 451 | C<method clone : L<Time::Piece|SPVM::Time::Piece> ();> | ||
| 452 | |||
| 453 | Clones this instance, and returns it. | ||
| 454 | |||
| 455 | =head1 Repository | ||
| 456 | |||
| 457 | L<SPVM::Time::Piece - Github|https://github.com/yuki-kimoto/SPVM-Time-Piece> | ||
| 458 | |||
| 459 | =head1 Author | ||
| 460 | |||
| 461 | Yuki Kimoto C<method kimoto.yuki@gmail.com> | ||
| 462 | |||
| 463 | =head1 Copyright & License | ||
| 464 | |||
| 465 | Copyright (c) 2023 Yuki Kimoto | ||
| 466 | |||
| 467 | MIT License | ||
| 468 |