1

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.

5 Answers5

2

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.

Mohayu Din
  • 433
  • 9
1

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

#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));
}
Humza Tareen
  • 146
  • 6
0

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

#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);
}
abhishek2046
  • 312
  • 1
  • 11
0

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

#include "utils/datetime.h"

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.

MAHMUDUL
  • 1
  • 2
-2

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

#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;
}