Example 1:
Let’s follow the below example to see the functionality of the strptime function in C language. Here, we have printed the year with the strptime function. As we are in the year “2022”, the strptime function will be output this year. Inside the program file of C, we have input the header file “time.h” through which we can get the date and time information. After that, we established the function in the main() of the program.
Next, we have created the buffer for the allocation of space for new data. The buffer is labeled as the “result” and the data type assigned to the buffer is char. We have set the size of the buffer “result” with the value “100”. Then, we constructed the variable “TIME” with the data type “time_t” which stores the system time value in the C language. The “TIME” variable is initialized with the “time(NULL)” system call. With this time system call, the current time will be retrieved.
After that, we employed the function strptime which takes the “result”, “sizeof” function, and the localtime function as arguments. The localtime function inputs the reference of the “TIME” and is used to represent the present time of our system. Furthermore, we have used the modifier “%Y” in the strptime function, which is used to display the year in the century. In the last line of the program, we have a printf statement where we have used the %s. The printf function’s %s format specifier allows us to print a string. The user received a message that included the current year.
#include <time.h>
int main(int argc, const char * argv[])
{
char result[100];
time_t TIME;
TIME = time(NULL);
strftime(result, sizeof(result), "%Y", localtime(&TIME));
printf("We are in year %s !\n", result);
return 0;
}
When we have executed the strptime program, the current year “2022” is obtained in the system shell.
Example 2:
We have seen the performance of the strptime function in the preceding example program. As we have discussed earlier, the strptime function supports various modifiers in the C language. In this example, we have tried to use some of the strptime modifiers to explore their functionality. Here, we have used the #define directive to add the _XOPEN_SOURCE definition in the header section of the following program. It is used to input the extra definitions of functions that exist in the standards X/Open and POSIX.
Next, we have also added some required modules of C standards for the program implementation. After that, we constructed the main function where we established the structure “tm” for defining the time. We have declared the object “tm” for the “struct tm”. Then, we set the size of the buffer to “100” for the strptime function. We have deployed the memset function, which is known as the memory setter. As the name suggests, it is used to fill the blocks of memory with semantic value.
Then, we invoked the strptime function where we gave the data and time format with the strptime modifiers. The “%Y” modifier is used to indicate the year. “%m-%d” is used to represent the month and day. The “%H:%M:%S” modifier is formatted in a way that will provide the time in hours, minutes, and seconds. Moreover, we have utilized another function, strftime, to differentiate from the strptime function.
The strftime function created the characters from the time object. It takes the argument as a buffer, the size of buffer, and some modifiers. Then puts function holds the “buffer”. The specified format of date and time will be displayed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int
main(void)
{
struct tm tm;
char buffer[100];
memset(&tm, 0, sizeof(struct tm));
strptime("2022-10-9 12:25:05", "%Y-%m-%d %H:%M:%S", &tm);
strftime(buffer, sizeof(buffer), "%d %b %Y %H:%M", &tm);
puts(buffer);
exit(EXIT_SUCCESS);
}
By executing the program, we have obtained the data and time in the specified format given to the function.
Example 3:
Here, we have displayed the date and time of the US region by using the strptime function. We have inserted standard libraries into the following program. Next, we deployed the main function where we set the buffer size. After that, we have defined the object “TIME” with the data type “time_t”. Next, we have established the structure “tm” which declares the pointer object “timeptr” and “outcome”. Then, we call the setlocale function where we set the current locale. The “TIME” object is set with the time system call, which takes NULL as a value.
To the timeptr, we have assigned the localetime function, which is referenced by the “TIME”. The strftime function is employed, which is assigned the formatted date and time. Then, we have an if-else condition where the if condition takes the strptime function with the arguments. When the specified time and date inside the strptime function is NULL, then the printf statement will execute. Otherwise, we have another section to be executed. The else portion has various printf statements which utilize the different modifiers for the date and time.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <locale.h>
int main(void)
{
char buf[100];
time_t TIME;
struct tm *timeptr,Outcome;
setlocale(LC_ALL,"/QSYS.LIB/EN_US.LOCALE");
TIME = time(NULL);
timeptr = localtime(&TIME);
strftime(buf,sizeof(buf), "%a %m/%d/%Y %r", timeptr);
if (strptime(buf, "%a %m/%d/%Y %r",&Outcome) == NULL)
printf("\nstrptime failed\n");
else
{
printf("time in hour: %d\n",Outcome.tm_hour);
printf("time in min: %d\n",Outcome.tm_min);
printf("time in sec: %d\n",Outcome.tm_sec);
printf("time in mon: %d\n",Outcome.tm_mon);
printf("time in month and day: %d\n",Outcome.tm_mday);
printf("time in year: %d\n",Outcome.tm_year);
printf("time in year and day: %d\n",Outcome.tm_yday);
printf("time in week and day: %d\n",Outcome.tm_wday);
}
return 0;
}
The following converted data and time is executed against each modifier of the strptime function.
Conclusion
This guide is for the Linux strptime function in the C programming language. We have explained the functionality of this particular function with the example codes. We have implemented the above and all the codes are executable with the Ubuntu 22.04 terminal. Moreover, we have seen the difference between the two functions, strptime and strftime, which are somehow identical. The strptime function creates a time output from text, input and the strftime function gives the string data from the time input.