* Adeodato Simó [Sat, 24 Jun 2006 19:42:36 +0200]:

Heya,

> My plan now is to go through the code, and change source backends to set
> the timezone, and (if necessary) fix those target backends that had
> workarounds in place for the lack of such info (eg., bzr.py after
> applying my patch -- thankfully reverting that one won't be necessary. :)

Okay, see the attached patch. It fixes backends to properly set the
timezone when acting as source, and to use it when acting as target,
plus makes Changeset.__init__ default to UTC.

It is complete for: svn, bzr, darcs, and hg.

I think the result is more robust, so I hope you consider it for
inclusion (suggestions welcome, too). If you ack it, then I'll allocate
more time to fix the rest of backends, if nobody more familiar with them
steps up to do it, and will send a darcs changeset when it's complete.

After that, implementing the 'timezone' option should be fairly easy.

Cheers,

-- 
Adeodato Simó                                     dato at net.com.org.es
Debian Developer                                  adeodato at debian.org
 
                                Listening to: David Bowie - The dreamers
# vi: ft=diff
revno: 3
committer: Adeodato Simó <[EMAIL PROTECTED]>
branch nick: tailor
timestamp: Sun 2006-06-25 18:50:51 +0200
message:
  Merge date.tzinfo updates.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 12:00:30 +0200
    message:
      Add tzinfo.py copied from pytz sources, providing UTC and FixedOffset
      timezones.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 12:06:24 +0200
    message:
      Changeset.__init__:
        - ensure date is non-naive, by setting date.tzinfo to UTC if None.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 12:11:40 +0200
    message:
      SvnXMLLogHandler.endElement:
        - set tzinfo to UTC in the created timestamp.
      
      SvnWorkingDir._commit:
        - convert date to UTC from whichever timezone before feeding it to
          propset.execute(). Force it to naive and ms=0 so that isoformat() 
          gives the expected results.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 12:17:53 +0200
    message:
      BzrWorkingDir._changesetFromRevision:
        - set date.tzset from bzr revision's timezone if available, or to UTC
          otherwise.
      
      BzrWorkingDir._commit:
        - calculate seconds since epotch in UTC using calendar.timegm()
          instead of time.mktime(), since the latter is local-timezone aware.
        - use date.utcoffset() to give bzr information about the timezone.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 15:52:20 +0200
    message:
      DarcsXMLChangesHandler.startElement:
        - set timestamp.tzinfo to UTC.
      
      DarcsWorkingDir._commit:
        - transform date to UTC before using it.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 18:04:53 +0200
    message:
      Merge tailor updates.
    ------------------------------------------------------------
    merged: [EMAIL PROTECTED]
    committer: Adeodato Simó <[EMAIL PROTECTED]>
    branch nick: tailor.bzr
    timestamp: Sun 2006-06-25 18:25:35 +0200
    message:
      HgWorkingDir._changesetForRevision:
        - use the timezone offset to assign a proper FixedOffset tzinfo to the
          changeset date.
      
      HgWorkingDir._commit:
        - calculate seconds since epoch in UTC using calendar.timegm()
          instead of time.mktime(), since the latter is local-timezone aware.
        - use date.utcoffset() to give hg information about the timezone.

=== modified file 'vcpx/changes.py'
--- vcpx/changes.py     
+++ vcpx/changes.py     
@@ -51,6 +51,7 @@
 
 
 from textwrap import TextWrapper
+from tzinfo import UTC
 from re import compile, MULTILINE
 
 itemize_re = compile('^[ ]*[-*] ', MULTILINE)
@@ -104,6 +105,10 @@
         """
         Initialize a new Changeset.
         """
+
+        if date.tzinfo is None:
+            # print "Warning: changeset created with naive datetime"
+            date = date.replace(tzinfo=UTC)
 
         self.revision = revision
         self.date = date
=== modified file 'vcpx/repository/svn.py'
--- vcpx/repository/svn.py      
+++ vcpx/repository/svn.py      
@@ -16,6 +16,7 @@
 from vcpx.source import UpdatableSourceWorkingDir, ChangesetApplicationFailure
 from vcpx.target import SynchronizableTargetWorkingDir, 
TargetInitializationFailure
 from vcpx.config import ConfigurationError
+from vcpx.tzinfo import UTC
 
 
 class SvnRepository(Repository):
@@ -207,7 +208,7 @@
                 y,m,d = map(int, svndate[:10].split('-'))
                 hh,mm,ss = map(int, svndate[11:19].split(':'))
                 ms = int(svndate[20:-1])
-                timestamp = datetime(y, m, d, hh, mm, ss, ms)
+                timestamp = datetime(y, m, d, hh, mm, ss, ms, UTC)
 
                 changeset = Changeset(self.current['revision'],
                                       timestamp,
@@ -528,6 +529,7 @@
                                           "--revision", revision)
             propset = ExternalCommand(cwd=self.basedir, command=cmd)
 
+            date = date.astimezone(UTC).replace(microsecond = 0, tzinfo=None)
             propset.execute(date.isoformat()+".000000Z", propname='svn:date')
             propset.execute(encode(author), propname='svn:author')
 
=== modified file 'vcpx/repository/bzr.py'
--- vcpx/repository/bzr.py      
+++ vcpx/repository/bzr.py      
@@ -64,6 +64,7 @@
         """
         from datetime import datetime
         from vcpx.changes import ChangesetEntry, Changeset
+        from vcpx.tzinfo import FixedOffset, UTC
 
         revision = branch.repository.get_revision(revision_id)
         deltatree = 
branch.get_revision_delta(branch.revision_id_to_revno(revision_id))
@@ -90,8 +91,13 @@
             e.action_kind = ChangesetEntry.UPDATED
             entries.append(e)
 
+        if revision.timezone is not None:
+            timezone = FixedOffset(revision.timezone / 60)
+        else:
+            timezone = UTC
+
         return Changeset(revision.revision_id,
-                         datetime.fromtimestamp(revision.timestamp),
+                         datetime.fromtimestamp(revision.timestamp, timezone),
                          revision.committer,
                          revision.message,
                          entries)
@@ -201,7 +207,7 @@
         """
         Commit the changeset.
         """
-        from time import mktime
+        from calendar import timegm  # like mktime(), but returns UTC timestamp
         from binascii import hexlify
         from re import search
         from bzrlib.osutils import compact_date, rand_bytes
@@ -217,7 +223,9 @@
         else:
             self.log.info('Committing...')
             logmessage = "Empty changelog"
-        timestamp = int(mktime(date.timetuple()))
+
+        timestamp = timegm(date.utctimetuple())
+        timezone  = date.utcoffset().seconds + date.utcoffset().days * 24 * 
3600
 
         # Guess sane email address
         email = search("<([EMAIL PROTECTED])>", author)
@@ -237,7 +245,7 @@
         self._working_tree.commit(logmessage, committer=author,
                                   specific_files=entries, rev_id=revision_id,
                                   verbose=self.repository.projectref().verbose,
-                                  timestamp=timestamp)
+                                  timestamp=timestamp, timezone=timezone)
 
     def _removePathnames(self, names):
         """

=== modified file 'vcpx/repository/darcs.py'
--- vcpx/repository/darcs.py    
+++ vcpx/repository/darcs.py    
@@ -18,6 +18,7 @@
 from vcpx.source import UpdatableSourceWorkingDir, 
ChangesetApplicationFailure, \
                         GetUpstreamChangesetsFailure
 from vcpx.target import SynchronizableTargetWorkingDir, 
TargetInitializationFailure
+from vcpx.tzinfo import UTC
 
 
 MOTD = """\
@@ -100,6 +101,9 @@
                 except ValueError:
                     # Old darcs patches use the form Sun Oct 20 20:01:05 EDT 
2002
                     timestamp = datetime(*strptime(date[:19] + date[-5:], '%a 
%b %d %H:%M:%S %Y')[:6])
+
+                timestamp = timestamp.replace(tzinfo=UTC) # not true for the 
ValueError case, but oh well
+
                 self.current['date'] = timestamp
                 self.current['comment'] = ''
                 self.current['hash'] = attributes['hash']
@@ -510,7 +514,7 @@
 
         logmessage = []
 
-        logmessage.append(date.strftime('%Y/%m/%d %H:%M:%S UTC'))
+        logmessage.append(date.astimezone(UTC).strftime('%Y/%m/%d %H:%M:%S 
UTC'))
         logmessage.append(author)
         if patchname:
             logmessage.append(patchname)

=== modified file 'vcpx/repository/hg.py'
--- vcpx/repository/hg.py       
+++ vcpx/repository/hg.py       
@@ -121,16 +121,15 @@
     def _changesetForRevision(self, repo, revision):
         from datetime import datetime
         from vcpx.changes import Changeset, ChangesetEntry
+        from vcpx.tzinfo import FixedOffset
 
         entries = []
         node = self._getNode(repo, revision)
         parents = repo.changelog.parents(node)
         (manifest, user, date, files, message) = repo.changelog.read(node)
 
-        # Different targets seem to handle the TZ differently. It looks like
-        # darcs may be the most correct.
         dt, tz = date
-        date = datetime.fromtimestamp(int(dt) + int(tz))
+        date = datetime.fromtimestamp(dt, FixedOffset(-tz/60)) # note the 
minus!
 
         manifest = repo.manifest.read(manifest)
 
@@ -251,7 +250,7 @@
             self._hg.add(notdirs)
 
     def _commit(self, date, author, patchname, changelog=None, names=[]):
-        from time import mktime
+        from calendar import timegm  # like mktime(), but returns UTC timestamp
 
         encode = self.repository.encode
 
@@ -266,10 +265,14 @@
         else:
             self.log.info('Committing...')
             logmessage = "Empty changelog"
+
+        timestamp = timegm(date.utctimetuple())
+        timezone  = date.utcoffset().seconds + date.utcoffset().days * 24 * 
3600
+
         opts = {}
         opts['message'] = logmessage
         opts['user'] = encode(author)
-        opts['date'] =  '%d 0' % mktime(date.timetuple())
+        opts['date'] =  '%d %d' % (timestamp, -timezone) # note the minus sign!
         self._hgCommand('commit', *[encode(n) for n in names], **opts)
 
     def _tag(self, tag):

=== added file 'vcpx/tzinfo.py'
--- /dev/null   
+++ vcpx/tzinfo.py      
@@ -0,0 +1,201 @@
+# -*- mode: python; coding: utf-8 -*-
+#
+# All classes in this file are taken from the pytz sources, file
+# pytz/__init__.py. URL:<http://pytz.sourceforge.net>
+
+# Local changes:
+#
+# 2006-06-25
+# _FixedOffset.dst: return timedelta(0) instead of None, otherwise
+# datetime() won't consider the tz valid.
+
+# Copyright (c) 2003-2005 Stuart Bishop <[EMAIL PROTECTED]>
+# 
+# Permission is hereby granted, free of charge, to any person obtaining a
+# copy of this software and associated documentation files (the "Software"),
+# to deal in the Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish, distribute, sublicense,
+# and/or sell copies of the Software, and to permit persons to whom the
+# Software is furnished to do so, subject to the following conditions:
+# 
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+# 
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+# DEALINGS IN THE SOFTWARE.
+
+
+import datetime
+
+ZERO = datetime.timedelta(0)
+HOUR = datetime.timedelta(hours=1)
+
+###
+
+class UTC(datetime.tzinfo):
+    """UTC
+    
+    Identical to the reference UTC implementation given in Python docs except
+    that it unpickles using the single module global instance defined beneath
+    this class declaration.
+
+    Also contains extra attributes and methods to match other pytz tzinfo
+    instances.
+    """
+    zone = "UTC"
+
+    def utcoffset(self, dt):
+        return ZERO
+
+    def tzname(self, dt):
+        return "UTC"
+
+    def dst(self, dt):
+        return ZERO
+    
+    def __reduce__(self):
+        return _UTC, ()
+
+    def localize(self, dt, is_dst=False):
+        '''Convert naive time to local time'''
+        if dt.tzinfo is not None:
+            raise ValueError, 'Not naive datetime (tzinfo is already set)'
+        return dt.replace(tzinfo=self)
+
+    def normalize(self, dt, is_dst=False):
+        '''Correct the timezone information on the given datetime'''
+        if dt.tzinfo is None:
+            raise ValueError, 'Naive time - no tzinfo set'
+        return dt.replace(tzinfo=self)
+
+    def __repr__(self):
+        return "<UTC>"
+
+    def __str__(self):
+        return "UTC"
+
+
+UTC = utc = UTC() # UTC is a singleton
+
+
+def _UTC():
+    """Factory function for utc unpickling.
+    
+    Makes sure that unpickling a utc instance always returns the same 
+    module global.
+    """
+    return utc
+
+_UTC.__safe_for_unpickling__ = True
+
+###
+
+# Time-zone info based solely on fixed offsets
+
+class _FixedOffset(datetime.tzinfo):
+
+    zone = None # to match the standard pytz API
+
+    def __init__(self, minutes):
+        if abs(minutes) >= 1440:
+            raise ValueError("absolute offset is too large", minutes)
+        self._minutes = minutes
+        self._offset = datetime.timedelta(minutes=minutes)
+
+    def utcoffset(self, dt):
+        return self._offset
+
+    def __reduce__(self):
+        return FixedOffset, (self._minutes, )
+
+    def dst(self, dt):
+        return ZERO
+    
+    def tzname(self, dt):
+        return None
+
+    def __repr__(self):
+        return 'pytz.FixedOffset(%d)' % self._minutes
+
+    def localize(self, dt, is_dst=False):
+        '''Convert naive time to local time'''
+        if dt.tzinfo is not None:
+            raise ValueError, 'Not naive datetime (tzinfo is already set)'
+        return dt.replace(tzinfo=self)
+
+    def normalize(self, dt, is_dst=False):
+        '''Correct the timezone information on the given datetime'''
+        if dt.tzinfo is None:
+            raise ValueError, 'Naive time - no tzinfo set'
+        return dt.replace(tzinfo=self)
+
+def FixedOffset(offset, _tzinfos = {}):
+    """return a fixed-offset timezone based off a number of minutes.
+    
+        >>> one = FixedOffset(-330)
+        >>> one
+        pytz.FixedOffset(-330)
+        >>> one.utcoffset(datetime.datetime.now())
+        datetime.timedelta(-1, 66600)
+
+        >>> two = FixedOffset(1380)
+        >>> two
+        pytz.FixedOffset(1380)
+        >>> two.utcoffset(datetime.datetime.now())
+        datetime.timedelta(0, 82800)
+    
+    The datetime.timedelta must be between the range of -1 and 1 day,
+    non-inclusive.
+
+        >>> FixedOffset(1440)
+        Traceback (most recent call last):
+        ...
+        ValueError: ('absolute offset is too large', 1440)
+
+        >>> FixedOffset(-1440)
+        Traceback (most recent call last):
+        ...
+        ValueError: ('absolute offset is too large', -1440)
+
+    An offset of 0 is special-cased to return UTC.
+
+        >>> FixedOffset(0) is UTC
+        True
+
+    There should always be only one instance of a FixedOffset per timedelta.
+    This should be true for multiple creation calls.
+    
+        >>> FixedOffset(-330) is one
+        True
+        >>> FixedOffset(1380) is two
+        True
+
+    It should also be true for pickling.
+
+        >>> import pickle
+        >>> pickle.loads(pickle.dumps(one)) is one
+        True
+        >>> pickle.loads(pickle.dumps(two)) is two
+        True
+
+    """
+
+    if offset == 0:
+        return UTC
+
+    info = _tzinfos.get(offset)
+    if info is None:
+        # We haven't seen this one before. we need to save it.
+
+        # Use setdefault to avoid a race condition and make sure we have
+        # only one
+        info = _tzinfos.setdefault(offset, _FixedOffset(offset))
+
+    return info
+
+FixedOffset.__safe_for_unpickling__ = True

_______________________________________________
Tailor mailing list
[email protected]
http://lists.zooko.com/mailman/listinfo/tailor

Reply via email to