date format dd.mm.yyyy in C

荒凉一梦 提交于 2019-12-25 04:09:04

问题


I want to know if there is a way to read date from console in format dd.mm.yyyy in C. I have a structure with information for the date. I tried with another structure just for the date with day, month and year in it:

typedef struct
{
    int day;
    int month;
    int year;
} Date;

but the dots are a problem. Any idea?


回答1:


Try:

  Date d;
  if (scanf("%d.%d.%d", &d.day, &d.month, &d.year) != 3)
    error();



回答2:


You can use strptime() to read in an arbitrary formatted date string into a struct tm.

#define _XOPEN_SOURCE /* glibc2 needs this to have strptime(). */
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <errno.h>

...

Date d = {0};
char * fmt = "%d.%m.%Y";
char s[32] = "";
char fmt_scanf[32] = "";
int n = 0;

sprintf(fmt_scanf, "%%%ds", sizeof(s) - 1); /* Created format string for scanf(). */

errno = 0;    
if (1 == (n = scanf(fmt_scanf, s)))
{
  struct tm t = {0};
  char * p = strptime(s, fmt, &t);
  if ((s + strlen(s)) != p)
  {
    fprintf(stderr, "invalid date: '%s'\n", s);
  }
  else
  {
    d.day = t.tm_mday;
    d.month = t.tm_mon + 1; /* tm_mon it zero-based. */
    d.year = t.tm_year + 1900; /* tm_year is years since 1900. */ 
  }
}
else
{
  perror("scanf()");
}

Update:

The positive side effects and addtional gains of going this way are:

  • No input validation is necessary, as it's all done by strptime().
  • Changing the input format is trivial: Just let fmt point to a different format string.



回答3:


Let's use a function defined in just for the purpose: strftime()! (Thank you tutorialpoints.com for giving details on the standard libraries of C)

What does it do? It allows us to create a string with as much of date and/or time as we want, howsoever we want it to have them, and also other characters to the string if needed! For an example, we can create a "20191011.log" string for instance, if we wanted to say make a filename for today's log.

Here is the code required for just this:

#include<stdio.h>//printf
#include<time.h>//localtime,time,strftime


/*Here, the log file's name will be created*/
int main()  
{
    char filename[13];
    //Obtaining time
    time_t raw;
    struct tm *obtained_time;

    time(&raw);
    obtained_time = localtime (&raw);


    //Obtaining string_format out of generated time
    int success_filename;

    success_filename = strftime(filename,sizeof(filename),"%Y%m%d.log",obtained_time);//yyyymmdd.log
    if (success_filename != 0)
    {
            printf("%s",filename);
    }
    obtained_time = NULL;

    return 0;
}

`

The third argument of strftime is where you can make your recipe of strings, and there are a whole lot of options, such as abbreviated or full names of days and months, time, seconds, hours, minutes, AM/PM designation etc. To explore them more, go to the following link: Tutorialspoint on strftime() function

Do let me know if it helps you out!



来源:https://stackoverflow.com/questions/16761668/date-format-dd-mm-yyyy-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!