在unix shell ksh中将日期字符串转换为长字符串

在unix shell ksh中将日期字符串转换为长字符串,unix,ksh,Unix,Ksh,如何将下面的c代码转换为ksh脚本。下面的代码从年、月和日字符串生成日期。然后将日期转换为长变量 /* assemble date string */ sprintf(date_str,"%s%2s%s",year_str,month_str,day_str); /* convert to a long */ str2long((char *) date_str, 7, (long *) &long_date); 如果您只想将其转换为直长值,可以使用以下函数: #

如何将下面的c代码转换为ksh脚本。下面的代码从年、月和日字符串生成日期。然后将日期转换为长变量

    /* assemble date string */
sprintf(date_str,"%s%2s%s",year_str,month_str,day_str);

/* convert to a long  */
str2long((char *) date_str, 7, (long *) &long_date);    

如果您只想将其转换为直长值,可以使用以下函数:

# Call with year month day
# e.g. convert_date_to_long 2015 01 01
function convert_date_to_long() {
    date="${1}${2}${3}"
    let long_date=$date

    if [[ $long_date -gt 0 ]]; then
        echo "Numeric date: ${long_date}"
    fi
}
# Call with year month day
# e.g. convert_date_to_epoch 2015 01 01
function convert_date_to_epoch() {
    awk -v year=${1} -v month=${2} -v day=${3} 'BEGIN {
        agg_date = year " " month " " day " 00 00 00";
        utime = mktime(agg_date);
        print utime;
    }'
}
如果您不介意在ksh脚本中使用AWK语言,并且希望将其转换为历元时间戳,则可以使用以下函数:

# Call with year month day
# e.g. convert_date_to_long 2015 01 01
function convert_date_to_long() {
    date="${1}${2}${3}"
    let long_date=$date

    if [[ $long_date -gt 0 ]]; then
        echo "Numeric date: ${long_date}"
    fi
}
# Call with year month day
# e.g. convert_date_to_epoch 2015 01 01
function convert_date_to_epoch() {
    awk -v year=${1} -v month=${2} -v day=${3} 'BEGIN {
        agg_date = year " " month " " day " 00 00 00";
        utime = mktime(agg_date);
        print utime;
    }'
}

如果您只想将其转换为直长值,可以使用以下函数:

# Call with year month day
# e.g. convert_date_to_long 2015 01 01
function convert_date_to_long() {
    date="${1}${2}${3}"
    let long_date=$date

    if [[ $long_date -gt 0 ]]; then
        echo "Numeric date: ${long_date}"
    fi
}
# Call with year month day
# e.g. convert_date_to_epoch 2015 01 01
function convert_date_to_epoch() {
    awk -v year=${1} -v month=${2} -v day=${3} 'BEGIN {
        agg_date = year " " month " " day " 00 00 00";
        utime = mktime(agg_date);
        print utime;
    }'
}
如果您不介意在ksh脚本中使用AWK语言,并且希望将其转换为历元时间戳,则可以使用以下函数:

# Call with year month day
# e.g. convert_date_to_long 2015 01 01
function convert_date_to_long() {
    date="${1}${2}${3}"
    let long_date=$date

    if [[ $long_date -gt 0 ]]; then
        echo "Numeric date: ${long_date}"
    fi
}
# Call with year month day
# e.g. convert_date_to_epoch 2015 01 01
function convert_date_to_epoch() {
    awk -v year=${1} -v month=${2} -v day=${3} 'BEGIN {
        agg_date = year " " month " " day " 00 00 00";
        utime = mktime(agg_date);
        print utime;
    }'
}

您从2015年1月1日起所需的输出是否为数值20150101?在重温上述示例后,我认为它似乎没有得到数值20150101。我无法理解所需的输出。有什么帮助吗?您从2015年1月1日起所需的输出是否为数值20150101?在重温上述示例后,我觉得它似乎没有得到数值20150101。我无法理解所需的输出。有什么帮助吗?