# SPDX-FileCopyrightText: 2025 Greenbone AG
# Some text descriptions might be excerpted from (a) referenced
# source(s), and are Copyright (C) by the respective right holder(s).
#
# SPDX-License-Identifier: GPL-2.0-only

# @brief Converts a passed date string generated by the unixtime() function
#        into a form like 20180813
#
# @param date a date string generated by the unixtime() function
#
# @todo Merge the functionality of make_date_str() in 2013/gb_host_scanned_wmi.nasl
#       and 2013/gb_host_scanned_ssh.nasl into this function.
#
# @return A string containing the current date in the form of 20180813 or NULL
#         if no date parameter was passed.
#
function make_date_str( date ) {

  local_var date;
  local_var time, month, day;

  if( isnull( date ) ) {
    set_kb_item( name:"vt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#date#-#make_date_str" );
    return;
  }

  time  = localtime( date );
  month = fancy_date( datestr:time["mon"]  );
  day   = fancy_date( datestr:time["mday"] );

  # Using string to make sure we return a string and don't do an addition of ints
  return string( time["year"], month, day );
}

# @brief Adds a leading 0/zero to a passed numeric string if the value is < 10
#        and returns the string back.
#
# @todo Merge the functionality of fancy_date() in 2013/gb_host_scanned_wmi.nasl,
#       2013/gb_host_scanned_ssh.nasl, gb_nist_win_oval_sys_char_generator.nasl
#       and kb_2_sc.nasl into this function.
#
# @param datastr the numeric string to evaluate
#
# @return a string with a leading 0 if the passed string was < 10, the same string
#         otherwise and NULL if no datestr parameter was passed.
#
function fancy_date( datestr ) {

  local_var datestr;

  if( isnull( datestr ) ) {
    set_kb_item( name:"vt_debug_empty/" + get_script_oid(), value:get_script_oid() + "#-#datestr#-#fancy_date" );
    return;
  }

  if( int( datestr ) < 10 )
    return string( "0", datestr );
  else
    return datestr;
}
