midrange.com code scratchpad
Name:
HSSF Date / Time / Timestamp routines
Scriptlanguage:
Plain Text
Tabwidth:
4
Date:
01/07/2012 12:11:50 am
IP:
Logged
Description:
RPG code for implementation of HSSF (Excel) date, time and timestamp values
Code:
  1.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2.       *  hssf_date():  Shortcut for inserting a new cell that contains
  3.       *        a date value into a given row of a sheet
  4.       *
  5.       *    This is just a wrapper around the hssf_date2xls() and
  6.       *    hssf_num() routines.  (Dates in Excel are simply double
  7.       *    precision floating point numbers)
  8.       *
  9.       *    peRow = Row object that cell should be created in
  10.       *    peCol = column number of new cell
  11.       * peNumber = numeric value to place in cell
  12.       *  peStyle = cell style object to associate with cell
  13.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14.      P hssf_date       B                   Export
  15.      D hssf_date       PI
  16.      D   peRow                             like(HSSFRow)
  17.      D   peCol                        5I 0 value
  18.      D   peDate                        D   value
  19.      D   peStyle                           like(HSSFCellStyle)
  20.  
  21.      D wwDate          s                   like(jDouble)
  22.  
  23.       /free
  24.          If peRow = *NULL ;
  25.             sendEscape('CPF9898'
  26.                      : 'hssf_date() cannot deal with *NULL row') ;
  27.          EndIF ;
  28.          wwDate = hssf_date2xls(peDate);
  29.          hssf_num(peRow: peCol: wwDate: peStyle);
  30.       /end-free
  31.      P hssf_date       E
  32.  
  33.  
  34.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  35.       *  hssf_date2xls():
  36.       *    service program utility to convert an RPG date to a
  37.       *    number that can be formatted as a date in Excel
  38.       *
  39.       *    peDate = RPG date to convert
  40.       *
  41.       *  returns the date formatted for Excel
  42.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  43.      P hssf_date2xls   B                   Export
  44.      D hssf_date2xls   PI                  like(jdouble)
  45.      D   peDate                        D   value
  46.  
  47.      D wwStrDate       s               d   inz(d'1900-01-01')
  48.      D wwDays          s                   like(jdouble)
  49.  
  50.       ** Dates in Excel are simply double-precision floating point
  51.       ** numbers that represent the number of days since Jan 1, 1900
  52.       ** with a few quirks:
  53.       **     1)  Jan 1st 1900 is considered day #1, not 0.
  54.       **     2)  1900 is counted as a leap year (despite that it wasn't)
  55.       **     3)  Any fraction is considered a time of day.  For example,
  56.       **              1.5 would be noon on Jan 1st, 1900.
  57.       **
  58.  
  59.       /free
  60.  
  61.          wwDays = %diff(peDate: wwStrDate: *DAYS) + 1;
  62.  
  63.          // Excel incorrectly thinks that 1900-02-29 is
  64.          //  a valid date.
  65.  
  66.          if (peDate > d'1900-02-28');
  67.               wwDays = wwDays + 1;
  68.          endif;
  69.  
  70.          return wwDays;
  71.  
  72.       /end-free
  73.  
  74.      P hssf_date2xls   E
  75.  
  76.  
  77.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  78.       *  hssf_xls2date():
  79.       *    service program utility to convert an Excel date to
  80.       *    an RPG date field
  81.       *
  82.       *    peXls = Number used as a date in Excel
  83.       *
  84.       *  returns the RPG date
  85.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  86.      P hssf_xls2date   B                   Export
  87.      D hssf_xls2date   PI              D
  88.      D   peXls                             like(jdouble) value
  89.  
  90.      D wwStrDate       s               d   inz(d'1900-01-01')
  91.      D wwDate          s               d
  92.  
  93.       **
  94.       ** See hssf_date2xls for comments on how the Excel date format works
  95.       **
  96.       /free
  97.  
  98.          wwDate = wwStrDate + %days(%int(peXls) - 1);
  99.  
  100.          // Excel incorrectly thinks that 1900-02-29 is
  101.          //  a valid date.
  102.  
  103.          if (wwDate > d'1900-02-28');
  104.               wwDate = wwDate - %days(1);
  105.          endif;
  106.  
  107.          return wwDate;
  108.  
  109.       /end-free
  110.      P hssf_xls2date   E
  111.  
  112.  
  113.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  114.       *  hssf_xls2time():
  115.       *    service program utility to convert an Excel time to
  116.       *    an RPG time field
  117.       *
  118.       *    peXls = Number used as a time in Excel
  119.       *
  120.       *  returns the RPG date
  121.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  122.      P hssf_xls2time   B                   Export
  123.      D hssf_xls2time   PI              T
  124.      D   peXls                             like(jdouble) value
  125.  
  126.      D wwFract         s              8F
  127.      D wwSecs          s             10I 0
  128.      D wwTime          s               T
  129.      D SECSPERDAY      c                   86400
  130.  
  131.       **
  132.       ** See hssf_date2xls for comments on how the Excel date/time
  133.       ** format works.
  134.       **
  135.       /free
  136.          wwFract = peXls - %int(peXls);
  137.          wwSecs  = %inth(SECSPERDAY * wwFract);
  138.          wwTime  = t'00.00.00' + %seconds(wwSecs);
  139.          return wwTime;
  140.       /end-free
  141.      P hssf_xls2time   E
  142.  
  143.  
  144.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  145.       *  hssf_time2xls():
  146.       *    service program utility to convert an RPG time field
  147.       *    to an Excel time
  148.       *
  149.       *    peTime = RPG time field to convert
  150.       *
  151.       *  returns the Excel time, which is a floating point number
  152.       * (you have to apply a cell format to make it look like a time)
  153.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  154.      P hssf_time2xls   B                   Export
  155.      D hssf_time2xls   PI                  like(jdouble)
  156.      D   peTime                        T   value
  157.  
  158.      D wwFract         s                   like(jdouble)
  159.      D wwSecs          s             10I 0
  160.      D SECSPERDAY      c                   86400
  161.  
  162.       **
  163.       ** See hssf_date2xls for comments on how the Excel date/time
  164.       ** format works.
  165.       **
  166.       /free
  167.          wwSecs  = %diff(peTime: t'00.00.00': *SECONDS);
  168.          wwFract = wwSecs / SECSPERDAY;
  169.          return wwFract;
  170.       /end-free
  171.      P hssf_time2xls   E
  172.  
  173.  
  174.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  175.       *  hssf_xls2ts():
  176.       *    service program utility to convert an Excel date/time value
  177.       *    to an RPG timestamp field
  178.       *
  179.       *    peXls = Excel date/time value to convert
  180.       *
  181.       *  returns the RPG timestamp
  182.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  183.      P hssf_xls2ts     B                   Export
  184.      D hssf_xls2ts     PI              Z
  185.      D   peXls                             like(jdouble) value
  186.      D wwDate          s               D
  187.      D wwTime          s               T
  188.      D wwTs            s               Z
  189.       **
  190.       ** See hssf_date2xls for comments on how the Excel date/time
  191.       ** format works.
  192.       **
  193.       /free
  194.           wwDate = hssf_xls2date(peXls);
  195.           wwTime = hssf_xls2time(peXls);
  196.           wwTs   = wwDate + wwTime;
  197.           return wwTs;
  198.       /end-free
  199.      P hssf_xls2ts     E
  200.  
  201.  
  202.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  203.       *  hssf_ts2xls():
  204.       *    service program utility to convert an RPG timestamp field
  205.       *    to an Excel date/time value
  206.       *
  207.       *    peTS = RPG timestamp field to convert
  208.       *
  209.       *  returns the Excel date/time, which is a floating point number
  210.       * (you have to apply a cell format to make it look like a TS)
  211.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  212.      P hssf_ts2xls     B                   Export
  213.      D hssf_ts2xls     PI                  like(jdouble)
  214.      D   peTS                          Z   value
  215.      D wwDate          s                   like(jdouble)
  216.      D wwTime          s                   like(jdouble)
  217.       **
  218.       ** See hssf_date2xls for comments on how the Excel date/time
  219.       ** format works.
  220.       **
  221.       /free
  222.           wwTime = hssf_time2xls(%time(peTS));
  223.           wwDate = hssf_date2xls(%date(peTS));
  224.           return wwDate + wwTime;
  225.       /end-free
  226.      P hssf_ts2xls     E                                                
© 2004-2019 by midrange.com generated in 0.007s valid xhtml & css