Windows/C + + Gets the current system time __c++

Source: Internet
Author: User
Tags filetime function prototype month name
Windows CC get current system time scheme a localtime advantage only use C standard library disadvantage can only be accurate to the second level scenario two Getlocaltime SYS benefits can be accurate to millisecond disadvantage using Windows API Scheme three SYSTEMTIME solution four time Null scheme five CTime how to convert filetime time to string in C filetime SYSTEMTIME St char strTime128 sprintfstrtimed-d-d Ystwhourstwminutestwsecond How to convert filetime time to regular time format in C Windows/C + + Gets the current system time

"Original" http://blog.csdn.NET/dadalan/article/details/5771693

Write software often use to get the system time to display to the status bar, here on the basis of predecessors summed up a few scenarios. programme I: localtime () Advantages: Only use C standard library; Disadvantage: only accurate to the second level

time_t is a type defined in Time.h that represents a calendar time, which is the number of seconds from January 1, 1970 0:0 0 seconds to this time, and the prototype is:
typedef long time_t; /* Time value */
It can be seen that time_t is actually a long integer, because the long integer can represent a limited number, so it can represent the latest time January 18, 2038 19:14 07 seconds.

Function time can get the current calendar time, and the definition of times:
time_t Time (time_t *)

time_t(typedef __int64 time_t)Just a long integer, does not conform to our usage, need to convert cost time, will use the TM structure, TIME.H Structure TM prototype is:
[CPP] View Plain Copy struct tm {           int tm_sec;      /* seconds after the minute - [0,59] */            int tm_min;     /*  minutes after the hour - [0,59] */            int tm_hour;    /* hours since midnight -  [0,23] */           int tm_mday;     /* day of the month - [1,31] */            int tm_mon;     /* months since january  - [0,11] */           int tm_year;     /* years since 1900 */            int tm_wday;    /* days since sunday - [0,6] */            int tm_yday;    /* days  since January 1 - [0,365] */            int tm_isdst;   /* daylight savings time flag */           };  

As you can see, this organization defines the year, month, day, hour, minute, second, week, Day of the year, daylight saving time. You can use this structure to easily display the time.

Gets the current system time with localtime, which converts a time_t time to the time represented by the TM structure, the function prototype:
struct TM * localtime (const time_t *)
Using the Gmtime function to get GMT, function prototype:
struct TM * GMTIME (const time_t *) output mode 1:[CPP] View plain copy #include  <iostream>   #include  <time.h>   using  namespace std;   Void dsptime (const struct tm *);  //output time.       int main (void)    {    time_t nowtime;    nowtime = time (NULL);  //get calendar Time     cout << nowtime < < endl;  //Output nowtime       struct tm *local,*gm;     local=localtime (&nowtime)   //get current system time     dsptime (local);      gm=gmtime (&nowtime)   //get GMT     dsptime (GM);          return 0;  }   void dsptime (const struct tm &NBSP;*&NBSP;PTM)    {    char *pxq[]={"Day", "one", "two", "three", "four", "five", "six"};    cout << ptm->tm_year+1900 <<  "Year"  << ptm->tm_mon+1 <<  "month"  << ptm->tm_mday <<  "Day  "  ;    cout <<  ptm->tm_hour <<  ":  << ptm->tm_min << ": "  << ptm->tm_sec << " "  ;    cout <<  "  Week"  <<pxq[ptm->tm_wday] <<  "  The first of the year"  << ptm->tm_yday  <<  "Day  "  << endl;  }  

Output Mode 2: [CPP] view plain copy #include <time.h> #include <stdio.h> int main (void) {        time_t t = time (0);        Char tmp[64];        Strftime (TMP, sizeof (TMP), "%y/%m/%d%x%A%j Days of the Year",%z (localtime));        Puts (TMP);    return 0; }  

C + + provides a custom time format function strftime in time.h, function prototype:
 size_t strftime (char *strdest, size_t maxsize, const char * format, const struct TM *timeptr);
Parameter Description:
 char *strdest: Used to hold the formatted string cache,
 size_t maxsize: Specify the maximum number of characters to output,
 const char * Format: Formats the string,
 const struct TM *timeptr: the time to convert.

The format string that you can use:
%a Day of the week
%A the full name of the week
%b of the Month
Full name of%B month
Time series for the%c standard date
Rear two digits of%c year
The first day of the month in%d decimal notation
%d Month/day/year
%e in a two-character field, the first day of the month in decimal notation
%F year-month-day
Two digits of%g year, using week-based years
%G years, using weeks based
%h Abbreviated month name
%H 24-hour system hours
%I 12-hour hours
%j decimal representation of the first day of the year
Month in%m decimal notation
Number of minutes represented by%m 10 o'clock
%n New Line character
%p the equivalent display of a local AM or PM
%r 12 hours.
%R display hours and minutes: hh:mm
%s decimal number of seconds
%t Horizontal Tab
%T when displayed: Hh:mm:ss
%u days of the week, Monday is the first day (values from 0 to 6, Monday to 0)
%u week of the year, make Sunday the first day (values from 0 to 53)
%V the first weeks of the year, using a week based year
%w decimal representation of the week (values from 0 to 6, Sunday to 0)
%w Week of the year, Monday is the first day (value from 0 to 53)
The date string of the%x standard
%x Standard Time series
%y decimal Year without century (values from 0 to 99)
%Y decimal year with the century part
%z,%z the time zone name and returns a null character if the time zone name cannot be obtained.
Percent percent%


Programme II: Getlocaltime (&sys); Advantages: Can be accurate to the millisecond level; disadvantage: Using Windows API
[CPP] view plain copy #include <windows.h> #include <stdio.h> int main (void) {SYSTEMTI    ME SYS;    Getlocaltime (&sys); printf ("%4d/%02d/%02d%02d:%02d:%02d.%0    3d Week%1d/n ", Sys.wyear,sys.wmonth,sys.wday,sys.whour,sys.wminute, Sys.wsecond,sys.wmilliseconds,sys.wdayofweek);   return 0; }  
Programme III: System ("time");Advantages: Use system function, also can modify system time

[CPP] view Plain copy//This file must be a C + + file #include <stdlib.h> #include <iostream> using namespace std;   void Main () {System ("time"); }  
Scenario Four: Time (null)Convert the current time to a second level, and then convert it by the corresponding time
[CPP] view Plain copy//This file must be a C + + file #include <iostream> #include <ctime> using namespace std;   int main () {time_t now_time;   Now_time = time (NULL);   cout<<now_time;   return 0; }  
Programme V: CTimeUse MFC inside the CTime class, more convenient
The first: [CPP] view plain copy CString str; Obtain the system time CTime TM;         Tm=ctime::getcurrenttime (); Str=tm.  Format ("%y-%m-%d%h:%m:%s");   Mainly is the Y m d,h m S middle of the connector of its own definition MessageBox (STR,NULL,MB_OK);

The second type: [CPP] view plain copy SYSTEMTIME St;         CString Strdate,strtime;         Getlocaltime (&ST);         Strdate.format ("%4d-%2d-%2d", st.wyear,st.wmonth,st.wday); Strtime.format ("%2d:%2d:%2d", St.whour,st.wminute,st.wsecond);

Comments:

How to convert filetime time into strings in C + +.

First filetime into SystemTime

Again

SYSTEMTIME St; Char strtime[128]; sprintf (Strtime, "%d-%d-%d%d:%d:%d", St.wyear,st.wmonth,st.wday,st.whour,st.wminute,st.wsecond);


Reference:

How to convert filetime time to regular time format in C + +. http://bbs.csdn.net/topics/310015002

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.