0

I have a requirement in , which I have to read a file which consists date and time and convert it to unsigned __int64.

For example I have a file "mytime.txt" with following data.

2012-06-25 05:32:06.963

How do I convert it to unsigned __int64, and how do I convert back from unsigned __int64 to the above string to write to file and also to verify it is converted to unsigned __int64 correctly.

I am working on windows in VS.NET C++ compiler.

I am not supposed to use boost.

venkysmarty
  • 11,099
  • 25
  • 101
  • 184
  • The standard C library doesn't handle resolutions under one second, so you have to resort to platform specific functions. As for parsing the string, use either one of the [`scanf`](http://en.cppreference.com/w/cpp/io/c/fscanf) functions, [regular expressions](http://en.cppreference.com/w/cpp/regex), or extract directly using something like [`std::string::substr`](http://en.cppreference.com/w/cpp/string/basic_string/substr). – Some programmer dude Jun 26 '12 at 09:29
  • Have a look at [this question](http://stackoverflow.com/questions/3404393/date-time-parsing-in-c-any-format-string-to-epoch) - yours is a subset of it. – Aleks G Jun 26 '12 at 09:33
  • @AleksG The poster says he is not supposed to use Boost, so the answer in that question is not applicable. – Some programmer dude Jun 26 '12 at 09:35
  • @JoachimPileborg Oops, my bad: missed that line. – Aleks G Jun 26 '12 at 09:45
  • "atoi" would produce `2012`, which is a valid `__int64`. But presumably you wanted a different result. Which? – MSalters Jun 26 '12 at 11:02

1 Answers1

1

you can use strptime to convert your string into struct and than use it to convert to time_t and do reverse using gmtime

struct tm tm;
time_t epoch;
if ( strptime(timestamp, "%Y-%m-%d %H:%M:%S", &tm) != NULL )
  epoch = mktime(&tm);

// Reverse 
  struct tm * ptm;

  time ( &epoch );

  ptm = gmtime ( &epoch );
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88