14-9-11/C + + curriculum design--The Library Management department---<time.h> time data type learning record

Source: Internet
Author: User
Tags month name

Small semester C + + curriculum design needs to do a library management system, need to get the current time and time calculation, on-line to find some information self-study, summarized as follows:


1. Get Calendar Time:

The time prototype in C + + is a calendar time (calender times), which represents the number of seconds between the current time and a fixed time.

Defined as follows:

#typedef long time_t;

In other words, the calendar time is actually a long-shaped data. Use the time () function to get the current calendar times,

time_t time (NUL);

The return value is the current calendar time.

2. Time to convert calendar time (time_t) to TM type:

Turn the calendar time into a common year-month-day, time-minutes-seconds to call LocalTime (time_t * timer) to convert time representation to a TM structure, localtime () function, the TM structure is defined as follows:

        struct tm * gmtime (const time_t * Timer);                                                     struct tm  * localtime (Const time_t * timer);          struct tm {               int tm_sec;      /*  seconds  –  value interval is [0,59] */               int tm_min;       /*  points  -  value interval is [0,59] */               int tm_hour;     /*  time  -  value interval is [0,23] */               int tm_mday;      /*  one month date  -  value range is [1,31] */               int tm_mon;      /*  month (starting from January, 0 for January)  -  value range for [0,11] */               int tm_year;     /*  year with a value equal to the actual year minus 1900 */               int tm_wday;     The  /*  week  –  value interval is [0,6], where 0 represents Sunday and 1 for Monday, which pushes  */              int tm_yday;     /*  days starting January 1 of each year   The –  value interval is [0,365], where 0 represents January 1, 1 stands for January 2, and so on  */              int  tm_isdst;    /*  Daylight Saving Time identifier, the TM_ISDST is positive when daylight saving is implemented. Without daylight saving time, TM_ISDST is 0 and TM_ISDST () is negative when the situation is not known. */              };


In addition, you can use the Mktime () function to convert a TM structure data into a t_time long shape, which can be used to complete incomplete portions of the date and output, for example, the following program calculates the day of the week July 1, 1997:

#include "time.h" #include "stdio.h" #include "stdlib.h" int main (void) {struct TM t;      time_t T_of_day;      t.tm_year=1997-1900;      t.tm_mon=6;      T.tm_mday=1;      t.tm_hour=0;      t.tm_min=0;      T.tm_sec=1;      t.tm_isdst=0;      T_of_day=mktime (&t);      printf (CTime (&t_of_day)); return 0; }


Operation Result:

Tue Jul 01 00:00:01 1997


3. Time input and output:

The TM structure cannot be output directly, and the library function in time.h converts the variables of the time_t and TM types into fixed-format strings, respectively:

char * asctime (const struct TM * timeptr); char * CTime (const time_t *timer);

Assuming that T is a non-empty t_time variable, the asctime () and CTime () functions are contacted as follows:

printf (CTime (&t));         Equivalent to: struct TM *ptr;         Ptr=localtime (&t); printf (Asctime (PTR));

4. Custom formatted output time:

Output a custom- formatted time string to the output stream by using the strftime () function, which is prototyped as follows:

size_t strftime (char *strdest, size_t maxsize, const char *format, const struct TM *timeptr);

The function is to timeptr the contents of the time in the TM structure pointed to by the format string, output to strdest, and maxsize indicates the maximum number of characters (to prevent overflow).

Where the format-ideographic characters are as follows in format:

%a shorthand for the day of the week

%A full name of the week

%b of the Month

Full name of the%B month

Time string for the date of%c standard

After two digits of the%c year

The day ordinal of a month in%d decimal notation

%d Month/day/year

%e the day ordinal of a month in a two-character field, in decimal notation

%F year-month-day

%g two digits of the year, using week-based

%G years, using week-based years

%h Abbreviated month name

%H 24-Hour Hour

%I 12-Hour Hour

%j decimal indicates the day ordinal of the year

%m the month represented by decimal

%M minutes in a 10 o'clock-hour representation

%n New Line character

%p equivalent display of the local AM or PM

%r 12 hours of time

%R display hours and minutes: hh:mm

%s number of seconds in decimal

%t Horizontal Tab

%T display time seconds: hh:mm:ss

%u days of the week, Monday for the first day (values from 0 to 6, Monday to 0)

%u year of the week, the Sunday as the first day (value from 0 to 53)

%V Week of the year, using week-based

%w Decimal Day of the week (value from 0 to 6, Sunday is 0)

%W Week of the year, Monday as the first day (value from 0 to 53)

Date string for%x standard

Time string for%x standard

%y decimal Year without century (values from 0 to 99)

%Y decimal year with century part

%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.

Percent hundred percent semicolon

4. Calculate the time span:

You can use the Difftime () function to calculate the span between two times, and its function prototype is as follows:

Double Difftime (time_t end, time_t start);

In fact, he is using two time to subtract the realization.


5. A self-written experimental procedure:

#include  <time.h> #include  <stdio.h>using namespace std;int main () {     time_t tm1;    time (&AMP;TM1);                 //Get calendar Time     tm *tm_s;     char str[40];    char temps[40];     Char temps1[40];    tm_s=localtime (&AMP;TM1);             //Calendar Time converted to TM time     printf ("Local hour is:  %d\n ", tm_s->tm_hour);             // The output struct member     tm_s=gmtime (&AMP;TM1);     printf ("Utc hour is:  %d\n ", tm_s->tm_hour);     printf (Asctime (tm_s));           //Output Fixed CompleteFormat time string// & (Temps[0]) =ctime (&AMP;TM1);    //error Hint:/home/al/desktop/c++/test_time.cpp|22 |error: incompatible types in assignment of  ' char* '  to  ' char [40] ' |    printf (CTime (&AMP;TM1));         // Asctime output is local time, ctime output is GMT                                              //another ctime parameter is a time_t that is a pointer to the calendar time                                               //In addition, if you need to save with a string, you must use a non-array pointer to char (I don't know why.) )                                       //does the name of the character array in C + + no longer represent the first address of the character array?     strftime (str,40, "date:%a     %p %i", tm_s);     //output a custom formatted string     printf ("%s\n", str);}

Operation Result:

[Email protected]:~/desktop/c++$./test_time

Local Hour Is:12

UTC Hour Is:4

Fri Sep 12 04:10:20 2014

Fri Sep 12 12:10:20 2014

Date:friday PM 12

[Email protected]:~/desktop/c++$


This article is from "Al's self-study record" blog, please be sure to keep this source http://al0707.blog.51cto.com/9215022/1551527

14-9-11/C + + curriculum design--The Library Management department---<time.h> time data type learning record

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.