我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用datetime.datetime.datetime()。
def __preptime(self,when): """ Extract information in a suitable format from when, a datetime.datetime object. """ # datetime days are numbered in the Gregorian calendar # while the calculations from NOAA are distibuted as # OpenOffice spreadsheets with days numbered from # 1/1/1900. The difference are those numbers taken for # 18/12/2010 self.day = when.toordinal()-(734124-40529) t=when.time() self.time= (t.hour + t.minute/60.0 + t.second/3600.0)/24.0 self.timezone=0 offset=when.utcoffset() if not offset is None: self.timezone=offset.seconds/3600.0
def to_utc_timestamp(date_time): """Convert a naive or timezone-aware datetime to UTC timestamp. Arguments: date_time (:py:class:`datetime.datetime`): The datetime to convert. Returns: :py:class:`int`: The timestamp (in seconds). """ if date_time is None: return if date_time.tzname() is None: timestamp = date_time.replace(tzinfo=timezone.utc).timestamp() else: timestamp = date_time.timestamp() return int(round(timestamp, 0))
def safe_parse(time): """Parse a string without throwing an error. Arguments: time (:py:class:`str`): The string to parse. Returns: :py:class:`datetime.datetime`: The parsed datetime. """ if time is None: return try: return parse(time) except (OverflowError, ValueError): pass
def occurred(at_): """Calculate when a service event occurred. Arguments: at_ (:py:class:`str`): When the event occurred. Returns: :py:class:`str`: The humanized occurrence time. """ try: occurred_at = parse(at_) except (TypeError, ValueError): logger.warning('failed to parse occurrence time %r', at_) return 'time not available' utc_now = datetime.now(tz=timezone.utc) try: return naturaltime((utc_now - occurred_at).total_seconds()) except TypeError: # at_ is a naive datetime return naturaltime((datetime.now() - occurred_at).total_seconds())
def convertToDate(fmt="%Y-%m-%d"): """ Helper to create a parse action for converting parsed date string to Python datetime.date Params - - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%d"}) Example:: date_expr = pyparsing_common.iso8601_date.copy() date_expr.setParseAction(pyparsing_common.convertToDate()) print(date_expr.parseString("1999-12-31")) prints:: [datetime.date(1999, 12, 31)] """ def cvt_fn(s,l,t): try: return datetime.strptime(t[0], fmt).date() except ValueError as ve: raise ParseException(s, l, str(ve)) return cvt_fn
def convertToDatetime(fmt="%Y-%m-%dT%H:%M:%S.%f"): """ Helper to create a parse action for converting parsed datetime string to Python datetime.datetime Params - - fmt - format to be passed to datetime.strptime (default=C{"%Y-%m-%dT%H:%M:%S.%f"}) Example:: dt_expr = pyparsing_common.iso8601_datetime.copy() dt_expr.setParseAction(pyparsing_common.convertToDatetime()) print(dt_expr.parseString("1999-12-31T23:59:59.999")) prints:: [datetime.datetime(1999, 12, 31, 23, 59, 59, 999000)] """ def cvt_fn(s,l,t): try: return datetime.strptime(t[0], fmt) except ValueError as ve: raise ParseException(s, l, str(ve)) return cvt_fn
def on_dt_changed(self, dt): """ Callback triggered by the simulation loop whenever the current dt changes. Any logic that should happen exactly once at the start of each datetime group should happen here. """ assert isinstance(dt, datetime), \ "Attempt to set algorithm's current time with non-datetime" assert dt.tzinfo == pytz.utc, \ "Algorithm expects a utc datetime" self.datetime = dt self.perf_tracker.set_date(dt) self.blotter.set_date(dt)
def add_history(self, bar_count, frequency, field, ffill=True): data_frequency = self.sim_params.data_frequency history_spec = HistorySpec(bar_count, frequency, field, ffill, data_frequency=data_frequency, env=self.trading_environment) self.history_specs[history_spec.key_str] = history_spec if self.initialized: if self.history_container: self.history_container.ensure_spec( history_spec, self.datetime, self._most_recent_data, ) else: self.history_container = self.history_container_class( self.history_specs, self.current_universe(), self.sim_params.first_open, self.sim_params.data_frequency, env=self.trading_environment, )
def parse_date(datestring, default_timezone=UTC): """Parses ISO 8601 dates into datetime objects The timezone is parsed from the date string. However it is quite common to have dates without a timezone (not strictly correct). In this case the default timezone specified in default_timezone is used. This is UTC by default. """ if not isinstance(datestring, _basestring): raise ParseError("Expecting a string %r" % datestring) m = ISO8601_REGEX.match(datestring) if not m: raise ParseError("Unable to parse date string %r" % datestring) groups = m.groupdict() tz = parse_timezone(groups["timezone"], default_timezone=default_timezone) if groups["fraction"] is None: groups["fraction"] = 0 else: groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6) return datetime(int(groups["year"]), int(groups["month"]), int(groups["day"]), int(groups["hour"]), int(groups["minute"]), int(groups["second"]), int(groups["fraction"]), tz)
def timestampUNIX(value, is_local): """ Convert an UNIX (32-bit) timestamp to datetime object. Timestamp value is the number of seconds since the 1st January 1970 at 00:00. Maximum value is 2147483647: 19 january 2038 at 03:14:07. May raise ValueError for invalid value: value have to be in 0..2147483647. >>> timestampUNIX(0, False) datetime.datetime(1970, 1, 1, 0, 0) >>> timestampUNIX(1154175644.37, False) datetime.datetime(2006, 7, 29, 12, 20, 44, 370000) """ timestamp = UNIX_TIMESTAMP_T0 + timedelta(seconds=value) if is_local: timestamp += LOCAL_TIMEZONE_OFFSET return timestamp
def _totimestamp(dt_obj): """ Args: dt_obj (:class:`datetime.datetime`): Returns: int: """ if not dt_obj: return None try: # Python 3.3+ return int(dt_obj.timestamp()) except AttributeError: # Python 3 (< 3.3) and Python 2 return int(mktime(dt_obj.timetuple()))
def template_localtime(value, use_tz=None): """ Checks if value is a datetime and converts it to local time if necessary. If use_tz is provided and is not None, that will force the value to be converted (or not), overriding the value of settings.USE_TZ. This function is designed for use by the template engine. """ should_convert = (isinstance(value, datetime) and (settings.USE_TZ if use_tz is None else use_tz) and not is_naive(value) and getattr(value, 'convert_to_local_time', True)) return localtime(value) if should_convert else value # Utilities
def localtime(value, timezone=None): """ Converts an aware datetime.datetime to local time. Local time is defined by the current time zone, unless another time zone is specified. """ if timezone is None: timezone = get_current_timezone() # If `value` is naive, astimezone() will raise a ValueError, # so we don't need to perform a redundant check. value = value.astimezone(timezone) if hasattr(timezone, 'normalize'): # This method is available for pytz time zones. value = timezone.normalize(value) return value
def query_hist_data( engine: object, sectype: str, symbol: str, datatype: str, barsize: str, start: datetime=None, end: datetime=None) -> MarketDataBlock: """Query database on conditions. """ if start is None: start = pytz.UTC.localize(datetime(1, 1, 1)) if end is None: end = pytz.UTC.localize(datetime(9999, 12, 31, 23, 59, 59)) table = _gen_sa_table(sectype) stmt = table.select().where( and_( table.c.Symbol == symbol, table.c.DataType == datatype, table.c.BarSize == barsize, table.c.TickerTime.between( start.astimezone(pytz.UTC), end.astimezone(pytz.UTC)) ) ) async with engine.acquire() as conn: result = await conn.execute(stmt) df = pd.DataFrame(list(result), columns=table.columns.keys()) blk = MarketDataBlock(df, tz='UTC') blk.tz_convert(start.tzinfo) return blk
def disambiguated(self, basedate): """Returns either a ``datetime`` or unambiguous ``timespan`` version of this object. Unless this ``adatetime`` object is full specified down to the microsecond, this method will return a timespan built from the "floor" and "ceil" of this object. This method raises an error if the ``adatetime`` object has no year. >>> adt = adatetime(year=2009, month=10, day=31) >>> adt.disambiguated() timespan(datetime(2009, 10, 31, 0, 0, 0, 0), datetime(2009, 10, 31, 23, 59 ,59, 999999) """ dt = self if not is_ambiguous(dt): return fix(dt) return timespan(dt, dt).disambiguated(basedate) # Time span class
def __deserialize_datatime(self, string): """ Deserializes string to datetime. The string should be in iso8601 datetime format. :param string: str. :return: datetime. """ try: from dateutil.parser import parse return parse(string) except ImportError: return string except ValueError: raise ApiException( status=0, reason=( "Failed to parse `{0}` into a datetime object" .format(string) ) )
def _DATETIME_to_python(self, value, dsc=None): """Connector/Python always returns naive datetime.datetime Connector/Python always returns naive timestamps since MySQL has no time zone support. Since Django needs non-naive, we need to add the UTC time zone. Returns datetime.datetime() """ if not value: return None dt = MySQLConverter._DATETIME_to_python(self, value) if dt is None: return None if settings.USE_TZ and timezone.is_naive(dt): dt = dt.replace(tzinfo=timezone.utc) return dt
def estimate_time(builds): """Update the working build with an estimated completion time. Takes a simple average over the previous builds, using those whose outcome is ``'passed'``. Arguments: builds (:py:class:`list`): All builds. """ try: index, current = next( (index, build) for index, build in enumerate(builds[:4]) if build['outcome'] == 'working' ) except StopIteration: return # no in-progress builds if current.get('started_at') is None: current['elapsed'] = 'estimate not available' return usable = [ current for current in builds[index + 1:] if current['outcome'] == 'passed' and current['duration'] is not None ] if not usable: current['elapsed'] = 'estimate not available' return average_duration = int(sum(build['duration'] for build in usable) / float(len(usable))) finish = current['started_at'] + average_duration remaining = (datetime.fromtimestamp(finish) - datetime.now()).total_seconds() if remaining >= 0: current['elapsed'] = '{} left'.format(naturaldelta(remaining)) else: current['elapsed'] = 'nearly done'
def handle_data(self, data): self._most_recent_data = data if self.history_container: self.history_container.update(data, self.datetime) self._handle_data(self, data) # Unlike trading controls which remain constant unless placing an # order, account controls can change each bar. Thus, must check # every bar no matter if the algorithm places an order or not. self.validate_account_controls()
def future_chain(self, root_symbol, as_of_date=None): """ Look up a future chain with the specified parameters. Parameters ---------- root_symbol : str The root symbol of a future chain. as_of_date : datetime.datetime or pandas.Timestamp or str, optional Date at which the chain determination is rooted. I.e. the existing contract whose notice date is first after this date is the primary contract, etc. Returns ------- FutureChain The future chain matching the specified parameters. Raises ------ RootSymbolNotFound If a future chain could not be found for the given root symbol. """ if as_of_date: try: as_of_date = pd.Timestamp(as_of_date, tz='UTC') except ValueError: raise UnsupportedDatetimeFormat(input=as_of_date, method='future_chain') return FutureChain( asset_finder=self.asset_finder, get_datetime=self.get_datetime, root_symbol=root_symbol, as_of_date=as_of_date )
def get_datetime(self, tz=None): """ Returns the simulation datetime. """ dt = self.datetime assert dt.tzinfo == pytz.utc, "Algorithm should have a utc datetime" if tz is not None: # Convert to the given timezone passed as a string or tzinfo. if isinstance(tz, string_types): tz = pytz.timezone(tz) dt = dt.astimezone(tz) return dt # datetime.datetime objects are immutable.