/usr/lib/python2.7/dist-packages/dfdatetime/precisions.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 | # -*- coding: utf-8 -*-
"""Date and time precision helpers."""
from __future__ import unicode_literals
from dfdatetime import definitions
class DateTimePrecisionHelper(object):
"""Date time precision helper interface.
This is the super class of different date and time precision helpers.
Time precision helpers provide functionality for converting date and time
values between different precisions.
"""
@classmethod
def CopyMicrosecondsToFractionOfSecond(cls, microseconds):
"""Copies the number of microseconds to a fraction of second value.
Args:
microseconds (int): number of microseconds.
Returns:
float: fraction of second, which must be a value between 0.0 and 1.0.
"""
raise NotImplementedError()
@classmethod
def CopyToDateTimeString(cls, time_elements_tuple, fraction_of_second):
"""Copies the time elements and fraction of second to a string.
Args:
time_elements_tuple (tuple[int, int, int, int, int, int]):
time elements, contains year, month, day of month, hours, minutes and
seconds.
fraction_of_second (float): fraction of second, which must be a value
between 0.0 and 1.0.
Returns:
str: date and time value formatted as: YYYY-MM-DD hh:mm:ss with fraction
of second part that corresponds to the precision.
"""
raise NotImplementedError()
class SecondsPrecisionHelper(DateTimePrecisionHelper):
"""Seconds precision helper."""
@classmethod
def CopyMicrosecondsToFractionOfSecond(cls, microseconds):
"""Copies the number of microseconds to a fraction of second value.
Args:
microseconds (int): number of microseconds.
Returns:
float: fraction of second, which must be a value between 0.0 and 1.0.
For the seconds precision helper this will always be 0.0.
Raises:
ValueError: if the number of microseconds is out of bounds.
"""
if microseconds < 0 or microseconds >= definitions.MICROSECONDS_PER_SECOND:
raise ValueError(
'Number of microseconds value: {0:d} out of bounds.'.format(
microseconds))
return 0.0
@classmethod
def CopyToDateTimeString(cls, time_elements_tuple, fraction_of_second):
"""Copies the time elements and fraction of second to a string.
Args:
time_elements_tuple (tuple[int, int, int, int, int, int]):
time elements, contains year, month, day of month, hours, minutes and
seconds.
fraction_of_second (float): fraction of second, which must be a value
between 0.0 and 1.0.
Returns:
str: date and time value formatted as:
YYYY-MM-DD hh:mm:ss
Raises:
ValueError: if the fraction of second is out of bounds.
"""
if fraction_of_second < 0.0 or fraction_of_second >= 1.0:
raise ValueError('Fraction of second value: {0:f} out of bounds.'.format(
fraction_of_second))
return '{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'.format(
time_elements_tuple[0], time_elements_tuple[1], time_elements_tuple[2],
time_elements_tuple[3], time_elements_tuple[4], time_elements_tuple[5])
class MillisecondsPrecisionHelper(DateTimePrecisionHelper):
"""Milliseconds precision helper."""
@classmethod
def CopyMicrosecondsToFractionOfSecond(cls, microseconds):
"""Copies the number of microseconds to a fraction of second value.
Args:
microseconds (int): number of microseconds.
Returns:
float: fraction of second, which must be a value between 0.0 and 1.0.
Raises:
ValueError: if the number of microseconds is out of bounds.
"""
if microseconds < 0 or microseconds >= definitions.MICROSECONDS_PER_SECOND:
raise ValueError(
'Number of microseconds value: {0:d} out of bounds.'.format(
microseconds))
milliseconds, _ = divmod(
microseconds, definitions.MICROSECONDS_PER_MILLISECOND)
return float(milliseconds) / definitions.MILLISECONDS_PER_SECOND
@classmethod
def CopyToDateTimeString(cls, time_elements_tuple, fraction_of_second):
"""Copies the time elements and fraction of second to a string.
Args:
time_elements_tuple (tuple[int, int, int, int, int, int]):
time elements, contains year, month, day of month, hours, minutes and
seconds.
fraction_of_second (float): fraction of second, which must be a value
between 0.0 and 1.0.
Returns:
str: date and time value formatted as:
YYYY-MM-DD hh:mm:ss.###
Raises:
ValueError: if the fraction of second is out of bounds.
"""
if fraction_of_second < 0.0 or fraction_of_second >= 1.0:
raise ValueError('Fraction of second value: {0:f} out of bounds.'.format(
fraction_of_second))
milliseconds = int(fraction_of_second * definitions.MILLISECONDS_PER_SECOND)
return '{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}.{6:03d}'.format(
time_elements_tuple[0], time_elements_tuple[1], time_elements_tuple[2],
time_elements_tuple[3], time_elements_tuple[4], time_elements_tuple[5],
milliseconds)
class MicrosecondsPrecisionHelper(DateTimePrecisionHelper):
"""Microseconds precision helper."""
@classmethod
def CopyMicrosecondsToFractionOfSecond(cls, microseconds):
"""Copies the number of microseconds to a fraction of second value.
Args:
microseconds (int): number of microseconds.
Returns:
float: fraction of second, which must be a value between 0.0 and 1.0.
Raises:
ValueError: if the number of microseconds is out of bounds.
"""
if microseconds < 0 or microseconds >= definitions.MICROSECONDS_PER_SECOND:
raise ValueError(
'Number of microseconds value: {0:d} out of bounds.'.format(
microseconds))
return float(microseconds) / definitions.MICROSECONDS_PER_SECOND
@classmethod
def CopyToDateTimeString(cls, time_elements_tuple, fraction_of_second):
"""Copies the time elements and fraction of second to a string.
Args:
time_elements_tuple (tuple[int, int, int, int, int, int]):
time elements, contains year, month, day of month, hours, minutes and
seconds.
fraction_of_second (float): fraction of second, which must be a value
between 0.0 and 1.0.
Returns:
str: date and time value formatted as:
YYYY-MM-DD hh:mm:ss.######
Raises:
ValueError: if the fraction of second is out of bounds.
"""
if fraction_of_second < 0.0 or fraction_of_second >= 1.0:
raise ValueError('Fraction of second value: {0:f} out of bounds.'.format(
fraction_of_second))
microseconds = int(fraction_of_second * definitions.MICROSECONDS_PER_SECOND)
return '{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}.{6:06d}'.format(
time_elements_tuple[0], time_elements_tuple[1], time_elements_tuple[2],
time_elements_tuple[3], time_elements_tuple[4], time_elements_tuple[5],
microseconds)
class PrecisionHelperFactory(object):
"""Date time precision helper factory."""
_PRECISION_CLASSES = {
definitions.PRECISION_1_MICROSECOND: MicrosecondsPrecisionHelper,
definitions.PRECISION_1_MILLISECOND: MillisecondsPrecisionHelper,
definitions.PRECISION_1_SECOND: SecondsPrecisionHelper,
}
@classmethod
def CreatePrecisionHelper(cls, precision):
"""Creates a precision helper.
Args:
precision (str): precision of the date and time value, which should
be one of the PRECISION_VALUES in definitions.
Returns:
class: date time precision helper class.
Raises:
ValueError: if the precision value is unsupported.
"""
precision_helper_class = cls._PRECISION_CLASSES.get(precision, None)
if not precision_helper_class:
raise ValueError('Unsupported precision: {0!s}'.format(precision))
return precision_helper_class
|