The basic method of reading time date in C language _c language

Source: Internet
Author: User
Tags time and date

C language Time () function: Gets the current times (in seconds)
header file:

#include <time.h>

To define a function:

time_t time (time_t *t);

Function Description: This function returns the number of seconds elapsed from 0:0 UTC, January 1, 1970, from 0 seconds to the current time. If T is not a null pointer, this function also saves the return value to the memory referred to by the T pointer.

Return value: Success returns the number of seconds, Failure returns ((time_t)-1) value, the reason for the error is in errno.

Example

#include <time.h>
Main () {
  int seconds = time ((time_t*) NULL);
  printf ("%d\n", seconds);
}

Execution results:

9.73E+08

C language Gmtime () function: Get current time and date
header file:

#include <time.h>

To define a function:

struct TM *gmtime (const time_t *TIMEP);

Function Description: gmtime () Converts the information in the time_t structure of the parameter TIMEP to the time-date representation used in the real world, and then returns the result from the structure TM.

The structure TM is defined as

struct tm{
  int tm_sec//representing the current number of seconds, normal range is 0-59, but allowed to 61 sec
  int tm_min;//represents current score, range 0-59
  int tm_hour;//number of hours from midnight, range is 0-23
  int Tm_mday///Current month, range 01-31
  int tm_mon;//representing the current month, from January onwards, range from 0-11
  int tm_year;//number
  of years since 1900 int tm_wday; The number of days in a week, starting from Monday, the range is 0-6
  int tm_yday//The number of days since January 1 of this year, the range is 0-365
  int tm_isdst;//Daylight saving time flag
};

The date returned by this function is not time zone conversion, but UTC time.

Return value: Returns the structure TM representing the current UTC time.

Example

#include <time.h>
Main () {
  char *wday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  time_t TIMEP;
  struct TM *p;
  Time (&TIMEP);
  p = gmtime (&TIMEP);
  printf ("%d%d%d", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
  printf ("%s%d;%d;%d\n", Wday[p->tm_wday], P->tm_hour, P->tm_min, p->tm_sec);

Execution results:

2000/10/28 Sat 8:15:38

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.