在 unix shell ksh 中将日期字符串转换为 long
Convert date string to long in unix shell ksh
如何将下面的 c 代码转换为 ksh 脚本。下面的代码从年月日字符串生成日期。然后将日期转换为long变量
/* 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);
如果您只是想将它转换为一个直接的 long 值,您可以使用以下函数来完成:
# Call with year month day
# e.g. convert_date_to_long 2015 01 01
function convert_date_to_long() {
date=""
let long_date=$date
if [[ $long_date -gt 0 ]]; then
echo "Numeric date: ${long_date}"
fi
}
如果您不介意在 ksh 脚本中使用 AWK 语言并且想将其转换为纪元时间戳,您可以使用以下函数来完成:
# Call with year month day
# e.g. convert_date_to_epoch 2015 01 01
function convert_date_to_epoch() {
awk -v year= -v month= -v day= 'BEGIN {
agg_date = year " " month " " day " 00 00 00";
utime = mktime(agg_date);
print utime;
}'
}
如何将下面的 c 代码转换为 ksh 脚本。下面的代码从年月日字符串生成日期。然后将日期转换为long变量
/* 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);
如果您只是想将它转换为一个直接的 long 值,您可以使用以下函数来完成:
# Call with year month day
# e.g. convert_date_to_long 2015 01 01
function convert_date_to_long() {
date=""
let long_date=$date
if [[ $long_date -gt 0 ]]; then
echo "Numeric date: ${long_date}"
fi
}
如果您不介意在 ksh 脚本中使用 AWK 语言并且想将其转换为纪元时间戳,您可以使用以下函数来完成:
# Call with year month day
# e.g. convert_date_to_epoch 2015 01 01
function convert_date_to_epoch() {
awk -v year= -v month= -v day= 'BEGIN {
agg_date = year " " month " " day " 00 00 00";
utime = mktime(agg_date);
print utime;
}'
}