示例#1
0
def _get_sharepoint_date_string(dt):
    """
    Using the ``nexusLIMS_timezone`` environment variable, convert a "naive"
    datetime object to a string with the proper offset to be correctly
    handled by the Sharepoint API. This timezone should be one listed as part
    of the
    `tz database <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones_>`.

    The reason this is necessary is that the Sharepoint calendar API uses UTC
    datetime, but displays them in the local timezone, so we need to convert
    our local datetime to UTC.  i.e. if you have an event that is
    displayed on the calendar as starting at 2019-07-24T00:00:00 (midnight on
    July 24th), an API query datetime greater than or equal to that time
    will not work unless you convert to UTC (2019-07-23T20:00:00.000)


    Parameters
    ----------
    dt : :py:class:`~datetime.datetime`
        The "naive" local timezone datetime object (i.e. as displayed in the
        sharepoint calendar)

    Returns
    -------
    dt_str : str
        The datetime formatted in ISO format, adjusted for the timezone
        offset (for Eastern time, that's four hours during DST and 5 hours in
        standard time)
    """
    if 'nexusLIMS_timezone' not in _os.environ:
        raise EnvironmentError('Please make sure the "nexusLIMS_timezone" '
                               'variable is set as part of your environment '
                               'before using this function')

    tz = _timezone(_os.environ['nexusLIMS_timezone'])
    dt_str = _pytz.utc.localize(dt).astimezone(tz).strftime(
        '%Y-%m-%dT%H:%M:%S')

    return dt_str
示例#2
0
 def get_timezone(self, zone):
     if isinstance(zone, string_t):
         return _timezone(zone)
     return zone
示例#3
0
文件: time.py 项目: zxjly/celery
 def get_timezone(self, zone):
     if isinstance(zone, string_t):
         return _timezone(zone)
     return zone
示例#4
0
 def get_timezone(self, zone):
     if isinstance(zone, basestring):
         return _timezone(zone)
     return zone
示例#5
0
    def from_xml(cls, xml):
        """
        Alternative constructor that allows parsing of an xml response from
        :py:func:`~.fetch_xml` rather than providing values directly

        Parameters
        ----------
        xml : str
            Output of an API query to the Sharepoint calendar that contains a
            single event (which should be the case if start and end times were
            provided to :py:func:`~.fetch_xml`)

        Returns
        -------
        cal_event : CalendarEvent or None
            An object representing an entry on the SharePoint calendar. Could
            be None if no entry is found within the provided XML
        """
        def _get_el_text(xpath):
            el = et.find(xpath, namespaces=et.nsmap)
            if el is None:
                return el
            else:
                return el.text

        et = _etree.fromstring(xml)
        if _get_el_text('entry') is None:
            # no "entry" nodes were found, so return None
            return None
        title = _get_el_text('entry//d:TitleOfExperiment')
        # get instrument from calendar title
        instrument = _get_el_text('title')
        if instrument is not None:
            instrument = _from_cal(instrument)
        sp_tz = _get_sharepoint_tz()
        updated = _get_el_text('entry/updated')
        if updated is not None:
            updated = _datetime.fromisoformat(updated)
        username = _get_el_text('entry/link[@title="UserName"]//d:UserName')
        created_by = _get_el_text('entry/link[@title="CreatedBy"]//d:UserName')
        start_time = _get_el_text('entry//d:StartTime')
        if start_time is not None:
            start_time = _timezone(sp_tz).localize(
                _datetime.fromisoformat(start_time))
        end_time = _get_el_text('entry//d:EndTime')
        if end_time is not None:
            end_time = _timezone(sp_tz).localize(
                _datetime.fromisoformat(end_time))
        category_value = _get_el_text('entry//d:CategoryValue')
        sample_details = _get_el_text('entry//d:SampleDetails')
        project_id = _get_el_text('entry//d:ProjectID')
        sharepoint_id = _get_el_text('entry/content//d:Id')
        if sharepoint_id is not None:
            sharepoint_id = int(sharepoint_id)

        return CalendarEvent(title=title,
                             instrument=instrument,
                             updated=updated,
                             username=username,
                             created_by=created_by,
                             start_time=start_time,
                             end_time=end_time,
                             category_value=category_value,
                             sample_details=sample_details,
                             project_id=project_id,
                             sharepoint_id=sharepoint_id)
示例#6
0
 def get_timezone(self, zone):
     if isinstance(zone, basestring):
         return _timezone(zone)
     return zone