lib/fast_excel.rb in fast_excel-0.1.1 vs lib/fast_excel.rb in fast_excel-0.1.2

- old
+ new

@@ -16,38 +16,59 @@ Libxlsxwriter.workbook_new(filename) end Libxlsxwriter::Workbook.new(workbook) end - def self.datetime(time) + # Creates internal Libxlsxwriter::Datetime from Datetime object + def self.lxw_datetime(time) date = Libxlsxwriter::Datetime.new date[:year] = time.year date[:month] = time.month date[:day] = time.day date[:hour] = time.hour date[:min] = time.minute date[:sec] = time.second date end - def self.time(time) + # Creates internal Libxlsxwriter::Datetime from Time object + def self.lxw_time(time) date = Libxlsxwriter::Datetime.new date[:year] = time.year date[:month] = time.month date[:day] = time.day date[:hour] = time.hour date[:min] = time.min date[:sec] = time.sec date end - module WorkbookExt + # seconds in 1 day + XLSX_DATE_DAY = 86400.0 - #def add_worksheet(title = nil) - # Libxlsxwriter::Worksheet.new(super(title)) - #end + # days between 1970-jan-01 and 1900-jan-01 + XLSX_DATE_EPOCH_DIFF = 25567 + # Convert time to number of days, and change beginning point from 1st jan 1970 to 1st jan 1900 + # Offset argument should be number of seconds, if not specified then it will use Time.zone.utc_offset || 0 + # + # https://support.microsoft.com/en-us/help/214330/differences-between-the-1900-and-the-1904-date-system-in-excel + def self.date_num(time, offset = nil) + unless offset + # Try use Rails' app timezone + if Time.respond_to?(:zone) + offset = Time.zone.utc_offset + else + offset = 0 # rollback to UTC + end + end + + time.to_f / XLSX_DATE_DAY + XLSX_DATE_EPOCH_DIFF + offset / XLSX_DATE_DAY + end + + module WorkbookExt + def bold_cell_format bold = add_format bold.set_bold bold end @@ -72,12 +93,12 @@ if value.is_a?(Integer) || value.is_a?(Numeric) || value.is_a?(Float) write_number(row_number, index, value, format) elsif defined?(BigDecimal) && value.is_a?(BigDecimal) write_number(row_number, index, value.to_f, format) elsif defined?(DateTime) && value.is_a?(DateTime) - write_datetime(row_number, index, FastExcel.datetime(value), format) + write_datetime(row_number, index, FastExcel.lxw_datetime(value), format) elsif value.is_a?(Time) - write_datetime(row_number, index, FastExcel.time(value), format) + write_datetime(row_number, index, FastExcel.lxw_time(value), format) else write_string(row_number, index, value.to_s, format) end end end \ No newline at end of file