Skip to content

Fix paginator issue in different timezone

related issue #366866 (closed)

What does this MR do and why?

On issue page, comments will be loaded by page, first page will return cursor info as params in second api to load latter data, however we found cursor info is wrong when we set Rails Default Timezone, cause second page return no data.

When we generate cursor info as below code, we should not use code like field_value.strftime('%Y-%m-%d %H:%M:%S.%N %Z')

# lib/gitlab/pagination/keyset/order.rb
def cursor_attributes_for_node(node)
  column_definitions.each_with_object({}.with_indifferent_access) do |column_definition, hash|
  field_value = node[column_definition.attribute_name]
  hash[column_definition.attribute_name] = if field_value.is_a?(Time)
                                              field_value.strftime('%Y-%m-%d %H:%M:%S.%N %Z')
                                           elsif field_value.nil?
                                              nil
                                           elsif lower_named_function?(column_definition)
                                              field_value.downcase
                                           else
                                              field_value.to_s
                                           end
end

JiHu GitLab set Rails Time.zone = 'Asia/Shanghai', which will generate 2022-07-05 11:17:50.948163000 CST, CST means China Standard Time UTC+8.

However when rails try to use this time to build sql, it need convert time to UTC format, which consider CST as Central Standard Time UTC-6, then sql will query data with wrong UTC time.

We should use field_value.to_s(:inspect), same as field_value.strftime('%Y-%m-%d %H:%M:%S.%N %z'), according to strftime, %z(downcase) will generate like 2022-07-05 16:45:02.933753000 +0800, the specific timezone info will make converting UTC time in sql correctly.

Screenshots or screen recordings

Use %Z generate wrong sql query image

Use %z to generate correct sql query image

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

/cc @daveliu

Edited by MAO Chao

Merge request reports

Loading