在AGE中实现一个日期函数

huangapple go评论59阅读模式
英文:

Implementing a Date Function in AGE

问题

我正在尝试在AGE中实现date()函数,但C语言没有内置的date函数可供使用。我正在考虑利用age_timestamp()函数并将其输出转换为日期格式。然而,我一直没有找到这个转换的具体算法。

Datum age_date(PG_FUNCTION_ARGS) {
      agtype time = age_timestamp()
      
      // 程序逻辑
}

在AGE中是否有任何替代方法可以提供所需的日期功能?

英文:

I am trying to implement date() function in AGE, but C doesn't have an in-build date function that I can use. I am considering utilizing the age_timestamp() function and converting its output to date format. However, I have been unable to find the specific algorithm for this conversion.

Datum age_date(PG_FUNCTION_ARGS) {
      agtype time = age_timestamp()
      
      // Program Logic
}

Are there any alternative methods that can provide the desired date functionality in AGE.

答案1

得分: 2

你可以利用PostgreSQL内置的localtime函数来实现这个功能。

实际上,date()函数已经在AGE(GDBMS_cypher)中实现了。以下是其实现方式:

Datum date(PG_FUNCTION_ARGS)
{
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    char s[11];
    sprintf(s, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
    PG_RETURN_TEXT_P(cstring_to_text(s));
}

你可以修改这段代码来返回agtype。

Datum age_date(PG_FUNCTION_ARGS)
{
    agtype_value agtv;
    agtype *agt;

    time_t t = time(NULL);
    struct tm *tm = localtime(&t);
    char s[11];
    sprintf(s, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);

    agtv.type = AGTV_STRING;
    agtv.val.string.len = check_string_length(strlen(s));
    agtv.val.string.val = s;
    agt = agtype_value_to_agtype(&agtv);

    return AGTYPE_P_GET_DATUM(agt);
}

希望能够起作用。

英文:

You can leverage PostgreSQL's built-in localtime function to achieve this functionality.

Actually date() function is already implemented in AGE (GDBMS_cypher). Here is how it was implemented:

Datum date(PG_FUNCTION_ARGS)
{
	time_t t = time(NULL);
	struct tm *tm = localtime(&t);
	char s[11];
	sprintf(s, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
	PG_RETURN_TEXT_P(cstring_to_text(s));
} 

You can change this code to return an agtype.

Datum age_date(PG_FUNCTION_ARGS)
{
    agtype_value agtv;
    agtype *agt;

	time_t t = time(NULL);
	struct tm *tm = localtime(&t);
	char s[11];
	sprintf(s, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
	
    agtv.type = AGTV_STRING;
    agtv.val.string.len = check_string_length(strlen(s));
    agtv.val.string.val = s;
    agt = agtype_value_to_agtype(&agtv);

    return AGTYPE_P_GET_DATUM(agt);
}

Hope it works.

答案2

得分: 1

你可以使用 time.h 中的 localtime() 函数。

#include <time.h>

Datum age_date(PG_FUNCTION_ARGS) {
    time_t t = time(NULL);
    struct tm *tm = localtime(&t);

    char date_string[11];
    sprintf(date_string, "%04d-%02d-%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);

    PG_RETURN_TEXT_P(cstring_to_text(date_string));
}
英文:

You can use the localtime() function from time.h

#include &lt;time.h&gt;

Datum age_date(PG_FUNCTION_ARGS) {
    time_t t = time(NULL);
    struct tm *tm = localtime(&amp;t);

    char date_string[11];
    sprintf(date_string, &quot;%04d-%02d-%02d&quot;, tm-&gt;tm_year + 1900, tm-&gt;tm_mon + 1, tm-&gt;tm_mday);

    PG_RETURN_TEXT_P(cstring_to_text(date_string));
}

答案3

得分: 0

你可以使用 time.h 中提供的 localtime 函数来获取本地日期和时间。

#include <stdio.h>
#include <time.h>

int main() {
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    printf("YYYY-MM-DD HH:MM:SS = %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}

希望这对你有帮助。

英文:

You can find local date and time using localtime which is available in time.h.

#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

int main() {
    time_t t = time(NULL);
    struct tm tm = *localtime(&amp;t);
    printf(&quot;YYYY-MM-DD HH:MM:SS = %d-%02d-%02d %02d:%02d:%02d\n&quot;, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
}

答案4

得分: 0

"Since date() function already implemented in AGE. We can built this another way using help of #include "utils/datetime.h". which is-:

<!-- begin snippet: js hide: false console: true babel: false -->

#include &quot;utils/datetime.h&quot;

PG_FUNCTION_INFO_V1(age_date);

Datum age_date(PG_FUNCTION_ARGS) {
    int64 timestamp = GetCurrentTimestamp();

    /* Convert the timestamp to a DateADT */
    DateADT date = timestamp / USECS_PER_DAY + POSTGRES_EPOCH_JDATE;

    PG_RETURN_DATEADT(date);
}

In here GetCurrentTimestamp() function is available from utils/datetime.h header. USECS_PER_DAY and POSTGRES_EPOCH_JDATE both are constant defined in the PostgreSQL source code that represents the number of microseconds in a day and Julian date corresponding to the PostgreSQL epoch.

英文:

Since date() function already implemented in AGE. We can built this another way using help of #include "utils/datetime.h". which is-:

<!-- begin snippet: js hide: false console: true babel: false -->

#include &quot;utils/datetime.h&quot;

PG_FUNCTION_INFO_V1(age_date);

Datum age_date(PG_FUNCTION_ARGS) {
    int64 timestamp = GetCurrentTimestamp();

    /* Convert the timestamp to a DateADT */
    DateADT date = timestamp / USECS_PER_DAY + POSTGRES_EPOCH_JDATE;

    PG_RETURN_DATEADT(date);
}

In here GetCurrentTimestamp() function is available from utils/datetime.h header. USECS_PER_DAY and POSTGRES_EPOCH_JDATE both are constant defined in the PostgreSQL source code that represents the number of microseconds in a day and Julian date corresponding to the PostgreSQL epoch.

答案5

得分: -2

time.h 中有一个函数 localtime()。以下是一个示例:

#include <stdio.h>
#include <time.h>

int main() {
    time_t currTime;
    time(&currTime);

    struct tm* localTime = localtime(&currTime);

    int year = localTime->tm_year + 1900;
    int month = localTime->tm_mon + 1;
    int day = localTime->tm_mday;

    printf("Local Time: %d-%02d-%02d\n", year, month, day);

    return 0;
}
英文:

time.h has a function localtime(). Here is an example:

#include &lt;stdio.h&gt;
#include &lt;time.h&gt;

int main() {
    time_t currTime;
    time(&amp;currTime);

    struct tm* localTime = localtime(&amp;currTime);

    int year = localTime-&gt;tm_year + 1900;
    int month = localTime-&gt;tm_mon + 1;
    int day = localTime-&gt;tm_mday;

    printf(&quot;Local Time: %d-%02d-%02d\n&quot;, year, month, day);

    return 0;
}

huangapple
  • 本文由 发表于 2023年5月29日 22:21:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定