C/Development/Time Date — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 14:20, 25 мая 2010

converts earth days into Jovian years

#include <stdio.h>
int main(void)
{
  float earth_days; /* number of earth days */
  float jovian_years; /* equivalent number of Jovian years */
  printf("Enter number of earth days: ");
  scanf("%f", &earth_days);
  jovian_years = earth_days / (365.0 * 12.0);
  printf("Equivalent Jovian years: %f", jovian_years);
  return 0;
}


Convert time_t value to string: how to use ctime

#include <stdio.h>
#include <time.h>
int main () {
  time_t rawtime;
  time ( &rawtime );
  printf ( "Current date and time are: %s", ctime (&rawtime) );
  
  return 0;
}


Convert time_t value to tm structure as UTC time: how to use gmtime

#include <stdio.h>
#include <time.h>
#define PST (-8)
#define CET (1)
#define BJ (8)
int main ()
{
  time_t rawtime;
  struct tm *p;
  time ( &rawtime );
  p = gmtime ( &rawtime );
  printf ("Time in Los Angeles: %2d:%02d\n", p->tm_hour+PST, p->tm_min);
  printf ("Time in Berlin:      %2d:%02d\n", p->tm_hour+CET, p->tm_min);
  printf ("Time in Bei Jing:   %2d:%02d\n", p->tm_hour+BJ, p->tm_min);
  
  return 0;
}


Convert time_t value to tm structure in local time: How to use localtime

#include <stdio.h>
#include <time.h>
int main ()
{
  time_t rawtime;
  struct tm *tminfo;
  time ( &rawtime );
  tminfo = localtime ( &rawtime );
  printf ( "Current date and time are: %s", asctime (tminfo) );
  
  return 0;
}


Convert tm structure to string: how to use asctime

#include <stdio.h>
#include <time.h>
int main ()
{
  time_t rawtime;
  struct tm *timeinfo;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current date and time are: %s", asctime (timeinfo) );
  
  return 0;
}


Convert tm structure to time_t value: how to use mktime

#include <stdio.h>
#include <time.h>
int main ()
{
  time_t rawtime;
  struct tm *timeinfo;
  int year = 2005, month = 2 ,day = 2;
  
  char * weekday[] = { "Sunday", "Monday",
                       "Tuesday", "Wednesday",
                       "Thursday", "Friday", "Saturday"};
  
  /* get current timeinfo and modify it to user"s choice */
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_year = year - 1900;
  timeinfo->tm_mon = month - 1;
  timeinfo->tm_mday = day;
  /* call mktime: timeinfo->tm_wday will be set */
  mktime ( timeinfo );
  printf ("That day is a %s.\n", weekday[timeinfo->tm_wday]);
  
  return 0;
}


Create local time from time()

#include <time.h>
#include <stdio.h>
int main(void)
{
  struct tm *ptr;
  time_t lt;
  char str[80];
  lt = time(NULL);
  ptr = localtime(&lt);
  strftime(str, 100, "It is now %H %p.", ptr);
  printf(str);
  return 0;
}


Display a date

#include <stdio.h>
void main()
{
  int month = 0;
  int day = 0;
  int year = 0;
  printf("Enter the date as three integer values separated by spaces (month day year):\n");
  scanf("%d", &month);
  scanf("%d", &day);
  scanf("%d", &year);
  if(day>3 && day<21 || day>23 && day<31)
    printf("\n%dth ",day);
  else
    printf("\n%d%s ", day, (day%10 == 1 ? "st": (day%10 == 2 ? "nd" : "rd")));
  switch(month)
  {
    case 1:
      printf("January ");
      break;
    case 2:
      printf("February ");
      break;
    case 3:
      printf("March ");
      break;
    case 4:
      printf("April ");
      break;
    case 5:
      printf("May");
      break;
    case 6:
      printf("June");
      break;
    case 7:
      printf("July");
      break;
    case 8:
      printf("August");
      break;
    case 9:
      printf("September");
      break;
    case 10:
      printf("October");
      break;
    case 11:
      printf("November");
      break;
    case 12:
      printf("December");
      break;
  }
  printf(" %d\n", year);
}


Get current time: how to use time and time_t

#include <stdio.h>
#include <time.h>
int main ()
{
  time_t seconds;
  seconds = time (NULL);
  printf ("%ld hours since January 1, 1970", seconds/3600);
  
  return 0;
}


Get the elapsed time: clock and CLOCKS_PER_SEC

#include <time.h>
#include <stdio.h>
int main(void)
{
  printf("Elapsed time: %u secs.\n", clock()/CLOCKS_PER_SEC);

  return 0;
}


Get time: how to use time() and time_t

#include <time.h>
#include <stdio.h>
int main(void)
{
  time_t lt;
  lt = time(NULL);
  printf(ctime(&lt));
  return 0;
}


Get time: now

#include <stdio.h>                     /* For input and output             */
#include <time.h>                      /* For time() and clock() functions */
void main() {
   
   long time_taken = clock();    
    
   printf("%ld\n", time_taken);
   printf("%d\n", CLOCKS_PER_SEC);
   
   
 }


Getting date data with ease

#include <stdio.h>
#include <time.h>
void main()
{
  char *Day[7] = {
                   "Sunday"  , "Monday", "Tuesday", "Wednesday",
                   "Thursday", "Friday", "Saturday"
                 };
  char *Month[12] = {
                     "January",   "February", "March",    "April",
                     "May",       "June",     "July",     "August",
                     "September", "October",  "November", "December"
                    };
  char *Suffix[] = { "st", "nd", "rd", "th" };
  int i = 3;                                
  struct tm *OurT = NULL;                   
  time_t Tval = 0;
  Tval = time(NULL);
  OurT = localtime(&Tval);
  switch( OurT->tm_mday )
  {
    case 1: case 21: case 31:
      i= 0;                  /* Select "st" */
      break;
    case 2: case 22:
      i = 1;                 /* Select "nd" */
      break;
    case 3: case 23:
      i = 2;                 /* Select "rd" */
      break;
    default:
      i = 3;                 /* Select "th" */
      break;
  }
  printf("\nToday is %s the %d%s %s %d", Day[OurT->tm_wday],
      OurT->tm_mday, Suffix[i], Month[OurT->tm_mon], 1900 + OurT->tm_year);
  printf("\nThe time is %d : %d : %d",
                                      OurT->tm_hour, OurT->tm_min, OurT->tm_sec );
}


Getting the time as a string

#include <stdio.h>
#include <time.h>
#include <string.h>
#define TIME_24   1
#define TIME_12   0
char *get_time_string(int mode24);     /* Returns the time as a string in 12 or 24 hour format */
int main()
{
  char time_str[12];
  get_time_string(TIME_24);
  printf("\n\n");
  get_time_string(TIME_12);
}
char* get_time_string(int mode24)
{
  static char time_str[12] = {0};   /* Stroes the time as a string */
  struct tm *now = NULL;
  int hour = 0;
  time_t time_value = 0;
  time_value = time(NULL);          /* Get time value              */
  now = localtime(&time_value);     /* Get time and date structure */
  hour = now->tm_hour;              /* Save the hour value         */
  if(!mode24 && hour>12)            /* Adjust if 12 hour format    */
    hour -= 12;
  printf("%d:",hour);
  printf("%d:",now->tm_min);
  printf("%d",now->tm_sec);

  if(!mode24)
    if(now->tm_hour>24){
       printf("PM\n");
    }else{
       printf("AM\n");
     }
  return time_str;
}


How to check the performance: difftime

#include <time.h>
#include <stdio.h>
int main(void)
{
  time_t start,end;
  volatile long unsigned t;
  start = time(NULL);
  for(t = 0; t < 5000000; t++) 
      ;
  end = time(NULL);
  
  printf("Loop used %f seconds.\n", difftime(end, start));
  return 0;
}


How to get local time: localtime and asctime

#include <time.h>
#include <stdio.h>
int main(void)
{
  struct tm *ptr;
  time_t lt;
  lt = time(NULL);
  ptr = localtime(&lt);
  printf(asctime(ptr));
  return 0;
}


Local time and Coordinated Universal Time and date

#include <time.h>
#include <stdio.h>
/* Print local and UTC time. */
int main(void)
{
  struct tm *local, *gm;
  time_t t;
  t = time(NULL);
  
  local = localtime(&t);
  
  printf("Local time and date: %s\n", asctime(local));
  
  gm = gmtime(&t);
  
  printf("Coordinated Universal Time and date: %s", asctime(gm));
  return 0;
}


Make time: hour, minute, second, year, day, month

#include <time.h>
#include <stdio.h>
int main(void)
{
  struct tm t;
  time_t t_of_day;
  t.tm_year = 2005-1900;
  t.tm_mon = 0;
  t.tm_mday = 3;
  t.tm_hour = 0;  /* hour, min, sec don"t matter */
  t.tm_min = 0;   /* as long as they don"t cause a */
  t.tm_sec = 1;   /* new day to occur */
  t.tm_isdst = 0;
  t_of_day = mktime(&t);
  printf(ctime(&t_of_day));
  return 0;
}


Print local and UTC time

#include <time.h>
#include <stdio.h>

int main(void)
{
  struct tm *local;
  time_t t;
  t = time(NULL);
  local = localtime(&t);
  printf("Local time and date: %s\n", asctime(local));
  local = gmtime(&t);
  printf("UTC time and date: %s\n", asctime(local));
  return 0;
}


Return difference between two times: how to use difftime

#include <stdio.h>
#include <time.h>
int main ()
{
  time_t start, end;
  char str[256];
  double dif;
  time (&start);
  printf ("Please, enter your name: ");
  gets (str);
  time (&end);
  
  dif = difftime (end, start);
  
  printf ("You have taken %.2lf seconds to type your name.\n", dif );
 
  return 0;
}


Return number of clock ticks since process start

#include <stdio.h>
#include <time.h>
void wait ( int seconds ) {
  clock_t endwait;
  endwait = clock () + seconds * CLK_TCK ;
  
  while (clock() < endwait) {
  }
}
int main ()
{
  int i;
  printf ("Starting count down...\n");
  
  for (i = 20; i > 0; i--) {
    printf (" i = %d\n",i);
    wait (1);
  }
  
  printf ("Done!!!\n");
  
  return 0;
}