github.com/google/grumpy@v0.0.0-20171122020858-3ec87959189c/lib/time.py (about)

     1  # Copyright 2016 Google Inc. All Rights Reserved.
     2  #
     3  # Licensed under the Apache License, Version 2.0 (the "License");
     4  # you may not use this file except in compliance with the License.
     5  # You may obtain a copy of the License at
     6  #
     7  #     http://www.apache.org/licenses/LICENSE-2.0
     8  #
     9  # Unless required by applicable law or agreed to in writing, software
    10  # distributed under the License is distributed on an "AS IS" BASIS,
    11  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  # See the License for the specific language governing permissions and
    13  # limitations under the License.
    14  
    15  """Time access and conversions."""
    16  
    17  from '__go__/time' import Local, Now, Second, Sleep, Unix, Date, UTC # pylint: disable=g-multiple-import
    18  
    19  
    20  _strftime_directive_map = {
    21      '%': '%',
    22      'a': 'Mon',
    23      'A': 'Monday',
    24      'b': 'Jan',
    25      'B': 'January',
    26      'c': NotImplemented,
    27      'd': '02',
    28      'H': '15',
    29      'I': '03',
    30      'j': NotImplemented,
    31      'L': '.000',
    32      'm': '01',
    33      'M': '04',
    34      'p': 'PM',
    35      'S': '05',
    36      'U': NotImplemented,
    37      'W': NotImplemented,
    38      'w': NotImplemented,
    39      'X': NotImplemented,
    40      'x': NotImplemented,
    41      'y': '06',
    42      'Y': '2006',
    43      'Z': 'MST',
    44      'z': '-0700',
    45  }
    46  
    47  
    48  class struct_time(tuple):  #pylint: disable=invalid-name,missing-docstring
    49  
    50    def __init__(self, args):
    51      super(struct_time, self).__init__(tuple, args)
    52      self.tm_year = self[0]
    53      self.tm_mon = self[1]
    54      self.tm_mday = self[2]
    55      self.tm_hour = self[3]
    56      self.tm_min = self[4]
    57      self.tm_sec = self[5]
    58      self.tm_wday = self[6]
    59      self.tm_yday = self[7]
    60      self.tm_isdst = self[8]
    61  
    62    def __repr__(self):
    63      return ("time.struct_time(tm_year=%s, tm_mon=%s, tm_mday=%s, "
    64              "tm_hour=%s, tm_min=%s, tm_sec=%s, tm_wday=%s, "
    65              "tm_yday=%s, tm_isdst=%s)") % self
    66  
    67    def __str__(self):
    68      return repr(self)
    69  
    70  
    71  def gmtime(seconds=None):
    72    t = (Unix(seconds, 0) if seconds else Now()).UTC()
    73    return struct_time((t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(),
    74                        t.Second(), (t.Weekday() + 6) % 7, t.YearDay(), 0))
    75  
    76  
    77  def localtime(seconds=None):
    78    t = (Unix(seconds, 0) if seconds else Now()).Local()
    79    return struct_time((t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(),
    80                        t.Second(), (t.Weekday() + 6) % 7, t.YearDay(), 0))
    81  
    82  
    83  def mktime(t):
    84    return float(Date(t[0], t[1], t[2], t[3], t[4], t[5], 0, Local).Unix())
    85  
    86  
    87  def sleep(secs):
    88    Sleep(secs * Second)
    89  
    90  
    91  def time():
    92    return float(Now().UnixNano()) / Second
    93  
    94  
    95  def strftime(format, tt=None):  # pylint: disable=missing-docstring,redefined-builtin
    96    t = Unix(int(mktime(tt)), 0) if tt else Now()
    97    ret = []
    98    prev, n = 0, format.find('%', 0, -1)
    99    while n != -1:
   100      ret.append(format[prev:n])
   101      next_ch = format[n + 1]
   102      c = _strftime_directive_map.get(next_ch)
   103      if c is NotImplemented:
   104        raise NotImplementedError('Code: %' + next_ch + ' not yet supported')
   105      if c:
   106        ret.append(t.Format(c))
   107      else:
   108        ret.append(format[n:n+2])
   109      n += 2
   110      prev, n = n, format.find('%', n, -1)
   111    ret.append(format[prev:])
   112    return ''.join(ret)
   113  
   114  
   115  # TODO: Calculate real value for daylight saving.
   116  daylight = 0
   117  
   118  # TODO: Use local DST instead of ''.
   119  tzname = (Now().Zone()[0], '')
   120