how to add seconds to datetime

English! place to talk about development, programming and coding
Post Reply
nicole19890107
Znuny newbie
Posts: 31
Joined: 24 Jun 2013, 22:30
Znuny Version: 3.2.6

how to add seconds to datetime

Post by nicole19890107 »

Hello,
I add the MyField7 to TicketPhone page, using Dynamic Fields - Ticket: Add Date / Time Field,
Image
Now I want to add seconds to this functionality, how can I get it?
crythias
Moderator
Posts: 10170
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: how to add seconds to datetime

Post by crythias »

find "Minute" within Kernel/System/DynamicField/Backend/DateTime.pm and add "Second" where doesn't exist...
This is a grep of some locations where you might want to see "Second"

Code: Select all

181-        $Value = $Param{Value};
182:        my ( $Year, $Month, $Day, $Hour, $Minute, $Second ) = $Value =~
--
195-            $FieldName . 'Minute' => $Minute,
196:                    $FieldName . 'Second' => $Second,
--
209-    if ( defined $FieldValues && IsHashRefWithData($FieldValues) ) {
210:        for my $Type (qw(Used Year Month Day Hour Minute Second)) {
--
310-    if ( IsHashRefWithData( $Param{Template} ) ) {
311:        for my $Type (qw(Used Year Month Day Hour Minute Second)) {
--
317-    else {
318:        for my $Type (qw(Used Year Month Day Hour Minute Second)) {
--
331-        && !$DynamicFieldValues{ $Prefix . 'Minute' }
332:        && !$DynamicFieldValues{ $Prefix . 'Second' };
--
360-        # time stamp
361:        for my $Type (qw(Month Day Hour Minute Second)) {
--
377-        my $Minute = $DynamicFieldValues{ $Prefix . 'Minute' } || '00';
378:        my $Second = $DynamicFieldValues{ $Prefix . 'Second' } || '00';
--
381-            $Year . '-' . $Month . '-' . $Day . ' '
382:            . $Hour . ':' . $Minute . ':' . $Second;
--
579-    for my $Type (qw(Start Stop)) {
580:        for my $Part (qw(Year Month Day Hour Minute Second)) {
--
615-
616:    $DynamicFieldValues{ $Prefix . 'StartSecond' } = '00';
617:    $DynamicFieldValues{ $Prefix . 'StopSecond' }  = '59';
--
628-    for my $Type (qw(Start Stop)) {
629:        for my $Part (qw(Month Day Hour Minute Second)) {
--
647-        $Prefix . 'StartMinute' => $DynamicFieldValues{ $Prefix . 'StartMinute' } || '00',
648:        $Prefix . 'StartSecond' => $DynamicFieldValues{ $Prefix . 'StartSecond' } || '00',
--
656-        $Prefix . 'StopMinute' => $DynamicFieldValues{ $Prefix . 'StopMinute' } || '00',
657:        $Prefix . 'StopSecond' => $DynamicFieldValues{ $Prefix . 'StopSecond' } || '00',
--
690-            . $Value->{ValueStart}->{ $Prefix . 'StartMinute' } . ':'
691:            . $Value->{ValueStart}->{ $Prefix . 'StartSecond' };
--
698-            . $Value->{ValueStop}->{ $Prefix . 'StopMinute' } . ':'
699:            . $Value->{ValueStop}->{ $Prefix . 'StopSecond' };
--
785-    my $MinuteValue = int( rand(30) ) + 10;
786:    my $SecondValue = int( rand(30) ) + 10;
--
788-    my $Value = $YearValue . '-0' . $MonthValue . '-' . $DayValue . ' '
789:        . $HourValue . ':' . $MinuteValue . ':' . $SecondValue;
 
and edit Kernel/Output/HTML/Layout.pm and add this after the # minute else around line 3200?

Code: Select all

        # second
        if ( $DateInputStyle eq 'Option' ) {
            my %Second;
            for ( 0 .. 59 ) {
                my $Tmp = sprintf( "%02d", $_ );
                $Second{$_} = $Tmp;
            }
            $Param{Second} = $Self->BuildSelection(
                Name       => $Prefix . 'Second',
                Data       => \%Second,
                SelectedID => defined( $Param{ $Prefix . 'Second' } )
                ? int( $Param{ $Prefix . 'Second' } )
                : int($s),
                Translation => 0,
                Class       => $Validate ? ( 'Validate_DateSecond ' . $Class ) : $Class,
                Title       => $Self->{LanguageObject}->Get('Seconds'),
            );
        }
        else {
            $Param{Second} = "<input type=\"text\" "
                . ( $Validate ? "class=\"Validate_DateSecond $Class\" " : "class=\"$Class\" " )
                . "name=\"${Prefix}Second\" id=\"${Prefix}Second\" size=\"2\" maxlength=\"2\" "
                . "title=\""
                . $Self->{LanguageObject}->Get('Seconds')
                . "\" value=\""
                . sprintf(
                "%02d",
                (
                    defined( $Param{ $Prefix . 'Second' } )
                    ? int( $Param{ $Prefix . 'Second' } )
                    : $s
                    )
                ) . "\"/>";
        } 
and also look for Minute where Second doesn't also exist.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
nicole19890107
Znuny newbie
Posts: 31
Joined: 24 Jun 2013, 22:30
Znuny Version: 3.2.6

Re: how to add seconds to datetime

Post by nicole19890107 »

I change codes according to your instruction, it worked. However, I got another problem now, I cannot display any information from dynamicfield(They are empty!).
Image
I think it happened because of the seconds functionalities. The error_log shows something is wrong in backend.pm about DisplayBalueRender:
sub DisplayValueRender
{
my ( $Self, %Param ) = @_;

# check needed stuff
for my $Needed (qw(DynamicFieldConfig LayoutObject)) {
if ( !$Param{$Needed} ) {
$Self->{LogObject}->Log( Priority => 'error', Message => "Need $Needed!" );
return;
}
}

# check DynamicFieldConfig (general)
if ( !IsHashRefWithData( $Param{DynamicFieldConfig} ) ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "The field configuration is invalid",
);
return;
}

# check DynamicFieldConfig (internally)
for my $Needed (qw(ID FieldType ObjectType Config Name)) {
if ( !$Param{DynamicFieldConfig}->{$Needed} ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Need $Needed in DynamicFieldConfig!"
);
return;
}
}

# set the dynamic field specific backend
my $DynamicFieldBackend = 'DynamicField' . $Param{DynamicFieldConfig}->{FieldType} . 'Object';

if ( !$Self->{$DynamicFieldBackend} ) {
$Self->{LogObject}->Log(
Priority => 'error',
Message => "Backend $Param{DynamicFieldConfig}->{FieldType} is invalid!"
);
return;
}

# call DisplayValueRender on the specific backend
my $ValueStrg = $Self->{$DynamicFieldBackend}->DisplayValueRender(
%Param
);

return $ValueStrg;
}
crythias
Moderator
Posts: 10170
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: how to add seconds to datetime

Post by crythias »

I *believe* you're looking at the wrong DisplayValueRender, and the error will be more specific than what you displayed.

Kernel/System/DynamicField/Backend/DateTimeField.pm

please post yours in tags.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
nicole19890107
Znuny newbie
Posts: 31
Joined: 24 Jun 2013, 22:30
Znuny Version: 3.2.6

Re: how to add seconds to datetime

Post by nicole19890107 »

Hey thank you for replying, the codes from DateTime.pm is following

Code: Select all

sub DisplayValueRender 
{
    my ( $Self, %Param ) = @_;

    my $Value = '';

    # convert date to localized string
    if ( defined $Param{Value} ) {
        $Value = $Param{LayoutObject}->Output(
            Template => '$TimeShort{"$Data{"Value"}"}',
            Data => { Value => $Param{Value}, },
        );
    }

    # in this backend there is no need for HTMLOutput
    # Title is always equal to Value
    my $Title = $Value;

    # set field link form config
    my $Link = $Param{DynamicFieldConfig}->{Config}->{Link} || '';

    my $Data = {
        Value => $Value,
        Title => $Title,
        Link  => $Link,
    };

    return $Data;
}
This is the information from error_log:
Image
crythias
Moderator
Posts: 10170
Joined: 04 May 2010, 18:38
Znuny Version: 5.0.x
Location: SouthWest Florida, USA
Contact:

Re: how to add seconds to datetime

Post by crythias »

'Tis a puzzlement, but "Need Config in DynamicFieldConfig" refers outside of this entry. I'd probably need to diff the entire DateTime.pm to source to make sure there isn't a misplaced semicolon or something....

my $Link = $Param{DynamicFieldConfig}->{Config}->{Link} || '';

This is saying that there isn't a "Config" for DynamicFieldConfig

This appears to be a problem storing the Config of the DynamicField in the DynamicField table.

Read: This is a problem with the field, not its data, as far as I can tell.

To verify: look at your database, DynamicField, config column for the field.

I can't give much more information than that, but if you disagree, stop now and revert those files as these changes won't survive version updates anyway.
OTRS 6.0.x (private/testing/public) on Linux with MySQL database.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask
nicole19890107
Znuny newbie
Posts: 31
Joined: 24 Jun 2013, 22:30
Znuny Version: 3.2.6

Re: how to add seconds to datetime

Post by nicole19890107 »

Hey, thank you for replying, I will check these codes again, and try to figure out more details. Because I need the functionality of seconds, I cannot just revert those files although I know it will disappear after update.
Thank you so much :)
nicole19890107
Znuny newbie
Posts: 31
Joined: 24 Jun 2013, 22:30
Znuny Version: 3.2.6

Re: how to add seconds to datetime

Post by nicole19890107 »

Hi, I reinstall the system and change those code again, now some information of dynamicfield can be showed, such as
Image
but there's no second shows there, for the ticket phone page, I have added the seconds functionality there:Image
so now my problem is to show the second functionality on the status page.
Post Reply