-1

How can I create structure to have timestamp inside? I would like to create array of structures and each time one element of array is printed, I would like to have timestamp(which I would like to implement inside structure St) and string.

struct St{
char Aps[65];
};

struct S1 Arr[20];
001
  • 13,291
  • 5
  • 35
  • 66
gagaaa
  • 7
  • 2
  • [Time stamp in the C programming language](https://stackoverflow.com/q/1444428) – 001 Aug 31 '21 at 19:36
  • 1
    What format do you want the timestamp to be in? – Barmar Aug 31 '21 at 19:51
  • 2
    Depends on the resolution that you're looking for. If one second resolution is good enough, and you don't care about time stamps after the year 2100, then a simple unsigned 32-bit number will work, and you can get the time stamp by calling the `time` function. – user3386109 Aug 31 '21 at 19:54
  • I am also having same problem...but I want to use it as NAT table destroying timer with in table structure and format will be in seconds – Jarvis__-_-__ Aug 31 '21 at 19:58
  • Consider making an [ISO8601](https://en.wikipedia.org/wiki/ISO_8601) time-stamp, because standards! Unfortunately, the language predates the standard, so this is non-trivial in C. – Neil Sep 01 '21 at 01:08
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 03 '21 at 08:35

1 Answers1

1

This all depends on the format and resolution the timestamp needs to be in, as pointed out in comments.

To construct a general approach, looking at the date and time utilities of C is useful. If this does not fit your requirements, then you'd have to look for a third party library. Either way, the basic structure of your program will most likely be the same.

Here is a basic example in C11 (with the exception of the sleep function). It prints the structs, sets the timestamps and sleeps. Afterwards it prints the stored timestamps.

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

#define ARRAY_SIZE 20
#define STRUCT_STRING_SIZE 65

struct St
{
    time_t timestamp;
    char aps[STRUCT_STRING_SIZE];
};

void nanosleepWrapper(void);
time_t timeWrapper(void);

int main(void)
{
    struct St arr[ARRAY_SIZE];

    // Fill structs array with placeholder data
    for (size_t idx = 0U; idx < ARRAY_SIZE; ++idx)
        snprintf(arr[idx].aps, STRUCT_STRING_SIZE, "Placeholder value %zu", idx);

    // Loop: print struct data, set timestamp, and sleep
    for (size_t idx = 0U; idx < ARRAY_SIZE; ++idx)
    {
        printf("%s\n", arr[idx].aps);
        fflush(stdout);

        nanosleepWrapper();
        arr[idx].timestamp = timeWrapper();
    }

    // Print all timestamps
    for (size_t idx = 0U; idx < ARRAY_SIZE; ++idx)
        printf("%zu: %s", idx, asctime(gmtime(&arr[idx].timestamp)));
}

// Sleep using a function which is not standard C, but is POSIX
void nanosleepWrapper(void)
{
    struct timespec req = {0, 500000000};
    if (nanosleep(&req, NULL) == -1)
        fprintf(stderr, "Sleep failed\n");
}

time_t timeWrapper(void)
{
    time_t tmp = time(NULL);
    if (tmp == (time_t)(-1))
    {
        fprintf(stderr, "Setting timestamp failed\n");
        exit(EXIT_FAILURE);
    }
    return tmp;
}

Example output:

Placeholder value 0
Placeholder value 1
Placeholder value 2
...
Placeholder value 18
Placeholder value 19
0: Wed Sep  1 11:18:04 2021
1: Wed Sep  1 11:18:05 2021
2: Wed Sep  1 11:18:05 2021
...
18: Wed Sep  1 11:18:13 2021
19: Wed Sep  1 11:18:14 2021
Yun
  • 3,056
  • 6
  • 9
  • 28