随手写点之QString to UTC time

原文链接:http://www.cnblogs.com/David-Yip/archive/2012/04/27/2472891.html

  今天遇到一个小小的问题,一个QString型的时间字符串需要转换成UTC时间,最后还要把生成的UTC时间再转换成QString型的一个timetick字符串。

用Qt完成似乎绕来绕去有点繁琐,用python则三言两句就能搞定。还是尝试着用Qt写吧。

  Qt解决此问题思想方法如下:先解析QString类型时间字符串,转换成Datetime型,然后再转成UTC时间。

 1 #include <QLocale>
 2 #include <QString>
 3 #include <QDateTime> 
 4 //const QString for example
 5 QString StrDateTime = "20120217091241";
 6 QLocale Locale;
 7 QDateTime Datetime1,Datetime2;
 8 //convert from QString to QDateTime by function toDateTime in QLocale class 
 9 Datetime1=Locale.toDateTime(StrDateTime,"yyyyMMddhhmmss");
10 //convert from QString to UTC time,timetick has been generated
11 Datetime2 = Datetime1.toUTC();
12 /*转成QString类型,并打印出来*/
13 int IntTimetick;
14 IntTimetick = Datetime2.toTime_t();    //DateTime to int
15 QString str,timetick;
16 timetick = str.number(IntTimetick,10);   //int to String
17 //timetick = "1329441161"(You can have a try.Believe it or not)
18 printf("%s\n",timetick.toStdString().c_str());

转载于:https://www.cnblogs.com/David-Yip/archive/2012/04/27/2472891.html

上一篇:如何将long(currentTimeInMillis)转换为UTC时间戳?


下一篇:linux – 给定时区的小时/分钟的GMT / UTC偏移,无夏令时(DST)