trying to convert year,month,day etc into int time number. Having some problems with code.

Hello everyone.

Here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <sstream>
#include <string>
#include <locale>
#include <ctime>
#include <iomanip>
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{

	std::wstring input = L"2011-Februar-18 23:12:34";
	std::tm t;
	std::wistringstream ss(input);
	ss.imbue(std::locale("de_DE"));
	ss >> std::get_time(&t, L"%Y-%b-%d %H:%M:%S"); // uses std::time_get<wchar_t>
	std::cout << std::asctime(&t);
	return 0;
}


Got it from here:
http://en.cppreference.com/w/cpp/locale/time_get

The problem is that it crashes.
C++ visual studio 2015 says followings on debug mode:

Unhandled exception at 0x74AC4B32 in testing.exe: Microsoft C++ exception: std::runtime_error at memory location 0x00ADF8D8.


Debugger points at the line:
 
ss.imbue(std::locale("de_DE"));



Is there another way to do what I want or what am i doing wrong here
besides fully copy-pasteing code from example?

Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <sstream>
#include <string>
#include <locale>
#include <ctime>
#include <iomanip>

int main()
{
    std::wstring input = L"2011-Februar-18 23:12:34";
    
    
    std::wistringstream ss( input );
    ss.imbue( std::locale( "German_germany" ) ); // locale names are implementation specific
    
    std::tm t {}; // (value initialise) zero initialise
    ss >> std::get_time( &t, L"%Y-%b-%d %H:%M:%S" ); // uses std::time_get<wchar_t>

    std::cout << std::asctime( &t );
}

http://rextester.com/IZAXD49418
It's working now, thank you!

I was trying to do the opposite of that:
http://www.cplusplus.com/forum/general/181835/

But it appears to do something else.
Timezone conversion.

How could I convert that string back to a one whole number?
String containing date.
Thanks!
Assuming that both the server and the client use the same std::time_t - this is very likely; in almost all implementations std::time_t is an integer type holding the Unix time (seconds since 00:00:00, 1 Jan 1970 UTC, not counting leap seconds):
https://en.wikipedia.org/wiki/Unix_time

Use std::gmtime() to convert std::time_t to UTC calender time
http://en.cppreference.com/w/cpp/chrono/c/gmtime

Use std::localtime() to convert std::time_t to calender time in the local time zone
http://en.cppreference.com/w/cpp/chrono/c/localtime

Both fill up the struct std::tm. The members of the struct hold the various components making up the calender time.
http://en.cppreference.com/w/cpp/chrono/c/tm

To convert between time zones, use UTC as the base
(time zones are typically expressed as UTC + HH:MM or UTC - HH:MM).
For an example see: http://www.cplusplus.com/forum/general/186395/#msg908780
Thank you!

This is what i came up with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <sstream>
#include <string>
#include <locale>
#include <ctime>
#include <iomanip>

int main()
{
	using namespace std;
	time_t today = time(NULL);
	tm tm_today = {};
	tm_today = *localtime(&today);
	cout << "local: " << put_time(&tm_today, "%c %Z") << '\n';
	cout << "Time number:" << today << endl;
	time_t back_to_time_t = mktime(&tm_today);
	cout << "Time number:" << back_to_time_t << endl;
}

http://rextester.com/GHSC57151

One thing i do not like is:
 
tm_today = *localtime(&today);


Why does it return the pointer?
Anyway around it? Another function?

Right now my code creates memory leak cause i don't delete the allocated memory what
localtime creates.

I wish to have to function what doesn't allocate it so that i have to delete it afterwards?
Last edited on
> Right now my code creates memory leak cause i don't delete the allocated memory that localtime creates.

Your code is fine; std::localtime() does not allocate dynamic memory.

Return value: pointer to a static internal std::tm object on success, or NULL otherwise. The structure may be shared between std::gmtime, std::localtime, and std::ctime, and may be overwritten on each invocation.
http://en.cppreference.com/w/cpp/chrono/c/localtime
Last edited on
Okay this is something new to me.
I always thought that if something returns a pointer, i have to deallocate it cause
well, when else it is going to get deallocated.

Forget all about static/local variables.
Thank you again so much!
Last edited on
Topic archived. No new replies allowed.