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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
JBURGHARDTAuthor Commented:
Well please post an answer.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

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
curriCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JBURGHARDTAuthor Commented:
Curri can tell me how to reset screen?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.