Cpp - Process time string returned from ctime()

Introduction

The function time() returns the current time as the number of seconds since 1/1/1970, 0:0.

The number of seconds is stored in the variable sec, whose address was supplied as &sec when the function was called.

The function ctime() converts the number of seconds to a string with a date and time and returns this string.

The string comprises exactly 26 characters including the null character \0.

Demo

#include <iostream> 
#include <string> 
#include <ctime>         // For time(), ctime(), ... 
using namespace std;
int main()/*from ww w.ja v a  2  s  . c om*/
{
  time_t sec;
  time(&sec); // Reads the present time in seconds into sec. 
  string tm = ctime(&sec);// Converts the seconds to a string. 

  string hr(tm, 11, 2);  // Substring of tm starting at 
               // position 11, 2 characters long. 
  string greeting("hi ");

  if (hr < "10")             // Compares strings 
    greeting += "Morning!";
  else if (hr < "17")
    greeting += "Day!";
  else
    greeting += "Evening!";

  cout << greeting << endl;

  return 0;
}

Result

Related Topic