Perl 的 rtoday() ESQL 函数?
rtoday() ESQL function for Perl?
我目前正在用 Perl 重写一个 Informix ESQL/C 程序。我遇到的问题之一是试图获得与 Perl 中的 rtoday()
完全相同的值。
查看 IBM 的文档后:
http://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.esqlc.doc/sii-xbrstod-41016.htm%23sii-xbrstod-41016
"today
A pointer to an int4 value that receives the internal DATE.
Usage
The rtoday() function obtains the system date on the client computer, not
the server computer."
在命令行上执行 echo $DATE 时,我得到:
username@myServer:/u/aDir/ $ echo $DATE
2457470 for March 23, 2016
但是当我通过昨天在我的 Unix 机器上的日志获取值时,我有:
42447 for March 22,2016
(日志来自我的 ESQL 程序)
问题是我需要使用 rtoday()
值才能将该值导出到平面文件(连同其他数据)以便其他程序进行处理。
我只需要知道如何在 Perl 中或通过系统命令行获取此值以便我可以使用它。
您是否尝试过使用 echo $DATE
作为命令调用 system
?
@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"
或者您可以使用 %ENV
哈希从 Perl 访问环境变量:
$ perl -e 'print $ENV{"DATE"}'
从您所指的 IBM 网站上发布的代码(粘贴在下面)看来,rtoday() returns 今天日期的内部表示 和这个表示需要使用 rdatestr
函数进行格式化。这可能意味着,这种表示完全依赖于实现。
#include <stdio.h>
main()
{
mint errnum;
char today_date[20];
int4 i_date;
printf("RTODAY Sample ESQL Program running.\n\n");
/* Get todays date in the internal format */
rtoday(&i_date);
/* Convert date from internal format into a mm/dd/yyyy string */
if ((errnum = rdatestr(i_date, today_date)) == 0)
printf("\n\tTodays date is %s.\n", today_date);
else
printf("\n\tError %d in converting date to mm/dd/yyyy\n", errnum);
printf("\nRTODAY Sample Program over.\n\n");
}
手册页来自 Informix ESQL/C。
您需要知道 Informix 中的日期是从第 0 天开始的天数,即 1899-12-31 – 所以第 1 天是 1900-01-01。
#include <stdio.h>
#include "sqlhdr.h"
int main(void)
{
int4 date;
rtoday(&date);
printf("%ld\n", date);
return 0;
}
当我在 2016-03-23 运行 这个 (Informix ESQL/C) 程序时,我得到响应 42451。当我测试 SQL:
$ sqlcmd -d stores "select date('2016-03-22') - date('1899-12-31')
> from informix.systables where tabid = 1"
输出为 42450,这与 ESQL/C 程序一致(尽管 sqlcmd
— 来自 IIUG 的程序,不是微软最近发明的同名程序 — 也是ESQL/C 程序)。与问题中显示的值不一致(2016-03-22为42447);我不知道为什么会有差异。仅作记录,我在 RHEL 5 Linux 上为 x86_64 使用 ESQL/C 3.70.FC4(和 Informix 11.70.FC4)——并使用 ESQL/C 4.10.FC5(和Mac OS X 10.11.4 上的 Informix 12.10.FC5)。我不希望在任何其他平台上与 ESQL/C 3.50 (Informix 11.50) 有任何区别。
日期 1970-01-01 使用相同的方案得到序号 25568。因此,您可以将值从 time
转换为正确的值:
$ perl -le 'print time, " ", int(time/(24*60*60))+25568'
1458764790 42451
$ perl -le 'sub informix_date_from_unix_time { return int($_[0]/86400) + 25568; }
> print time, " ", informix_date_from_unix_time(time)'
1458765443 42451
$
这是核心答案,在UTC时区是准确的;您需要仔细考虑日期界限和时区以及您想要的答案。
所以经过一些工作和我正在寻找的值的详细定义,我想到了这个:
#!/opt/standard_perl/perl5881/bin/perl
use Date::Calc qw(:all);
my $date = "03/19/2016";
print getSQLDate($date);
sub getSQLDate
{
my $value = shift;
my @date = split('/', $value);
my $alphaDay = 31;
my $alphaMonth = 12;
my $alphaYear = 1899;
my @alphaDate = ($alphaYear, $alphaMonth,$alphaDay);
my @currentDate = ($date[2], $date[0], $date[1]);
return Delta_Days(@alphaDate, @currentDate);
}
我的输出是:
42447
我是根据我发现的关于我的 SQL 日期的这个定义来做的:
Informix ESQL/C supports the SQL DATE data type with the Informix ESQL/C
date data type for host variables. The date data type stores internal DATE
values. It is implemented as a 4-byte integer whose value is the number of days
since December 31, 1899. Dates before December 31, 1899, are negative numbers,
while dates after December 31, 1899, are positive numbers. For a complete
description of the SQL DATE data type, see the IBM® Informix® Guide to SQL:
Reference.
我目前正在用 Perl 重写一个 Informix ESQL/C 程序。我遇到的问题之一是试图获得与 Perl 中的 rtoday()
完全相同的值。
查看 IBM 的文档后: http://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.esqlc.doc/sii-xbrstod-41016.htm%23sii-xbrstod-41016
"today
A pointer to an int4 value that receives the internal DATE.
Usage
The rtoday() function obtains the system date on the client computer, not
the server computer."
在命令行上执行 echo $DATE 时,我得到:
username@myServer:/u/aDir/ $ echo $DATE
2457470 for March 23, 2016
但是当我通过昨天在我的 Unix 机器上的日志获取值时,我有:
42447 for March 22,2016
(日志来自我的 ESQL 程序)
问题是我需要使用 rtoday()
值才能将该值导出到平面文件(连同其他数据)以便其他程序进行处理。
我只需要知道如何在 Perl 中或通过系统命令行获取此值以便我可以使用它。
您是否尝试过使用 echo $DATE
作为命令调用 system
?
@args = ("command", "arg1", "arg2");
system(@args) == 0
or die "system @args failed: $?"
或者您可以使用 %ENV
哈希从 Perl 访问环境变量:
$ perl -e 'print $ENV{"DATE"}'
从您所指的 IBM 网站上发布的代码(粘贴在下面)看来,rtoday() returns 今天日期的内部表示 和这个表示需要使用 rdatestr
函数进行格式化。这可能意味着,这种表示完全依赖于实现。
#include <stdio.h>
main()
{
mint errnum;
char today_date[20];
int4 i_date;
printf("RTODAY Sample ESQL Program running.\n\n");
/* Get todays date in the internal format */
rtoday(&i_date);
/* Convert date from internal format into a mm/dd/yyyy string */
if ((errnum = rdatestr(i_date, today_date)) == 0)
printf("\n\tTodays date is %s.\n", today_date);
else
printf("\n\tError %d in converting date to mm/dd/yyyy\n", errnum);
printf("\nRTODAY Sample Program over.\n\n");
}
手册页来自 Informix ESQL/C。
您需要知道 Informix 中的日期是从第 0 天开始的天数,即 1899-12-31 – 所以第 1 天是 1900-01-01。
#include <stdio.h>
#include "sqlhdr.h"
int main(void)
{
int4 date;
rtoday(&date);
printf("%ld\n", date);
return 0;
}
当我在 2016-03-23 运行 这个 (Informix ESQL/C) 程序时,我得到响应 42451。当我测试 SQL:
$ sqlcmd -d stores "select date('2016-03-22') - date('1899-12-31')
> from informix.systables where tabid = 1"
输出为 42450,这与 ESQL/C 程序一致(尽管 sqlcmd
— 来自 IIUG 的程序,不是微软最近发明的同名程序 — 也是ESQL/C 程序)。与问题中显示的值不一致(2016-03-22为42447);我不知道为什么会有差异。仅作记录,我在 RHEL 5 Linux 上为 x86_64 使用 ESQL/C 3.70.FC4(和 Informix 11.70.FC4)——并使用 ESQL/C 4.10.FC5(和Mac OS X 10.11.4 上的 Informix 12.10.FC5)。我不希望在任何其他平台上与 ESQL/C 3.50 (Informix 11.50) 有任何区别。
日期 1970-01-01 使用相同的方案得到序号 25568。因此,您可以将值从 time
转换为正确的值:
$ perl -le 'print time, " ", int(time/(24*60*60))+25568'
1458764790 42451
$ perl -le 'sub informix_date_from_unix_time { return int($_[0]/86400) + 25568; }
> print time, " ", informix_date_from_unix_time(time)'
1458765443 42451
$
这是核心答案,在UTC时区是准确的;您需要仔细考虑日期界限和时区以及您想要的答案。
所以经过一些工作和我正在寻找的值的详细定义,我想到了这个:
#!/opt/standard_perl/perl5881/bin/perl
use Date::Calc qw(:all);
my $date = "03/19/2016";
print getSQLDate($date);
sub getSQLDate
{
my $value = shift;
my @date = split('/', $value);
my $alphaDay = 31;
my $alphaMonth = 12;
my $alphaYear = 1899;
my @alphaDate = ($alphaYear, $alphaMonth,$alphaDay);
my @currentDate = ($date[2], $date[0], $date[1]);
return Delta_Days(@alphaDate, @currentDate);
}
我的输出是:
42447
我是根据我发现的关于我的 SQL 日期的这个定义来做的:
Informix ESQL/C supports the SQL DATE data type with the Informix ESQL/C date data type for host variables. The date data type stores internal DATE values. It is implemented as a 4-byte integer whose value is the number of days since December 31, 1899. Dates before December 31, 1899, are negative numbers, while dates after December 31, 1899, are positive numbers. For a complete description of the SQL DATE data type, see the IBM® Informix® Guide to SQL: Reference.