LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2008, 11:25 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
Get time as a string


I need a C++ function that returns the current time as a string. I put together the below function. It works but I would like to learn how I could do it 'better'! Is there anything wrong with the way that I am doing it? could you guys suggest a better way of doing it?

in header file
Code:
std::string GetTime();
in cpp file
Code:
std::string Test::GetTime()
{
    string sTime;
    char time[10];
    struct timeval tv;
    
    time_t curtime;  
    gettimeofday(&tv, NULL); 
    curtime=tv.tv_sec;
    
    //Format time
    strftime(time,10,"%T",localtime(&curtime));
    sTime = time;
 
    return sTime;
}
 
Old 07-31-2008, 12:30 PM   #2
cmnorton
Member
 
Registered: Feb 2005
Distribution: Ubuntu, CentOS
Posts: 585

Rep: Reputation: 35
Alternative Answer

Take a spin through STD or RTL, and see what is already available. I have to believe there is already a class available from which you can derive the time format you want.
 
Old 07-31-2008, 01:14 PM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Maybe
Code:
std::string Test::GetTime()
{
    time_t t;
    time ( &t );
    return std::string( asctime ( localtime ( &t ) ) );
}
 
Old 08-01-2008, 01:34 AM   #4
rogx
LQ Newbie
 
Registered: Jan 2005
Location: Roma, Italy
Distribution: slackware, slamd64
Posts: 23

Rep: Reputation: 15
This is plain C, but I think You can use it in Your C++ program:

Code:
// gcc -Wall -o mytime mytime.c

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

int main()
  {
  struct tm *Btime;
  time_t mytime1;

  char mydata[30];
  char tmpstr[30];

  mytime1=time(NULL);
  Btime=localtime(&mytime1);

  sprintf(tmpstr,"%4d", (Btime->tm_year+1900)); strcpy(mydata,tmpstr); strcat(mydata,"/");
  sprintf(tmpstr,"%02d",(Btime->tm_mon+1));     strcat(mydata,tmpstr); strcat(mydata,"/");
  sprintf(tmpstr,"%02d", Btime->tm_mday);       strcat(mydata,tmpstr); strcat(mydata,"  ");

  sprintf(tmpstr,"%02d", Btime->tm_hour);       strcat(mydata,tmpstr); strcat(mydata,":");
  sprintf(tmpstr,"%02d", Btime->tm_min);        strcat(mydata,tmpstr); strcat(mydata,":");
  sprintf(tmpstr,"%02d", Btime->tm_sec);        strcat(mydata,tmpstr);

  printf("\ntime: %s\n\n", mydata);
  
  return(0);
  }
Output:
Code:
~$ gcc -Wall -o mytime mytime.c
~$ ./mytime

time: 2008/08/01  08:36:48

~$
With this type of code You can format the date/time string as You prefer.

Bye, Rogx.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Convert Time String to DATETIME field Centinul Programming 2 09-25-2006 07:20 PM
Converting time from string to long format Deepak Inbasekaran Programming 6 07-21-2006 05:00 PM
convert date time format given in string suchi_s Programming 6 01-10-2005 04:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration