Clock in linux

Can someone post code for clock in linux.
That will display curent time and month and year. How to display only hours and minutes?
LVL 5
JBURGHARDTAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
curriConnect With a Mentor Commented:
Here is the answer. Thanks for giving me the points, jburghardt.

I'm re-posting all code so it gets archived here.

To display everything:
#include <iostream>
#include <time.h>

using namespace std;
                  
int main(void)
{
      time_t t;

      t=time(NULL);

      cout << ctime(&t) << endl;
}

And to display only hours and minutes:
#include <iostream>

#include <time.h>

using namespace std;

int main(void)
{
  time_t t;
  struct tm *pt;

  t=time(NULL);

  pt=localtime(&t);
  cout << pt->tm_hour << ":" << pt->tm_min << endl;

 
}


0
 
curriCommented:
The easiest way to display everything:

#include <iostream>

#include <time.h>

using namespace std;

int main(void)
{
  time_t t;

  t=time(NULL);

  cout << ctime(&t) << endl;
}

(in unix and linux, you can get info about time and ctime by using man ...)

0
 
curriCommented:
This one will display only hours and minutes (again check man pages for localtime):

#include <iostream>

#include <time.h>

using namespace std;

int main(void)
{
  time_t t;
  struct tm *pt;

  t=time(NULL);

  pt=localtime(&t);
  cout << pt->tm_hour << ":" << pt->tm_min << endl;

 
}
0
[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

 
JBURGHARDTAuthor Commented:
Well please post an answer.
0
 
Anju111599Commented:
If all you want is to display the current hour and minute and you don't need to have it inside a program, try the date command with a format specifier:

date +"%H:%M"

You can use that from a shell prompt or from inside a script.  See the date manual page (man date) for details on all the format specifiers.

If you really need C++ code, you can do as curri suggests above.

Anju
0
 
LeimyCommented:
I think he ment post it as an answer so you can get the points!!

Leimy

0
 
alexa_rCommented:
If you want a solution in Perl, here is a simple script,
from which you can extract any info that you want:
---
#!/usr/bin/perl
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime();
# you must add 1 to $mon in order to get a correct # month
$mon++;
print "Time - $mon/$mday/$year - $hour:$min:$sec\n";
---

Using this simple script, you can print anything you want.

check Perl manual pages for extra info.

0
 
JBURGHARDTAuthor Commented:
Well I need c++ so curri shoul post answer
0
 
JBURGHARDTAuthor Commented:
Curri can tell me how to reset screen?
0
All Courses

From novice to tech pro — start learning today.