semantic_time.py 8.86 KB
Newer Older
1 2 3
# -*- coding: utf-8 -*-
"""Semantic time implementation."""

4 5
from __future__ import unicode_literals

6 7 8 9
from dfdatetime import interface


class SemanticTime(interface.DateTimeValues):
10
  """Semantic time.
11 12 13 14 15 16 17 18

  Semantic time is term to describe date and time values that have specific
  meaning such as: "Never", "Yesterday", "Not set".

  Attributes:
    is_local_time (bool): True if the date and time value is in local time.
  """

19 20
  _SORT_ORDER = 50

21 22 23 24 25 26 27 28
  def __init__(self, string=None):
    """Initializes a semantic time.

    Args:
      string (str): semantic representation of the time, such as:
          "Never", "Not set".
    """
    super(SemanticTime, self).__init__()
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    self._string = string

  @property
  def string(self):
    """str: semantic representation of the time, such as: "Never"."""
    return self._string

  def __eq__(self, other):
    """Determines if the date time values are equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are equal to other.
    """
    if not isinstance(other, SemanticTime):
      return False

    return self._SORT_ORDER == other._SORT_ORDER  # pylint: disable=protected-access

  def __ge__(self, other):
    """Determines if the date time values are greater than or equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are greater than or equal to other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    if not isinstance(other, SemanticTime):
      return False

    return self._SORT_ORDER >= other._SORT_ORDER  # pylint: disable=protected-access

  def __gt__(self, other):
    """Determines if the date time values are greater than other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are greater than other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    if not isinstance(other, SemanticTime):
      return False

    return self._SORT_ORDER > other._SORT_ORDER  # pylint: disable=protected-access

  def __le__(self, other):
    """Determines if the date time values are greater than or equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are greater than or equal to other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    if not isinstance(other, SemanticTime):
      return True

    return self._SORT_ORDER <= other._SORT_ORDER  # pylint: disable=protected-access

  def __lt__(self, other):
    """Determines if the date time values are less than other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are less than other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    if not isinstance(other, SemanticTime):
      return True

    return self._SORT_ORDER < other._SORT_ORDER  # pylint: disable=protected-access

  def __ne__(self, other):
    """Determines if the date time values are not equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are not equal to other.
    """
    if not isinstance(other, SemanticTime):
      return True

    return self._SORT_ORDER != other._SORT_ORDER  # pylint: disable=protected-access

  def _GetNormalizedTimestamp(self):
    """Retrieves the normalized timestamp.

    Returns:
      decimal.Decimal: normalized timestamp, which contains the number of
          seconds since January 1, 1970 00:00:00 and a fraction of second used
          for increased precision, or None if the normalized timestamp cannot be
          determined.
    """
    return None
154

155
  def CopyFromDateTimeString(self, time_string):
156
    """Copies semantic time from a date and time string.
157 158 159 160 161 162 163 164

    Args:
      time_string (str): semantic representation of the time, such as:
          "Never", "Not set".

    Raises:
      ValueError: because semantic time cannot be copied from a string.
    """
165
    self._string = time_string
166

167 168 169 170 171 172
  def CopyToDateTimeString(self):
    """Copies the date time value to a date and time string.

    Returns:
      str: semantic representation of the time, such as: "Never", "Not set".
    """
173
    return self._string
174

175 176 177 178 179
  def CopyToStatTimeTuple(self):
    """Copies the semantic timestamp to a stat timestamp tuple.

    Returns:
      tuple[int, int]: a POSIX timestamp in seconds and the remainder in
180 181 182 183 184 185 186 187 188 189
          100 nano seconds, which will always be None, None.
    """
    return None, None

  def GetDate(self):
    """Retrieves the date represented by the date and time values.

    Returns:
       tuple[int, int, int]: year, month, day of month or (None, None, None)
           if the date and time values do not represent a date.
190
    """
191
    return None, None, None
192 193 194 195 196 197 198 199

  def GetPlasoTimestamp(self):
    """Retrieves a timestamp that is compatible with plaso.

    Returns:
      int: a POSIX timestamp in microseconds, which will always be 0.
    """
    return 0
200 201 202 203 204


class InvalidTime(SemanticTime):
  """Semantic time that represents invalid."""

205 206
  _SORT_ORDER = 1

207 208 209 210 211 212 213 214
  def __init__(self):
    """Initializes a semantic time that represents invalid."""
    super(InvalidTime, self).__init__(string='Invalid')


class Never(SemanticTime):
  """Semantic time that represents never."""

215 216
  _SORT_ORDER = 99

217 218 219 220
  def __init__(self):
    """Initializes a semantic time that represents never."""
    super(Never, self).__init__(string='Never')

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
  def __eq__(self, other):
    """Determines if the date time values are equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are equal to other.
    """
    return isinstance(other, Never)

  def __ge__(self, other):
    """Determines if the date time values are greater than or equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are greater than or equal to other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    return True

  def __gt__(self, other):
    """Determines if the date time values are greater than other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are greater than other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    return not isinstance(other, Never)

  def __le__(self, other):
    """Determines if the date time values are less than or equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are greater than or equal to other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    return isinstance(other, Never)

  def __lt__(self, other):
    """Determines if the date time values are less than other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are less than other.

    Raises:
      ValueError: if other is not an instance of DateTimeValues.
    """
    if not isinstance(other, interface.DateTimeValues):
      raise ValueError('Other not an instance of DateTimeValues')

    return False

  def __ne__(self, other):
    """Determines if the date time values are not equal to other.

    Args:
      other (DateTimeValues): date time values to compare against.

    Returns:
      bool: True if the date time values are not equal to other.
    """
    return not isinstance(other, Never)

311 312 313 314

class NotSet(SemanticTime):
  """Semantic time that represents not set."""

315 316
  _SORT_ORDER = 2

317 318 319
  def __init__(self):
    """Initializes a semantic time that represents not set."""
    super(NotSet, self).__init__(string='Not set')