Merge pull request #957 from mojombo/timezonify

Set Timezone in _config.yml
This commit is contained in:
Parker Moore 2013-04-15 07:23:53 -07:00
commit afa3ef90b9
6 changed files with 93 additions and 4 deletions

View File

@ -116,6 +116,46 @@ Feature: Site configuration
And I should see "Post Layout: <p>content for entry1.</p>" in "_site/2007/12/31/entry1.html" And I should see "Post Layout: <p>content for entry1.</p>" in "_site/2007/12/31/entry1.html"
And I should see "Post Layout: <p>content for entry2.</p>" in "_site/2020/01/31/entry2.html" And I should see "Post Layout: <p>content for entry2.</p>" in "_site/2020/01/31/entry2.html"
Scenario: Generate proper dates with explicitly set timezone (same as posts' time)
Given I have a _layouts directory
And I have a page layout that contains "Page Layout: {{ site.posts.size }}"
And I have a post layout that contains "Post Layout: {{ content }} built at {{ page.date | date_to_xmlschema }}"
And I have an "index.html" page with layout "page" that contains "site index page"
And I have a configuration file with:
| key | value |
| timezone | America/New_York |
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2013-04-09 23:22 -0400 | post | content for entry1. |
| entry2 | 2013-04-10 03:14 -0400 | post | content for entry2. |
When I run jekyll
Then the _site directory should exist
And I should see "Page Layout: 2" in "_site/index.html"
And I should see "Post Layout: <p>content for entry1.</p> built at 2013-04-09T23:22:00-04:00" in "_site/2013/04/09/entry1.html"
And I should see "Post Layout: <p>content for entry2.</p> built at 2013-04-10T03:14:00-04:00" in "_site/2013/04/10/entry2.html"
Scenario: Generate proper dates with explicitly set timezone (different than posts' time)
Given I have a _layouts directory
And I have a page layout that contains "Page Layout: {{ site.posts.size }}"
And I have a post layout that contains "Post Layout: {{ content }} built at {{ page.date | date_to_xmlschema }}"
And I have an "index.html" page with layout "page" that contains "site index page"
And I have a configuration file with:
| key | value |
| timezone | Australia/Melbourne |
And I have a _posts directory
And I have the following posts:
| title | date | layout | content |
| entry1 | 2013-04-09 23:22 -0400 | post | content for entry1. |
| entry2 | 2013-04-10 03:14 -0400 | post | content for entry2. |
When I run jekyll
Then the _site directory should exist
And I should see "Page Layout: 2" in "_site/index.html"
And the "_site/2013/04/10/entry1.html" file should exist
And the "_site/2013/04/10/entry2.html" file should exist
And I should see escaped "Post Layout: <p>content for entry1.</p> built at 2013-04-10T13:22:00+10:00" in "_site/2013/04/10/entry1.html"
And I should see escaped "Post Layout: <p>content for entry2.</p> built at 2013-04-10T17:14:00+10:00" in "_site/2013/04/10/entry2.html"
Scenario: Limit the number of posts generated by most recent date Scenario: Limit the number of posts generated by most recent date
Given I have a _posts directory Given I have a _posts directory
And I have a configuration file with: And I have a configuration file with:

View File

@ -1,4 +1,5 @@
Before do Before do
FileUtils.rm_rf(TEST_DIR)
FileUtils.mkdir(TEST_DIR) FileUtils.mkdir(TEST_DIR)
Dir.chdir(TEST_DIR) Dir.chdir(TEST_DIR)
end end
@ -61,7 +62,14 @@ Given /^I have the following (draft|post)s?(?: (.*) "(.*)")?:$/ do |status, dire
if "draft" == status if "draft" == status
path = File.join(before || '.', '_drafts', after || '.', "#{title}.#{ext}") path = File.join(before || '.', '_drafts', after || '.', "#{title}.#{ext}")
else else
date = Date.strptime(post['date'], '%m/%d/%Y').strftime('%Y-%m-%d') format = if has_time_component?(post['date'])
'%Y-%m-%d %H:%M %z'
else
'%m/%d/%Y' # why even
end
parsed_date = DateTime.strptime(post['date'], format)
post['date'] = parsed_date.to_s
date = parsed_date.strftime('%Y-%m-%d')
path = File.join(before || '.', '_posts', after || '.', "#{date}-#{title}.#{ext}") path = File.join(before || '.', '_posts', after || '.', "#{date}-#{title}.#{ext}")
end end
@ -69,6 +77,9 @@ Given /^I have the following (draft|post)s?(?: (.*) "(.*)")?:$/ do |status, dire
%w(title layout tag tags category categories published author path).each do |key| %w(title layout tag tags category categories published author path).each do |key|
matter_hash[key] = post[key] if post[key] matter_hash[key] = post[key] if post[key]
end end
if "post" == status
matter_hash["date"] = post["date"] if post["date"]
end
matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join.chomp matter = matter_hash.map { |k, v| "#{k}: #{v}\n" }.join.chomp
content = post['content'] content = post['content']
@ -134,7 +145,11 @@ Then /^the (.*) directory should exist$/ do |dir|
end end
Then /^I should see "(.*)" in "(.*)"$/ do |text, file| Then /^I should see "(.*)" in "(.*)"$/ do |text, file|
assert_match Regexp.new(text), File.open(file).readlines.join assert Regexp.new(text).match(File.open(file).readlines.join)
end
Then /^I should see escaped "(.*)" in "(.*)"$/ do |text, file|
assert Regexp.new(Regexp.escape(text)).match(File.open(file).readlines.join)
end end
Then /^the "(.*)" file should exist$/ do |file| Then /^the "(.*)" file should exist$/ do |file|

View File

@ -7,7 +7,7 @@ World do
end end
TEST_DIR = File.join('/', 'tmp', 'jekyll') TEST_DIR = File.join('/', 'tmp', 'jekyll')
JEKYLL_PATH = File.join(ENV['PWD'], 'bin', 'jekyll') JEKYLL_PATH = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'jekyll')
def run_jekyll(opts = {}) def run_jekyll(opts = {})
command = JEKYLL_PATH.clone command = JEKYLL_PATH.clone
@ -17,5 +17,9 @@ def run_jekyll(opts = {})
system command system command
end end
def has_time_component?(date_string)
date_string.split(" ").size > 1
end
# work around "invalid option: --format" cucumber bug (see #296) # work around "invalid option: --format" cucumber bug (see #296)
Test::Unit.run = true if RUBY_VERSION < '1.9' Test::Unit.run = true if RUBY_VERSION < '1.9'

View File

@ -73,6 +73,18 @@ module Jekyll
config = config.read_config_files(config.config_files(override)) config = config.read_config_files(config.config_files(override))
# Merge DEFAULTS < _config.yml < override # Merge DEFAULTS < _config.yml < override
config.deep_merge(override).stringify_keys config = config.deep_merge(override).stringify_keys
set_timezone(config['timezone']) if config['timezone']
config
end
# Static: Set the TZ environment variable to use the timezone specified
#
# timezone - the IANA Time Zone
#
# Returns nothing
def self.set_timezone(timezone)
ENV['TZ'] = timezone
end end
end end

View File

@ -10,6 +10,8 @@ module Jekyll
'layouts' => '_layouts', 'layouts' => '_layouts',
'keep_files' => ['.git','.svn'], 'keep_files' => ['.git','.svn'],
'timezone' => nil, # use the local timezone
'safe' => false, 'safe' => false,
'show_drafts' => nil, 'show_drafts' => nil,
'limit_posts' => nil, 'limit_posts' => nil,

View File

@ -81,6 +81,22 @@ class="flag">flags</code> (specified on the command-line) that control them.
<p><code class="option">include: [DIR, FILE, ...]</code></p> <p><code class="option">include: [DIR, FILE, ...]</code></p>
</td> </td>
</tr> </tr>
<tr class='setting'>
<td>
<p class='name'><strong>Time Zone</strong></p>
<p class="description">
Set the time zone for site generation. This sets the <code>TZ</code>
environment variable, which Ruby uses to handle time and date
creation and manipulation. Any entry from the
<a href="http://en.wikipedia.org/wiki/Tz_database">IANA Time Zone
Database</a> is valid, e.g. <code>America/New_York</code>. The default
is the local time zone, as set by your operating system.
</p>
</td>
<td class='align-center'>
<p><code class="option">timezone: TIMEZONE</code></p>
</td>
</tr>
</tbody> </tbody>
</table> </table>