I am trying to input a time in a epoch format from the command line and I want to store it in a struct timespec variable.
I am able to store it and somehow print it but when I add something to the timespec variable it is giving strange things
This is the code
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main (int argc, char *argv[]){
struct timespec InputTime;
InputTime.tv_sec = (time_t)argv[1]; //(__time_t)
InputTime.tv_nsec = (long)argv[2]; //(__syscall_slong_t)
printf("The time before %s,%s\n",
InputTime.tv_sec,
InputTime.tv_nsec);
InputTime.tv_sec += 3;
InputTime.tv_nsec += 3;
printf("The time after %s,%s\n",
InputTime.tv_sec,
InputTime.tv_nsec);
return 0;
}
This are the inputs and the outputs
INPUT
./InputTime 1615578864 438734073
OUTPUT
The time before 140733952311809,140733952311820
The time after 140733952311812,140733952311823
I have tried to use other variable types for *argv[] and try to use %s for the output of print but then the time before gives me the right input but the time after gives me something strange here is the example of output when I change %ld to %s. Additionally when i do this the compiler gives some warnings because I am not using the right format
./InputTime 1615578864 438734073
The time before 1615578864,438734073
The time after 5578864,734073
I am using this gcc version
gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.