Format: text/x-python
⬇ Download File (3.3 KB)
   1  # Copyright 2010-2023 Kurt McKee <contactme@kurtmckee.org>
   2  # Copyright 2002-2008 Mark Pilgrim
   3  # All rights reserved.
   4  #
   5  # This file is a part of feedparser.
   6  #
   7  # Redistribution and use in source and binary forms, with or without
   8  # modification, are permitted provided that the following conditions are met:
   9  #
  10  # * Redistributions of source code must retain the above copyright notice,
  11  #   this list of conditions and the following disclaimer.
  12  # * Redistributions in binary form must reproduce the above copyright notice,
  13  #   this list of conditions and the following disclaimer in the documentation
  14  #   and/or other materials provided with the distribution.
  15  #
  16  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
  17  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19  # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20  # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21  # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22  # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23  # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24  # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25  # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26  # POSSIBILITY OF SUCH DAMAGE.
  27  
  28  import re
  29  
  30  from .w3dtf import _parse_date_w3dtf
  31  
  32  # 8-bit date handling routines written by ytrewq1.
  33  _korean_year = '\ub144' # b3e2 in euc-kr
  34  _korean_month = '\uc6d4' # bff9 in euc-kr
  35  _korean_day = '\uc77c' # c0cf in euc-kr
  36  _korean_am = '\uc624\uc804' # bfc0 c0fc in euc-kr
  37  _korean_pm = '\uc624\ud6c4' # bfc0 c8c4 in euc-kr
  38  
  39  _korean_onblog_date_re = re.compile(
  40      r'(\d{4})%s\s+(\d{2})%s\s+(\d{2})%s\s+(\d{2}):(\d{2}):(\d{2})'
  41      % (_korean_year, _korean_month, _korean_day)
  42  )
  43  
  44  _korean_nate_date_re = re.compile(
  45      r'(\d{4})-(\d{2})-(\d{2})\s+(%s|%s)\s+(\d{,2}):(\d{,2}):(\d{,2})'
  46      % (_korean_am, _korean_pm))
  47  
  48  
  49  def _parse_date_onblog(dateString):
  50      """Parse a string according to the OnBlog 8-bit date format"""
  51      m = _korean_onblog_date_re.match(dateString)
  52      if not m:
  53          return
  54      w3dtfdate = '%(year)s-%(month)s-%(day)sT%(hour)s:%(minute)s:%(second)s%(zonediff)s' % \
  55                  {'year': m.group(1), 'month': m.group(2), 'day': m.group(3),
  56                   'hour': m.group(4), 'minute': m.group(5), 'second': m.group(6),
  57                   'zonediff': '+09:00'}
  58      return _parse_date_w3dtf(w3dtfdate)
  59  
  60  
  61  def _parse_date_nate(dateString):
  62      """Parse a string according to the Nate 8-bit date format"""
  63      m = _korean_nate_date_re.match(dateString)
  64      if not m:
  65          return
  66      hour = int(m.group(5))
  67      ampm = m.group(4)
  68      if ampm == _korean_pm:
  69          hour += 12
  70      hour = str(hour)
  71      if len(hour) == 1:
  72          hour = '0' + hour
  73      w3dtfdate = '%(year)s-%(month)s-%(day)sT%(hour)s:%(minute)s:%(second)s%(zonediff)s' % \
  74                  {
  75                      'year': m.group(1),
  76                      'month': m.group(2),
  77                      'day': m.group(3),
  78                      'hour': hour,
  79                      'minute': m.group(6),
  80                      'second': m.group(7),
  81                      'zonediff': '+09:00',
  82                  }
  83      return _parse_date_w3dtf(w3dtfdate)
  84