0

I have a wxString that contains hours, minutes, seconds, and milliseconds. I'm trying to create a wxDateTime from this information. I can either leave it in the format of "%H:%M:%S.%f" or I can grab the values individually.

My first thought was to use the wxDateTime constructor that takes in h, m, s, ms, however I can't find how to create a wxDateTime_t anywhere to pass into this.

wxDateTime& wxDateTime(wxDateTime_t hour, wxDateTime_t minute = 0, wxDateTime_t second = 0, wxDateTime_t millisec = 0)

I've also been looking a lot at wxParseTime where I would use the default wxDateTime constructor and then call this, but I don't know how to pass in the time as a const wxChar* correctly to this function.

const wxChar * ParseTime(const wxChar *time)

Can anyone give any insight on either of these ideas or give a better approach to this?

I've been getting my info through this site wxwidgets.

Thanks in advance for any help!

Additional info: wxDateTime::wxDateTime, wxDateTime::wxParseTime.

4

1 回答 1

0

所以事实证明 wxParseTime 不支持毫秒。不过,通过"%H:%M:%S"是有效的。

我还发现了这条信息“wxDateTime_t 类型的类型定义为 unsigned short,用于包含年数、小时数、分钟数、秒数和毫秒数。” 因此可以使用类似这样的方法从 wxStrings 制作未签名的短裤。

unsigned long msec = 0;
token.ToULong(&msec);

这可以针对小时、分钟、秒和毫秒完成,以传递给wxDateTime& wxDateTime(wxDateTime_t hour, wxDateTime_t minute = 0, wxDateTime_t second = 0, wxDateTime_t millisec = 0)构造函数以创建所需的 wxDateTime。

我采用的方法是使用 wxParseTime 更新 wxDateTime 对象,然后设置毫秒。我得到了当前的 wxDateTime,所以年/月/日将被预设。

token = lineTkz.GetNextToken(); // Next token is "%H:%M:%S"
const wxChar* constTime = token.c_str(); // Convert from wxString to wxChar*
wxDateTime dateTime = wxDateTime::Now(); // Get current datetime object
dateTime.ParseTime(constTime); // Update hours, minutes, and seconds from wxChar*
token = lineTkz.GetNextToken(); // Next token is milliseconds as wxString
unsigned long msec = 0; // Create unsigned long
token.ToULong(&msec); // Get value out of wxString and save into unsigned long
dateTime.SetMillisecond(msec);  // Update wxDateTime's milliseconds using set method

您还可以通过bool isValid = dateTime.IsValid();用于错误检查来验证您有一个有效的 wxDateTime。

您可以检查的另一件事是调用 wxParseTime 的返回

const wxChar* bad = dateTime.ParseTime(constTime);

wxParseTime "如果转换失败返回 NULL,否则返回指向停止扫描的字符的指针。"。

希望这对遇到此问题的其他人有所帮助。

于 2013-06-27T16:40:47.193 回答