Convert time string to SYSTEMTIME

Discuss and announce Total Commander plugins, addons and other useful tools here, both their usage and their development.

Moderators: white, Hacker, petermad, Stefan2

Post Reply
User avatar
tbeu
Power Member
Power Member
Posts: 1336
Joined: 2003-07-04, 07:52 UTC
Location: Germany
Contact:

Convert time string to SYSTEMTIME

Post by *tbeu »

I have got a time string in special format pattern

Code: Select all

"ddd MMM dd HH:mm:ss yyyy"
. Is there a simple way to convert it back to SYSTEMTIME?
TC plugins: Autodesk 3ds Max / Inventor / Revit Preview, FileInDir, ImageMetaData (JPG Comment/EXIF/IPTC/XMP), MATLAB MAT-file Viewer, Mover, SetFolderDate, Solid Edge Preview, Zip2Zero and more
User avatar
SanskritFritz
Power Member
Power Member
Posts: 3693
Joined: 2003-07-24, 09:25 UTC
Location: Budapest, Hungary

Post by *SanskritFritz »

Delphi? C++? PL/SQL? Python?
:twisted:
I switched to Linux, bye and thanks for all the fish!
User avatar
TLis
Member
Member
Posts: 111
Joined: 2004-06-02, 16:48 UTC
Location: Szczecin, Poland

Post by *TLis »

Any language could be used, that supports regular expressions. Personally I would use Python, which I like best :)

If I understand you correctly, that ddd are the weekday shortcuts (Mon, Tue, etc.), and MMM are month name shortcuts (Jan, Feb, etc.), then you could use the proper alternatives in the pattern (e.g. (Mon|Tue|...) )to make the matching more precise. You could use groupings to get the parts of the matched string correspondings to the date elements. Then, before you use the time module functions, you need to translate the month name shortcuts into the month number. As soon, as you get the date, it is possible to translate it back into a string using the strftime function
User avatar
tbeu
Power Member
Power Member
Posts: 1336
Joined: 2003-07-04, 07:52 UTC
Location: Germany
Contact:

Post by *tbeu »

C/C++ would be my prefered language.
TC plugins: Autodesk 3ds Max / Inventor / Revit Preview, FileInDir, ImageMetaData (JPG Comment/EXIF/IPTC/XMP), MATLAB MAT-file Viewer, Mover, SetFolderDate, Solid Edge Preview, Zip2Zero and more
User avatar
HolgerK
Power Member
Power Member
Posts: 5406
Joined: 2006-01-26, 22:15 UTC
Location: Europe, Aachen

Post by *HolgerK »

Code: Select all

static const char* Days[]	= { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char* Months[]	= { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

SYSTEMTIME DateString2SYSTEMTIME( char* pstrDate )
{
	SYSTEMTIME systime;
	memset(&systime,0,sizeof(systime));
	char strDay[10];
	char strMonth[10];
	if( 7 == sscanf
			( pstrDate
			, "%3s %3s %d %d:%d:%d %d"
			, strDay
			, strMonth
			, &systime.wDay
			, &systime.wHour
			, &systime.wMinute
			, &systime.wSecond
			, &systime.wYear
			)
		)
	{
		WORD i;
		for( i= 0; i< 7; i++ )
			if( 0 == strcmpi( strDay, Days[i] ) )
				systime.wDayOfWeek	= i;
		for( i= 0; i< 12; i++ )
			if( 0 == strcmpi( strMonth, Months[i] ) )
				systime.wMonth		= WORD(i+1);

		// Time zone correction
		FILETIME ft;
		SystemTimeToFileTime( &systime,  &ft );
		LocalFileTimeToFileTime( &ft, &ft );
		FileTimeToSystemTime(   &ft, &systime );
	}
	return systime;
}
HTH,
Holger
User avatar
tbeu
Power Member
Power Member
Posts: 1336
Joined: 2003-07-04, 07:52 UTC
Location: Germany
Contact:

Post by *tbeu »

Thank you, Holger!
TC plugins: Autodesk 3ds Max / Inventor / Revit Preview, FileInDir, ImageMetaData (JPG Comment/EXIF/IPTC/XMP), MATLAB MAT-file Viewer, Mover, SetFolderDate, Solid Edge Preview, Zip2Zero and more
Post Reply