C++ Tutorial/Data Types/time

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Convert now to tm struct for local timezone

<source lang="cpp">#include <iostream>

  1. include <ctime>
  2. include <cstdlib>

using namespace std; int main( ) {

 // Current date/time based on current system
 time_t now = time(0);
 // Convert now to tm struct for local timezone
 tm* localtm = localtime(&now);
 cout << "The local date and time is: " << asctime(localtm) << endl;

}</source>

The local date and time is: Mon Apr 23 13:17:32 2007

Convert now to tm struct for UTC

<source lang="cpp">#include <iostream>

  1. include <ctime>
  2. include <cstdlib>

using namespace std; int main( ) {

 // Current date/time based on current system
 time_t now = time(0);
  // Convert now to tm struct for UTC
 tm* gmtm = gmtime(&now);
 if (gmtm != NULL) {
    cout << "The UTC date and time is: " << asctime(gmtm) << endl;
 }
 else {
   cerr << "Failed to get the UTC date and time" << endl;
   return EXIT_FAILURE;
 }

}</source>

The UTC date and time is: Mon Apr 23 20:17:33 2007

%c specifies the standard time and date pattern

<source lang="cpp">#include <iostream>

  1. include <locale>
  2. include <cstring>
  3. include <ctime>

using namespace std; int main() {

 // Obtain the current system time.
 time_t t = time(NULL);
 tm *cur_time = localtime(&t);
 // Create US and German locales.
 locale usloc("English_US");
 locale gloc("German_Germany");
 // %c specifies the standard time and date pattern.
 char *std_pat = "%c";
 char *std_pat_end = std_pat + strlen(std_pat);
 return 0;

}</source>

custom pattern displays hours and minutes followed by the date

<source lang="cpp">#include <iostream>

  1. include <locale>
  2. include <cstring>
  3. include <ctime>

using namespace std; int main() {

 // Obtain the current system time.
 time_t t = time(NULL);
 tm *cur_time = localtime(&t);
 // Create US and German locales.
 locale usloc("English_US");
 locale gloc("German_Germany");
 // custom pattern displays hours and minutes followed by the date.
 char *custom_pat = "%A %B %d, %Y %H:%M";
 char *custom_pat_end = custom_pat + strlen(custom_pat);
 return 0;

}</source>

Demonstrating the use of the ctime( ) function.

<source lang="cpp">#include <time.h>

  1. include <iostream>

using namespace std; time_t longtime; int main( ) {

time(&longtime);
cout << "The time and date are " <<
        ctime(&longtime) << "\n";
return (0);

}</source>

Demonstrating the use of the gmtime( ) and asctime( ) functions.

<source lang="cpp">#include <time.h>

  1. include <stdio.h>

int main( ) {

struct tm *date_time;
time_t timer;
time(&timer);
date_time=gmtime(&timer);
printf("%.19s\n",asctime(date_time));
return (0);

}</source>

Demonstrating the use of the localtime( ) and asctime( ) functions.

<source lang="cpp">#include <time.h>

  1. include <stdio.h>

struct tm *date_time; time_t timer; int main( ) {

time(&timer);
date_time=localtime(&timer);
printf("The present date and time is: %s\n",
asctime(date_time));
return (0);

}</source>

Displays the current date and time

<source lang="cpp">#include <iostream> using std::cout;

  1. include <ctime>

using std::time_t; using std::time; using std::localtime; using std::asctime; int main() {

  time_t currentTime;
  
  time( &currentTime );
  
  cout << asctime( localtime( &currentTime ) );
  return 0;

}</source>

Sun Apr 29 10:56:45 2007

Set the locale to Germany and get the time_put facet for Germany

<source lang="cpp">#include <iostream>

  1. include <locale>
  2. include <cstring>
  3. include <ctime>

using namespace std; int main() {

 // Obtain the current system time.
 time_t t = time(NULL);
 tm *cur_time = localtime(&t);
 // Create US and German locales.
 locale usloc("English_US");
 locale gloc("German_Germany");
 // Set the locale to Germany and get the time_put facet for Germany.
 cout.imbue(gloc);
 const time_put<char> &g_time = use_facet<time_put<char> >(cout.getloc());
 return 0;

}</source>

Set the locale to Germany, Show standard time and date string

<source lang="cpp">#include <iostream>

  1. include <ctime>
  2. include <clocale>

using namespace std; int main() {

 char str[64];
 // Set the locale to Germany.
 setlocale(LC_ALL, "German_Germany");
 // Get the current system time.
 time_t t = time(NULL);
 // Show standard time and date string.
 strftime(str, 64, "%c", localtime(&t));
 cout << "Standard format: " << str << endl;
 // Show a custom time and date string.
 strftime(str, 64, "%A, %B %Y %I:%M %p", localtime(&t));
 cout << "Custom format: " << str << endl;
 return 0;

}</source>

Set the locale to US and get the time_put facet for US

<source lang="cpp">#include <iostream>

  1. include <locale>
  2. include <cstring>
  3. include <ctime>

using namespace std; int main() {

 // Obtain the current system time.
 time_t t = time(NULL);
 tm *cur_time = localtime(&t);
 // Create US and German locales.
 locale usloc("English_US");
 locale gloc("German_Germany");
 // Set the locale to US and get the time_put facet for US.
 cout.imbue(usloc);
 const time_put<char> &us_time = use_facet<time_put<char> >(cout.getloc());
 return 0;

}</source>

Show a custom time and date string

<source lang="cpp">#include <iostream>

  1. include <ctime>

using namespace std; int main() {

 char str[64];
 // Get the current system time.
 time_t t = time(NULL);
 // Show a custom time and date string.
 strftime(str, 64, "%A, %B %d %Y %I:%M %p", localtime(&t));
 cout << "Custom format: " << str << endl;
 return 0;

}</source>

Show standard time and date string

<source lang="cpp">#include <iostream>

  1. include <ctime>

using namespace std; int main() {

 char str[64];
 // Get the current system time.
 time_t t = time(NULL);
 // Show standard time and date string.
 strftime(str, 64, "%c", localtime(&t));
 cout << "Standard format: " << str << endl;
 return 0;

}</source>

The strftime( ) Format Specifiers

<source lang="cpp">Command Replaced By %a Abbreviated weekday name %A Full weekday name %b Abbreviated month name %B Full month name %c Standard date and time string %d Day of month as a decimal (1-31) %H Hour (0-23) %I Hour (1-12) %j Day of year as a decimal (1-366) %m Month as decimal (1-12) %M Minute as decimal (0-59) %p Locale"s equivalent of AM or PM %S Second as decimal (0-61) %U Week of year, Sunday being first day (0-53) %w Weekday as a decimal (0-6, Sunday being 0) %W Week of year, Monday being first day (0-53) %x Standard date string %X Standard time string %y Year in decimal without century (0-99) %Y Year including century as decimal %Z Time zone name %% The percent sign</source>

use of the strftime( ) function.

<source lang="cpp">#include <time.h>

  1. include <stdio.h>

int main( ) {

struct tm *date_time;
time_t timer;
char str[80];
time(&timer);
date_time=localtime(&timer);
strftime(str,80,"It is %X on %A, %x",
         date_time);
printf("%s\n",str);
return (0);

}</source>