/usr/lib/python2.7/dist-packages/dfdatetime/interface.py is in python-dfdatetime 20180110-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 | # -*- coding: utf-8 -*-
"""Date and time values interface."""
from __future__ import unicode_literals
import abc
import calendar
from dfdatetime import decorators
class DateTimeValues(object):
"""Date time values interface.
This is the super class of different date and time representations.
Attributes:
is_local_time (bool): True if the date and time value is in local time.
precision (str): precision of the date and time value, which should
be one of the PRECISION_VALUES in definitions.
"""
_DAYS_PER_MONTH = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
_100NS_PER_SECOND = 10000000
_100NS_PER_DECISECOND = 1000000
_100NS_PER_MILLISECOND = 10000
_100NS_PER_MICROSECOND = 10
_INT64_MIN = -(1 << 63)
_INT64_MAX = (1 << 63) - 1
def __init__(self):
"""Initializes date time values."""
super(DateTimeValues, self).__init__()
self.is_local_time = False
self.precision = None
def _AdjustForTimeZoneOffset(
self, year, month, day_of_month, hours, minutes, time_zone_offset):
"""Adjusts the date and time values for a time zone offset.
Args:
year (int): year.
month (int): month.
day_of_month (int): day of month.
hours (int): hours.
minutes (int): minutes.
time_zone_offset (int): time zone offset in number of minutes from UTC.
Returns:
tuple[int, int, int, int, int, int]: time zone correct year, month,
day_of_month, hours and minutes values.
"""
hours_from_utc, minutes_from_utc = divmod(time_zone_offset, 60)
minutes += minutes_from_utc
# Since divmod makes sure the sign of minutes_from_utc is positive
# we only need to check the upper bound here, because hours_from_utc
# remains signed it is corrected accordingly.
if minutes >= 60:
minutes -= 60
hours += 1
hours += hours_from_utc
if hours < 0:
hours += 24
day_of_month -= 1
elif hours >= 24:
hours -= 24
day_of_month += 1
days_per_month = self._GetDaysPerMonth(year, month)
if day_of_month < 1:
month -= 1
if month < 1:
month = 12
year -= 1
day_of_month += self._GetDaysPerMonth(year, month)
elif day_of_month > days_per_month:
month += 1
if month > 12:
month = 1
year += 1
day_of_month -= days_per_month
return year, month, day_of_month, hours, minutes
def _CopyDateFromString(self, date_string):
"""Copies a date from a string.
Args:
date_string (str): date value formatted as: YYYY-MM-DD
Returns:
tuple[int, int, int]: year, month, day of month.
Raises:
ValueError: if the date string is invalid or not supported.
"""
date_string_length = len(date_string)
# The date string should at least contain 'YYYY-MM-DD'.
if date_string_length < 10:
raise ValueError('Date string too short.')
if date_string[4] != '-' or date_string[7] != '-':
raise ValueError('Invalid date string.')
try:
year = int(date_string[0:4], 10)
except ValueError:
raise ValueError('Unable to parse year.')
try:
month = int(date_string[5:7], 10)
except ValueError:
raise ValueError('Unable to parse month.')
try:
day_of_month = int(date_string[8:10], 10)
except ValueError:
raise ValueError('Unable to parse day of month.')
days_per_month = self._GetDaysPerMonth(year, month)
if day_of_month < 1 or day_of_month > days_per_month:
raise ValueError('Day of month value out of bounds.')
return year, month, day_of_month
def _CopyDateTimeFromString(self, time_string):
"""Copies a date and time from a string.
Args:
time_string (str): date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######[+-]##:##
Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The time of day, seconds
fraction and time zone offset are optional. The default time zone
is UTC.
Returns:
dict[str, int]: date and time values, such as year, month, day of month,
hours, minutes, seconds, microseconds.
Raises:
ValueError: if the time string is invalid or not supported.
"""
if not time_string:
raise ValueError('Invalid time string.')
time_string_length = len(time_string)
year, month, day_of_month = self._CopyDateFromString(time_string)
if time_string_length <= 10:
return {
'year': year,
'month': month,
'day_of_month': day_of_month}
# If a time of day is specified the time string it should at least
# contain 'YYYY-MM-DD hh:mm:ss'.
if time_string[10] != ' ':
raise ValueError(
'Invalid time string - space missing as date and time separator.')
hours, minutes, seconds, microseconds, time_zone_offset = (
self._CopyTimeFromString(time_string[11:]))
if time_zone_offset:
year, month, day_of_month, hours, minutes = self._AdjustForTimeZoneOffset(
year, month, day_of_month, hours, minutes, time_zone_offset)
date_time_values = {
'year': year,
'month': month,
'day_of_month': day_of_month,
'hours': hours,
'minutes': minutes,
'seconds': seconds}
if microseconds is not None:
date_time_values['microseconds'] = microseconds
return date_time_values
def _CopyTimeFromString(self, time_string):
"""Copies a time from a string.
Args:
time_string (str): time value formatted as:
hh:mm:ss.######[+-]##:##
Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The seconds fraction and
time zone offset are optional.
Returns:
tuple[int, int, int, int, int]: hours, minutes, seconds, microseconds,
time zone offset in minutes.
Raises:
ValueError: if the time string is invalid or not supported.
"""
time_string_length = len(time_string)
# The time string should at least contain 'hh:mm:ss'.
if time_string_length < 8:
raise ValueError('Time string too short.')
if time_string[2] != ':' or time_string[5] != ':':
raise ValueError('Invalid time string.')
try:
hours = int(time_string[0:2], 10)
except ValueError:
raise ValueError('Unable to parse hours.')
if hours not in range(0, 24):
raise ValueError('Hours value: {0:d} out of bounds.'.format(hours))
try:
minutes = int(time_string[3:5], 10)
except ValueError:
raise ValueError('Unable to parse minutes.')
if minutes not in range(0, 60):
raise ValueError('Minutes value: {0:d} out of bounds.'.format(minutes))
try:
seconds = int(time_string[6:8], 10)
except ValueError:
raise ValueError('Unable to parse day of seconds.')
# TODO: support a leap second?
if seconds not in range(0, 60):
raise ValueError('Seconds value: {0:d} out of bounds.'.format(seconds))
microseconds = None
time_zone_offset = None
time_zone_string_index = 8
while time_zone_string_index < time_string_length:
if time_string[time_zone_string_index] in ('+', '-'):
break
time_zone_string_index += 1
# The calculations that follow rely on the time zone string index
# to point beyond the string in case no time zone offset was defined.
if time_zone_string_index == time_string_length - 1:
time_zone_string_index += 1
if time_string_length > 8 and time_string[8] == '.':
time_fraction_length = time_zone_string_index - 9
if time_fraction_length not in (3, 6):
raise ValueError('Invalid time string.')
try:
time_fraction = time_string[9:time_zone_string_index]
time_fraction = int(time_fraction, 10)
except ValueError:
raise ValueError('Unable to parse time fraction.')
if time_fraction_length == 3:
time_fraction *= 1000
microseconds = time_fraction
if time_zone_string_index < time_string_length:
if (time_string_length - time_zone_string_index != 6 or
time_string[time_zone_string_index + 3] != ':'):
raise ValueError('Invalid time string.')
try:
hours_from_utc = int(time_string[
time_zone_string_index + 1:time_zone_string_index + 3])
except ValueError:
raise ValueError('Unable to parse time zone hours offset.')
if hours_from_utc not in range(0, 15):
raise ValueError('Time zone hours offset value out of bounds.')
try:
minutes_from_utc = int(time_string[
time_zone_string_index + 4:time_zone_string_index + 6])
except ValueError:
raise ValueError('Unable to parse time zone minutes offset.')
if minutes_from_utc not in range(0, 60):
raise ValueError('Time zone minutes offset value out of bounds.')
# pylint: disable=invalid-unary-operand-type
time_zone_offset = (hours_from_utc * 60) + minutes_from_utc
# Note that when the sign of the time zone offset is negative
# the difference needs to be added. We do so by flipping the sign.
if time_string[time_zone_string_index] != '-':
time_zone_offset = -time_zone_offset
return hours, minutes, seconds, microseconds, time_zone_offset
def _GetDateValues(
self, number_of_days, epoch_year, epoch_month, epoch_day_of_month):
"""Determines date values.
Args:
number_of_days (int): number of days since epoch.
epoch_year (int): year that is the start of the epoch.
epoch_month (int): month that is the start of the epoch.
epoch_day_of_month (int): day of month that is the start of the epoch.
Return:
tuple[int, int, int]: year, month, day of month.
Raises:
ValueError: if the epoch year, month or day of month values are out
of bounds.
"""
if epoch_year < 0:
raise ValueError('Epoch year value out of bounds.')
if epoch_month not in range(1, 13):
raise ValueError('Epoch month value out of bounds.')
epoch_days_per_month = self._GetDaysPerMonth(epoch_year, epoch_month)
if epoch_day_of_month < 1 or epoch_day_of_month > epoch_days_per_month:
raise ValueError('Epoch day of month value out of bounds.')
before_epoch = number_of_days < 0
year = epoch_year
month = epoch_month
if before_epoch:
month -= 1
if month <= 0:
month = 12
year -= 1
number_of_days += epoch_day_of_month
if before_epoch:
number_of_days *= -1
# Align with the start of the year.
while month > 1:
days_per_month = self._GetDaysPerMonth(year, month)
if number_of_days < days_per_month:
break
if before_epoch:
month -= 1
else:
month += 1
if month > 12:
month = 1
year += 1
number_of_days -= days_per_month
# Align with the start of the next century.
_, remainder = divmod(year, 100)
for _ in range(remainder, 100):
days_in_year = self._GetNumberOfDaysInYear(year)
if number_of_days < days_in_year:
break
if before_epoch:
year -= 1
else:
year += 1
number_of_days -= days_in_year
days_in_century = self._GetNumberOfDaysInCentury(year)
while number_of_days > days_in_century:
if before_epoch:
year -= 100
else:
year += 100
number_of_days -= days_in_century
days_in_century = self._GetNumberOfDaysInCentury(year)
days_in_year = self._GetNumberOfDaysInYear(year)
while number_of_days > days_in_year:
if before_epoch:
year -= 1
else:
year += 1
number_of_days -= days_in_year
days_in_year = self._GetNumberOfDaysInYear(year)
days_per_month = self._GetDaysPerMonth(year, month)
while number_of_days > days_per_month:
if before_epoch:
month -= 1
else:
month += 1
if month <= 0:
month = 12
year -= 1
elif month > 12:
month = 1
year += 1
number_of_days -= days_per_month
days_per_month = self._GetDaysPerMonth(year, month)
if before_epoch:
days_per_month = self._GetDaysPerMonth(year, month)
number_of_days = days_per_month - number_of_days
return year, month, number_of_days
def _GetDayOfYear(self, year, month, day_of_month):
"""Retrieves the day of the year for a specific day of a month in a year.
Args:
year (int): year e.g. 1970.
month (int): month where 1 represents January.
day_of_month (int): day of the month where 1 represents the first day.
Returns:
int: day of year.
Raises:
ValueError: if the month or day of month value is out of bounds.
"""
if month not in range(1, 13):
raise ValueError('Month value out of bounds.')
days_per_month = self._GetDaysPerMonth(year, month)
if day_of_month < 1 or day_of_month > days_per_month:
raise ValueError('Day of month value out of bounds.')
day_of_year = day_of_month
for past_month in range(1, month):
day_of_year += self._GetDaysPerMonth(year, past_month)
return day_of_year
def _GetDaysPerMonth(self, year, month):
"""Retrieves the number of days in a month of a specific year.
Args:
year (int): year e.g. 1970.
month (int): month ranging from 1 to 12.
Returns:
int: number of days in the month.
Raises:
ValueError: if the month value is out of bounds.
"""
if month not in range(1, 13):
raise ValueError('Month value out of bounds.')
days_per_month = self._DAYS_PER_MONTH[month - 1]
if month == 2 and self._IsLeapYear(year):
days_per_month += 1
return days_per_month
def _GetNumberOfDaysInCentury(self, year):
"""Retrieves the number of days in a century.
Args:
year (int): year in the century e.g. 1970.
Returns:
int: number of (remaining) days in the century.
Raises:
ValueError: if the year value is out of bounds.
"""
if year < 0:
raise ValueError('Year value out of bounds.')
year, _ = divmod(year, 100)
if self._IsLeapYear(year):
return 36525
return 36524
def _GetNumberOfDaysInYear(self, year):
"""Retrieves the number of days in a specific year.
Args:
year (int): year e.g. 1970.
Returns:
int: number of days in the year.
"""
if self._IsLeapYear(year):
return 366
return 365
def _GetNumberOfSecondsFromElements(
self, year, month, day_of_month, hours, minutes, seconds):
"""Retrieves the number of seconds from the date and time elements.
Args:
year (int): year e.g. 1970.
month (int): month of year.
day_of_month (int): day of month.
hours (int): hours.
minutes (int): minutes.
seconds (int): seconds.
Returns:
int: number of seconds since January 1, 1970 00:00:00 or None.
Raises:
ValueError: if the time elements are invalid.
"""
if not year or not month or not day_of_month:
return
# calendar.timegm does not sanity check the time elements.
if hours is None:
hours = 0
elif hours not in range(0, 24):
raise ValueError('Hours value: {0!s} out of bounds.'.format(hours))
if minutes is None:
minutes = 0
elif minutes not in range(0, 60):
raise ValueError('Minutes value: {0!s} out of bounds.'.format(minutes))
# TODO: support a leap second?
if seconds is None:
seconds = 0
elif seconds not in range(0, 60):
raise ValueError('Seconds value: {0!s} out of bounds.'.format(seconds))
# Note that calendar.timegm() does not raise when date is: 2013-02-29.
days_per_month = self._GetDaysPerMonth(year, month)
if day_of_month < 1 or day_of_month > days_per_month:
raise ValueError('Day of month value out of bounds.')
# calendar.timegm requires the time tuple to contain at least
# 6 integer values.
time_elements_tuple = (year, month, day_of_month, hours, minutes, seconds)
number_of_seconds = calendar.timegm(time_elements_tuple)
return int(number_of_seconds)
def _GetTimeValues(self, number_of_seconds):
"""Determines time values.
Args:
number_of_seconds (int): number of seconds.
Return:
tuple[int, int, int, int]: days, hours, minutes, seconds.
"""
number_of_minutes, seconds = divmod(number_of_seconds, 60)
number_of_hours, minutes = divmod(number_of_minutes, 60)
number_of_days, hours = divmod(number_of_hours, 24)
return number_of_days, hours, minutes, seconds
def _IsLeapYear(self, year):
"""Determines if a year is a leap year.
Args:
year (int): year e.g. 1970.
Returns:
bool: True if the year is a leap year.
"""
# pylint: disable=consider-using-ternary
return (year % 4 == 0 and year % 100 != 0) or year % 400 == 0
@decorators.deprecated
def CopyFromString(self, time_string):
"""Copies a date time value from a date and time string.
Args:
time_string (str): date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######[+-]##:##
Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The time of day, seconds
fraction and time zone offset are optional. The default time zone
is UTC.
Raises:
ValueError: if the time string is invalid or not supported.
"""
self.CopyFromDateTimeString(time_string)
@abc.abstractmethod
def CopyFromDateTimeString(self, time_string):
"""Copies a date time value from a date and time string.
Args:
time_string (str): date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######[+-]##:##
Where # are numeric digits ranging from 0 to 9 and the seconds
fraction can be either 3 or 6 digits. The time of day, seconds
fraction and time zone offset are optional. The default time zone
is UTC.
Raises:
ValueError: if the time string is invalid or not supported.
"""
@abc.abstractmethod
def CopyToStatTimeTuple(self):
"""Copies the date time value to a stat timestamp tuple.
Returns:
tuple[int, int]: a POSIX timestamp in seconds and the remainder in
100 nano seconds or (None, None) on error.
"""
@abc.abstractmethod
def CopyToDateTimeString(self):
"""Copies the date time value to a date and time string.
Returns:
str: date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######
"""
# TODO: remove this method when there is no more need for it in plaso.
@abc.abstractmethod
def GetPlasoTimestamp(self):
"""Retrieves a timestamp that is compatible with plaso.
Returns:
int: a POSIX timestamp in microseconds or None on error.
"""
|