0

I would like to spit a date/time string (for example 1/08/1957/11:20:01, or any type of time format) into month, hour, second, minute. The problem is that firstly I don't know how to define a type of time that can be split.

Should I write:

time_t now=1081987112001 s; //it is not correct. why? what's the c++ time data format?
struct tm* tm = localtime(&now);

cout << "Today is "
     << tm->tm_mon+1 // month
     << "/" << tm->tm_mday // day
     << "/" << tm->tm_year + 1900 // year with century
     << " " << tm->tm_hour // hour
     << ":" << tm->tm_min // minute
     << ":" << tm->tm_sec; // second

But it's not correct. Can someone give me an example with a method that takes a time value given from the keyboard and splits it?

What are the types of data times formats that c++ can accept?

cpx
  • 17,009
  • 20
  • 87
  • 142
marryy
  • 215
  • 1
  • 6
  • 13

3 Answers3

3

If you looking to take the time from a user-input (which is what it seems like you're wanting), and convert that into a valid struct tm, you can use strptime() found in time.h.

So for instance, if you had:

char user_input_time[] = "25 May 2011 10:15:45";

struct tm;
strptime(user_input_time, "%d %b %Y %H:%M:%S", &tm);

printf("year: %d; month: %d; day: %d;\n", tm.tm_year, tm.tm_mon, tm.tm_mday);
Jason
  • 31,834
  • 7
  • 59
  • 78
  • strptime() isn't in the standard library. See http://stackoverflow.com/questions/321849/strptime-equivalent-on-windows – Ian Goldby May 25 '11 at 15:04
  • No, unfortunately it's not ... it's a POSIX function. But thank you for the link to a more portable version of that function ... that should definitely be helpful. – Jason May 25 '11 at 18:11
1

time_t is an integer number and counts the number of seconds elapsed since the UNIX epoch: 1st Jan 1970, 00:00:00. You definitively cannot write what you did to assign this value.

You have to use the functions localtime or gmtime to convert between a convenient time_t value and a struct tm that have the various info of day, month, hour, etc.

You can also use the strftime and strptime functions to convert between a character string and struct tm.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
0
#include <cstdio>
#include <iostream>
#include <string>
...
{
  std::string date_time;
  std::getline(std::cin, date_time);
  sscanf(date_time.c_str(), "%d/%d/%d/%d:%d:%d", &month, &date, &year, &hour, &minute, &second);
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • this is the only way that a person can send the data and time? what if he sends yyyy.month.day.hour? – marryy May 25 '11 at 14:58
  • :) so i guess there has to be something that can take any value as a data/time value if it's correct and after that it splits it into hours, seconds and so on,no? – marryy May 25 '11 at 15:01
  • in c# the method is System.DataTime, in obj c its NSDate .in c++> – marryy May 25 '11 at 15:01
  • This (or better, `strptime`) is one way to parse a date if you know or specify the input format. If you don't know the input format in advance, you need to use a 3rd-party library. Google "c++ parse date" to look for one. – Robᵩ May 25 '11 at 15:04