Repository: getnikola/nikola

Issue:
When building a site with multiple date_formats, Nikola sometimes crashes and others it messes up the formats
My site source: https://github.com/sukiletxe/sukiletxe.github.io/tree/src
Some more data to track down the issue:
- IRC log: https://irclogs.getnikola.com/logs/2016-05-03/
- Traceback: http://pastebin.com/WJmyCjT7
- Weird things happen when setting locale: http://pastebin.com/DsJ4eED8 (generated using https://gist.github.com/Kwpolska/3cf154f5fc7444660a9c26a2e8adee7c

Note: Is it necessary to use calendar.different_locale? This may be causing the issue...



Discussion:
Hello,
So I tried to build the site with just one `date_format`, and it still fails. 

[Traceback here](http://pastebin.com/aTPBQ6Qg). [Here is the output generated with the previous script](http://pastebin.com/vFphr1ST).

That repo is missing a conf.py, so I tried with the defult one, except setting 

```
DATE_FORMAT = '%Y-%m-%d'
```

And it worked. So wither it's something in your config I don't know about, or it's windows specific.

It does have a conf.py, see https://github.com/sukiletxe/sukiletxe.github.io/blob/src/conf.py . 

Actually, I suspect the issue may be due to archives rather than dates, because of my previous comment. I'll test later and report back. I'll also try to test on Linux.

I've just tried on a Linux machine (specifically Ubuntu Mate), and there 
were no tracebacks, but when previewing the formats were messed up. 
Like, for example, in Spanish the one used for Basque was displayed.

Seeing my previous logs, it doesn't surprise me that in Linux there were 
no tracebacks, because something tries to change the locale into a Linux 
compatible one. Therefore, only Windows crashes.

El 04/06/2016 a las 2:59, Roberto Alsina escribió:

> That repo is missing a conf.py, so I tried with the defult one, except 
> setting
> 
> |DATE_FORMAT = '%Y-%m-%d' |
> 
> And it worked. So wither it's something in your config I don't know 
> about, or it's windows specific.
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub 
> https://github.com/getnikola/nikola/issues/2332#issuecomment-223726521, 
> or mute the thread 
> https://github.com/notifications/unsubscribe/AHG-oYQSIMO4ZMN7r2xGGw9gwB_KXdI_ks5qIM3YgaJpZM4IWdy5.

If you could provide steps to reproduce this, and what I should look for in the output, much appreciated.

After cloning the repository and checking out the `src` branch, run `nikola build` several times, checking the output and the generated site after each one.

If you are using Linux (probably any *Nix operating system), you won't get any errors, but the dates will sometimes be messed up (i. e. the `DATE_FORMAT` of another language will be used instead of the one for the current language). 

On the other hand, if you are using Windows, in addition to this problem, a traceback will eventually show up like the one posted above. The reason for that is that somehow an Unix-like locale is passed to `locale.setlocale`, and it complains when trying to query a month name.

This also happens with archives, if monthly or full archives are selected. Again, only on Windows.

@sukiletxe I have no idea what "the dates will sometimes be messed up". If you could create a small sample showing the failures, and describe what the failure is, it's much easier for me.

My site has currently three date formats:

```
DATE_FORMAT ={
    'default_lang': '%A, %d de %B de %Y %H:%M',
    'en': '%A, %B %d %Y %H:%M',
    'eu': '%A, %Y(e)ko %Bk %d %H:%M',
}
```

Which would render, for example:
- DEFAULT_LANG: Sábado, 19 de marzo de 2016 01:16
- en: Saturday, March 1 2016 01:16
- eu: Larunbata, 2016(e)ko martxoak 16 01:16

But this is rendered instead in one of the builds in the Spanish subsite:

sábado, 2016(e)ko marzok 19 01:16

And in another one:

sábado, marzo 19 2016 01:16 

These results were all gathered using Windows.

It would seem that only the default language is affected, but this showed up in Linux, in the English subsite:

Saturday, 2016(e)ko Marchk 19 01:16

Maybe this is not important, but in every build, the tags were rendered, although they weren't changed.

@ralsina, is there anything else I can do to try to isolate the problem?

Ok, I'll take a look today, maybe.

On Sun, Jun 12, 2016 at 7:05 AM Sukil Etxenike notifications@github.com
wrote:

> My site has currently three date formats:
> 
> DATE_FORMAT ={
>     'default_lang': '%A, %d de %B de %Y %H:%M',
>     'en': '%A, %B %d %Y %H:%M',
>     'eu': '%A, %Y(e)ko %Bk %d %H:%M',
> }
> 
> Which would render, for example:
> - DEFAULT_LANG: Sábado, 19 de marzo de 2016 01:16
> - en: Saturday, March 1 2016 01:16
> - eu: Larunbata, 2016(e)ko martxoak 16 01:16
> 
> But this is rendered instead in one of the builds in the Spanish subsite:
> 
> sábado, 2016(e)ko marzok 19 01:16
> 
> And in another one:
> 
> sábado, marzo 19 2016 01:16
> 
> These results were all gathered using Windows.
> 
> It would seem that only the default language is affected, but this showed
> up in Linux, in the English subsite:
> 
> Saturday, 2016(e)ko Marchk 19 01:16
> 
> Maybe this is not important, but in every build, the tags were rendered,
> although they weren't changed.
> 
> @ralsina https://github.com/ralsina, is there anything else I can do to
> try to isolate the problem?
> 
> —
> You are receiving this because you were mentioned.
> 
> Reply to this email directly, view it on GitHub
> https://github.com/getnikola/nikola/issues/2332#issuecomment-225424324,
> or mute the thread
> https://github.com/notifications/unsubscribe/AAAGK4x-OYfXRnzQT8ztSdtJrWyoAYsaks5qK9n7gaJpZM4IWdy5
> .

@sukiletxe there is a problem in your example config. "default_lang" is not a valid language.
What you could use there is DEFAULT_LANG (uppercase, no quotes) which is a variable set earlier in conf.py.

If you really have "default_lang" there I suspect that could cause many if not all the symptoms you see.

Me bad. I didn't notice. Thank you very much. Seems that the 
interchanging of formats is fixed, but not the Windows traceback.

El 23/06/2016 a las 2:51, Roberto Alsina escribió:

> @sukiletxe https://github.com/sukiletxe there is a problem in your 
> example config. "default_lang" is not a valid language.
> What you could use there is DEFAULT_LANG (uppercase, no quotes) which 
> is a variable set earlier in conf.py.
> 
> If you really have "default_lang" there I suspect that could cause 
> many if not all the symptoms you see.
> 
> —
> You are receiving this because you were mentioned.
> Reply to this email directly, view it on GitHub 
> https://github.com/getnikola/nikola/issues/2332#issuecomment-227920108, 
> or mute the thread 
> https://github.com/notifications/unsubscribe/AHG-oRjLj3nvRMRMFFoZsv0pctDTZMcpks5qOdiLgaJpZM4IWdy5.

Ok, so this is now Windows-only

That's right.

Since I don't have a windows machine available, I am not able to work on this. De-assigning me and removing from next milestone.


Resolving patch:
diff --git a/nikola/utils.py b/nikola/utils.py
--- a/nikola/utils.py
+++ b/nikola/utils.py
@@ -1080,7 +1080,6 @@ class LocaleBorg(object):
     NOTE: never use locale.getlocale() , it can return values that
     locale.setlocale will not accept in Windows XP, 7 and pythons 2.6, 2.7, 3.3
     Examples: "Spanish", "French" can't do the full circle set / get / set
-    That used to break calendar, but now seems is not the case, with month at least
     """
 
     initialized = False
@@ -1199,20 +1198,16 @@ def get_month_name(self, month_no, lang):
                 res = handler(month_no, lang)
                 if res is not None:
                     return res
-            if sys.version_info[0] == 3:  # Python 3
-                with calendar.different_locale(self.locales[lang]):
-                    s = calendar.month_name[month_no]
-                # for py3 s is unicode
-            else:  # Python 2
-                with calendar.TimeEncoding(self.locales[lang]):
-                    s = calendar.month_name[month_no]
+            old_lang = self.current_lang
+            self.__set_locale(lang)
+            s = calendar.month_name[month_no]
+            self.__set_locale(old_lang)
+            if sys.version_info[0] == 2:
                 enc = self.encodings[lang]
                 if not enc:
                     enc = 'UTF-8'
 
                 s = s.decode(enc)
-            # paranoid about calendar ending in the wrong locale (windows)
-            self.__set_locale(self.current_lang)
             return s
 
     def formatted_date(self, date_format, date):


Test patch: