Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Python datetime to int for db
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Christopher Nelson  
View profile  
 More options Sep 18 2012, 1:52 pm
From: Christopher Nelson <chris.nelson.1...@gmail.com>
Date: Tue, 18 Sep 2012 13:52:20 -0400
Local: Tues, Sep 18 2012 1:52 pm
Subject: Re: [Trac-dev] Python datetime to int for db

>> If I want to do:

>>   d = datetime.now()
>>   cursor.execute("INSERT INTO mytable VALUES", somefunc(d))

>> what is `somefunc()`

> Well, I think you're looking for to_utimestamp:

> http://www.edgewall.org/docs/branches-1.0-stable/html/api/trac_util_d...

I can't make that work.  I have:

        self.env.log.debug('utimestamp tests')
        ds = '2012-02-01'
        try:
            self.env.log.debug('a. ds:%s' % ds)
            d = datetime(*time.strptime(ds, '%Y-%m-%d')[0:7])
            self.env.log.debug('b. d:%s' % d)
            d = d.replace(hour=0, minute=0, second=0, microsecond=0)
            self.env.log.debug('c. d:%s' % d)
            d = to_datetime(d)
            self.env.log.debug('d. d:%s' % d)
            uts = to_utimestamp(d)
            self.env.log.debug('1. ds:%s -> uts:%s' % (ds, uts))
        except:
            self.env.log.debug('My way failed')

        d = parse_date(ds)
        uts = to_utimestamp(d)
        self.env.log.debug('2. ds:%s -> uts:%s' % (ds, uts))

and I see:

  2012-09-18 13:48:00,442 Trac[tracpm] DEBUG: utimestamp tests
  2012-09-18 13:48:00,442 Trac[tracpm] DEBUG: a. ds:2012-02-01
  2012-09-18 13:48:00,445 Trac[tracpm] DEBUG: b. d:2012-02-01 00:00:00.000002
  2012-09-18 13:48:00,445 Trac[tracpm] DEBUG: c. d:2012-02-01 00:00:00
  2012-09-18 13:48:00,445 Trac[tracpm] DEBUG: d. d:2012-02-01 00:00:00
  2012-09-18 13:48:00,446 Trac[tracpm] DEBUG: My way failed
  2012-09-18 13:48:00,447 Trac[tracpm] DEBUG: 2. ds:2012-02-01 ->
uts:1328072400000000

In 0.11 parse_date() seems to assume ISO format which isn't really an
option for me.  users can configure their own format to match their
data.  So, how can I parse a date string into something that
to_utimestamp() will accept?  (I'm more than willing to accept that my
parsing is awkward.  It's evolved painfully and may be Just Wrong.
Any wisdom on cleaning it up is appreciated.)

                                                   Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.