summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2018-10-27 01:40:19 -0600
committerGitHub <noreply@github.com>2018-10-27 01:40:19 -0600
commitb6445be9bffab71b763c8a2e22f71b1317c3f46c (patch)
tree73aa9606afad1f5d0ffd6865e3f45f50b3d5c278
parentb7264cc4c5fabf82dc8299d9a02e7788f3cb446e (diff)
resolves #2770 use OS independent timezone in time attributes (PR #2784)
- use timezone offset instead of OS dependent timezone string - use UTC if offset is 0 - add tests
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor.rb8
-rw-r--r--lib/asciidoctor/document.rb8
-rw-r--r--test/fixtures/doctime-localtime.adoc2
-rw-r--r--test/invoker_test.rb52
5 files changed, 56 insertions, 15 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 518208cd..810379c9 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -82,6 +82,7 @@ Improvements::
* add support for range syntax (.. delimiter) to highlight attribute on source block (#2918)
* add support for unbounded range to highlight attribute on source block (#2918)
* automatically assign title and caption on image block if title is set on custom block source (#2926)
+ * use OS independent timezone (UTC or time offset) in doctime and localtime attributes (#2770)
Documentation::
diff --git a/lib/asciidoctor.rb b/lib/asciidoctor.rb
index ea08b7ac..f90a3226 100644
--- a/lib/asciidoctor.rb
+++ b/lib/asciidoctor.rb
@@ -1338,11 +1338,9 @@ module Asciidoctor
docdate = attrs['docdate'] = (input_mtime.strftime '%F')
attrs['docyear'] ||= input_mtime.year.to_s
end
- doctime = (attrs['doctime'] ||= begin
- input_mtime.strftime '%T %Z'
- rescue # Asciidoctor.js fails if timezone string has characters outside basic Latin (see asciidoctor.js#23)
- input_mtime.strftime '%T %z'
- end)
+ # %Z is OS dependent and may contain characters that aren't UTF-8 encoded (see asciidoctor#2770 and asciidoctor.js#23)
+ # Ruby 1.8 doesn't support %:z
+ doctime = (attrs['doctime'] ||= input_mtime.strftime %(%T #{input_mtime.utc_offset == 0 ? 'UTC' : '%z'}))
attrs['docdatetime'] = %(#{docdate} #{doctime})
elsif input.respond_to? :readlines
# NOTE tty, pipes & sockets can't be rewound, but can't be sniffed easily either
diff --git a/lib/asciidoctor/document.rb b/lib/asciidoctor/document.rb
index 0528ceb1..c1c7faee 100644
--- a/lib/asciidoctor/document.rb
+++ b/lib/asciidoctor/document.rb
@@ -499,11 +499,9 @@ class Document < AbstractBlock
localdate = attrs['localdate'] = (now.strftime '%F')
localyear = (attrs['localyear'] ||= now.year.to_s)
end
- localtime = (attrs['localtime'] ||= begin
- now.strftime '%T %Z'
- rescue # Asciidoctor.js fails if timezone string has characters outside basic Latin (see asciidoctor.js#23)
- now.strftime '%T %z'
- end)
+ # %Z is OS dependent and may contain characters that aren't UTF-8 encoded (see asciidoctor#2770 and asciidoctor.js#23)
+ # Ruby 1.8 doesn't support %:z
+ localtime = (attrs['localtime'] ||= now.strftime %(%T #{now.utc_offset == 0 ? 'UTC' : '%z'}))
attrs['localdatetime'] ||= %(#{localdate} #{localtime})
# docdate, doctime and docdatetime should default to
diff --git a/test/fixtures/doctime-localtime.adoc b/test/fixtures/doctime-localtime.adoc
new file mode 100644
index 00000000..1f63b8e0
--- /dev/null
+++ b/test/fixtures/doctime-localtime.adoc
@@ -0,0 +1,2 @@
+{doctime}
+{localtime}
diff --git a/test/invoker_test.rb b/test/invoker_test.rb
index db95496e..a6a3a6c4 100644
--- a/test/invoker_test.rb
+++ b/test/invoker_test.rb
@@ -49,12 +49,12 @@ context 'Invoker' do
test 'should allow docdate and doctime to be overridden' do
sample_filepath = fixture_path 'sample.asciidoc'
- invoker = invoke_cli_to_buffer %w(-o /dev/null -a docdate=2015-01-01 -a doctime=10:00:00-07:00), sample_filepath
+ invoker = invoke_cli_to_buffer %w(-o /dev/null -a docdate=2015-01-01 -a doctime=10:00:00-0700), sample_filepath
doc = invoker.document
assert doc.attr?('docdate', '2015-01-01')
assert doc.attr?('docyear', '2015')
- assert doc.attr?('doctime', '10:00:00-07:00')
- assert doc.attr?('docdatetime', '2015-01-01 10:00:00-07:00')
+ assert doc.attr?('doctime', '10:00:00-0700')
+ assert doc.attr?('docdatetime', '2015-01-01 10:00:00-0700')
end
test 'should accept document from stdin and write to stdout' do
@@ -664,6 +664,48 @@ Sample *AsciiDoc*
assert_match(/Total time/, error)
end
+ test 'should show timezone as UTC if system TZ is set to UTC' do
+ ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']
+ executable = File.join ASCIIDOCTOR_PROJECT_DIR, 'bin', 'asciidoctor'
+ input_path = fixture_path 'doctime-localtime.adoc'
+ cmd = %(#{ruby} #{executable} -d inline -o - -s #{input_path})
+ old_tz = ENV['TZ']
+ begin
+ ENV['TZ'] = 'UTC'
+ result = Open3.popen3(cmd) {|_, out| out.read }
+ doctime, localtime = result.lines.map {|l| l.chomp }
+ assert doctime.end_with?(' UTC')
+ assert localtime.end_with?(' UTC')
+ rescue
+ if old_tz
+ ENV['TZ'] = old_tz
+ else
+ ENV.delete 'TZ'
+ end
+ end
+ end
+
+ test 'should show timezone as offset if system TZ is not set to UTC' do
+ ruby = File.join RbConfig::CONFIG['bindir'], RbConfig::CONFIG['ruby_install_name']
+ executable = File.join ASCIIDOCTOR_PROJECT_DIR, 'bin', 'asciidoctor'
+ input_path = fixture_path 'doctime-localtime.adoc'
+ cmd = %(#{ruby} #{executable} -d inline -o - -s #{input_path})
+ old_tz = ENV['TZ']
+ begin
+ ENV['TZ'] = 'EST+5'
+ result = Open3.popen3(cmd) {|_, out| out.read }
+ doctime, localtime = result.lines.map {|l| l.chomp }
+ assert doctime.end_with?(' -0500')
+ assert localtime.end_with?(' -0500')
+ ensure
+ if old_tz
+ ENV['TZ'] = old_tz
+ else
+ ENV.delete 'TZ'
+ end
+ end
+ end
+
test 'should use SOURCE_DATE_EPOCH as modified time of input file and local time' do
old_source_date_epoch = ENV.delete 'SOURCE_DATE_EPOCH'
begin
@@ -673,10 +715,10 @@ Sample *AsciiDoc*
doc = invoker.document
assert_equal '2009-02-08', (doc.attr 'docdate')
assert_equal '2009', (doc.attr 'docyear')
- assert_match(/2009-02-08 20:03:32 (GMT|UTC)/, (doc.attr 'docdatetime'))
+ assert_match(/2009-02-08 20:03:32 UTC/, (doc.attr 'docdatetime'))
assert_equal '2009-02-08', (doc.attr 'localdate')
assert_equal '2009', (doc.attr 'localyear')
- assert_match(/2009-02-08 20:03:32 (GMT|UTC)/, (doc.attr 'localdatetime'))
+ assert_match(/2009-02-08 20:03:32 UTC/, (doc.attr 'localdatetime'))
ensure
if old_source_date_epoch
ENV['SOURCE_DATE_EPOCH'] = old_source_date_epoch