Menu

milliseconds...

2019-12-16
2019-12-22
  • E.Naumovich

    E.Naumovich - 2019-12-16

    I found that there are big difficulties to store time with millisecnds precision. time_t dont store ms at all, when I tried doubles , sometimes values were rounded even to few minutes.
    I have proposition to make kind of hack: if string started from some unprintable char, like 0x01 or 0x21, string can be added as raw value, without quotation. In this case it may be easy to add cell preformated date-time like 1990-12-12 12:12:12.075 or double with desired precision.

     
  • Alexandr Belyak

    Alexandr Belyak - 2019-12-17

    Thanks for the info. I'll think about it. It may be sufficient to modify the CellDataTime class and there will be no need for hacking :-)

     
  • Alexandr Belyak

    Alexandr Belyak - 2019-12-18

    I modified the library, a new version in the attachment. I will be glad to any feedback from you.
    Please see this example.

    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <vector>
    
    #include <Xlsx/Workbook.h>
    
    using namespace SimpleXlsx;
    
    int main()
    {
        setlocale( LC_ALL, "" );
        time_t CurTime = time( NULL );
    
        CWorkbook book( "Incognito" );
        std::vector<ColumnWidth> ColWidth;
        ColWidth.push_back( ColumnWidth( 0, 0, 25 ) );
        CWorksheet & Sheet = book.AddSheet( "Date and time", ColWidth );
    
        Style style;
        style.numFormat.numberStyle = NUMSTYLE_DATETIME;
        size_t DateTimeStyleIndex = book.AddStyle( style );
        Sheet.BeginRow().AddCell( CellDataTime( CurTime, DateTimeStyleIndex ) ).EndRow();
    
        style.numFormat.formatString = "yyyy-mm-dd hh:mm:ss.000";
        size_t CustomDateTimeStyleIndex = book.AddStyle( style );
        Sheet.BeginRow().AddCell( CellDataTime( 1990, 12, 12, 12, 12, 12, 75, CustomDateTimeStyleIndex ) ).EndRow();
    
        if( book.Save( "Test_ms.xlsx" ) ) std::cout << "The book has been saved successfully" << std::endl;
        else std::cout << "The book saving has been failed" << std::endl;
        return 0;
    }
    
     
  • E.Naumovich

    E.Naumovich - 2019-12-19

    Looks like it works.
    But I have proposition: to add also call of CellDataTime with paramter, compatible with WinApi SYSTEMTIME. SYSTEMTIME is defined as

    typedef struct _SYSTEMTIME {
      WORD wYear;
      WORD wMonth;
      WORD wDayOfWeek;
      WORD wDay;
      WORD wHour;
      WORD wMinute;
      WORD wSecond;
      WORD wMilliseconds;
    } SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
    

    WORD is uint16_t.
    Such call looks more short and convinuent, at least for Windows users .

     
  • Alexandr Belyak

    Alexandr Belyak - 2019-12-19

    Your proposition is good, I added such functionality.
    I also added the ability to use the QDateTime class from the Qt framework.

     
  • Alexandr Belyak

    Alexandr Belyak - 2019-12-19

    The attachment has an updated example.
    Please pay attention to the generated xlsx-file in the section "Direct date and time".
    If the time specified is accurate to milliseconds, and the style is selected without milliseconds, then Excel rounds the milliseconds to the nearest second.
    I think this can be confusing for users.

     
  • E.Naumovich

    E.Naumovich - 2019-12-20

    Thanks!
    Looks like SYSTEMTIME work as well!

     
  • Alexandr Belyak

    Alexandr Belyak - 2019-12-22

    Thank you for your ideas and testing :-)

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.