# -- # Kernel/System/DateUtils.pm - date/time utils # # Copyright (C) 2014-2018 - SeTIC - UFSC - http://setic.ufsc.br/ # Version 2015-08-01 - Adjustments for OTRS 4 # Version 2018-01-05 - Adjustments for OTRS 6 # # This software comes with ABSOLUTELY NO WARRANTY. For details, see # the enclosed file COPYING for license information (AGPL). If you # did not receive this file, see http://www.gnu.org/licenses/agpl.txt. # -- # package Kernel::System::DateUtils; use strict; use warnings; use Data::Dumper; require DateTime; use Time::Piece; our @ObjectDependencies = (); sub new { my ( $Type, %Param ) = @_; # allocate new hash for object my $Self = {%Param}; bless( $Self, $Type ); return $Self; } =item NewTimestamp() Creates a new date from the parameters $ocurrenceObject->NewTimestamp( Year => int, Month => int, Day => int, Hour => int, Minute => int, Second => int ); =cut sub NewTimestamp { my ( $Self, %Param ) = @_; my $date = DateTime->new( year => $Param{Year}, month => $Param{Month}, day => $Param{Day}, hour => $Param{Hour}, minute => $Param{Minute}, second => $Param{Second} ); return $date; } =item AddDays() Adds a number of days to a timestamp $ocurrenceObject->AddDays( Timestamp => DateTime, Days => int ); =cut sub AddDays { my ( $Self, %Param ) = @_; my $timestamp = $Param{Timestamp}; $timestamp->add( days => $Param{Days} ); } =item GetDateFromParams() Create a DateTime based on parameter values $ocurrenceObject->GetDateFromParams( Prefix => string, ParamObject => ParamObject ); =cut sub GetDateFromParams { my ( $Self, %Param ) = @_; my $Prefix = $Param{"Prefix"}; my $ParamObject = $Param{"ParamObject"}; return $Self->NewTimestamp( Year => $ParamObject->GetParam(Param => $Prefix . "Year"), Month => $ParamObject->GetParam(Param => $Prefix . "Month"), Day => $ParamObject->GetParam(Param => $Prefix . "Day"), Hour => $ParamObject->GetParam(Param => $Prefix . "Hour"), Minute => $ParamObject->GetParam(Param => $Prefix . "Minute"), Second => 0 ); } sub SQLDate { my ( $Self, %Param ) = @_; my $timestamp = $Param{"Timestamp"}; return $timestamp->strftime("%y-%m-%d %H:%M:%S"); } sub BrazilianDate { my ( $Self, %Param ) = @_; my $timestamp = $Param{"Timestamp"}; return $timestamp->strftime("%d/%m/%y %H:%M:%S"); } sub FromSQL { my ( $Self, %Param ) = @_; my $timestamp = $Param{"StringDate"}; return Time::Piece->strptime($timestamp, "%Y-%m-%d %H:%M:%S"); } 1;