[Tutor] datetime, time zones, and ISO time

David Perlman dperlman at wisc.edu
Wed Feb 17 21:12:42 CET 2010


Yeah, I got this part.  The thing that's hanging me up is that there  
doesn't seem to be any way to get a tzinfo instance that contains the  
current local time zone information.  You can do time.timezone to get  
the seconds from UTC, but there doesn't seem to be any way to convert  
that into a tzinfo!  I would be perfectly satisfied with this:

tz=offset_to_tzinfo(time.timezone) # or whatever
aware_now=datetime.datetime.now(tz)
print aware_now.isoformat()

I'm pretty sure that would give me what I want, but there doesn't seem  
to be any way to do step one without subclassing tzinfo.  This makes  
me feel like I MUST be missing something obvious, because it shouldn't  
require so much coding just to find out what the current local time  
and timezone is!


On Feb 17, 2010, at 1:42 PM, William Witteman wrote:

> On Wed, Feb 17, 2010 at 12:44:02PM -0600, David Perlman wrote:
>> I have been really scratching my head over this, it seems like there
>> *should* be a nice easy way to do what I want but I can't find it for
>> the life of me.
> ...
>> But a) I don't know how to stick the offset info into a datetime
>> object, and the documentation doesn't seem to say anything about
>> this; and b) the offset line doesn't work anyway:
>
> I think that you need to push in a tzinfo object, rather than a value:
>
> http://docs.python.org/library/datetime.html#datetime.tzinfo
>
> I get that from here:
>
> For applications requiring more, datetime  and time objects have an
> optional time zone information member, tzinfo, that can contain an
> instance of a subclass of the abstract tzinfo class. These tzinfo
> objects capture information about the offset from UTC time, the time
> zone name, and whether Daylight Saving Time is in effect. Note that no
> concrete tzinfo classes are supplied by the datetime module.  
> Supporting
> timezones at whatever level of detail is required is up to the
> application. The rules for time adjustment across the world are more
> political than rational, and there is no standard suitable for every
> application.[1]
>
> I suspect that it'll take some fooling around to see how it works  
> though
> - use the interpreter or ipython to test things out.
>
> [1] http://docs.python.org/library/datetime.html
> -- 
>
> yours,
>
> William
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

--
-dave----------------------------------------------------------------
"Pseudo-colored pictures of a person's brain lighting up are
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph.  That could be a big problem if the goal is to get to the
truth."  -Dr. Steven Hyman, Harvard





More information about the Tutor mailing list