xlsxnumformatparser.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /****************************************************************************
  2. ** Copyright (c) 2013-2014 Debao Zhang <hello@debao.me>
  3. ** All right reserved.
  4. **
  5. ** Permission is hereby granted, free of charge, to any person obtaining
  6. ** a copy of this software and associated documentation files (the
  7. ** "Software"), to deal in the Software without restriction, including
  8. ** without limitation the rights to use, copy, modify, merge, publish,
  9. ** distribute, sublicense, and/or sell copies of the Software, and to
  10. ** permit persons to whom the Software is furnished to do so, subject to
  11. ** the following conditions:
  12. **
  13. ** The above copyright notice and this permission notice shall be
  14. ** included in all copies or substantial portions of the Software.
  15. **
  16. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. ** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. ** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. ** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. **
  24. ****************************************************************************/
  25. #include "xlsxnumformatparser_p.h"
  26. #include <QString>
  27. namespace QXlsx {
  28. bool NumFormatParser::isDateTime(const QString &formatCode)
  29. {
  30. for (int i = 0; i < formatCode.length(); ++i) {
  31. const QChar &c = formatCode[i];
  32. switch (c.unicode()) {
  33. case '[':
  34. // [h], [m], [s] are valid format for time
  35. if (i < formatCode.length()-2 && formatCode[i+2] == QLatin1Char(']')) {
  36. const QChar cc = formatCode[i+1].toLower();
  37. if (cc == QLatin1Char('h') || cc == QLatin1Char('m') || cc == QLatin1Char('s'))
  38. return true;
  39. i+=2;
  40. break;
  41. } else {
  42. // condition or color: don't care, ignore
  43. while (i < formatCode.length() && formatCode[i] != QLatin1Char(']'))
  44. ++i;
  45. break;
  46. }
  47. // quoted plain text block: don't care, ignore
  48. case '"':
  49. while (i < formatCode.length()-1 && formatCode[++i] != QLatin1Char('"'))
  50. ;
  51. break;
  52. // escaped char: don't care, ignore
  53. case '\\':
  54. if (i < formatCode.length() - 1)
  55. ++i;
  56. break;
  57. // date/time can only be positive number,
  58. // so only the first section of the format make sense.
  59. case ';':
  60. return false;
  61. break;
  62. // days
  63. case 'D':
  64. case 'd':
  65. // years
  66. case 'Y':
  67. case 'y':
  68. // hours
  69. case 'H':
  70. case 'h':
  71. // seconds
  72. case 'S':
  73. case 's':
  74. // minutes or months, depending on context
  75. case 'M':
  76. case 'm':
  77. return true;
  78. default:
  79. break;
  80. }
  81. }
  82. return false;
  83. }
  84. } // namespace QXlsx