datetime question

Ferrous Cranus nikos.gr33k at gmail.com
Tue Nov 12 14:32:48 CET 2013


Στις 12/11/2013 2:47 μμ, ο/η Andy Lawton έγραψε:
> Firstly , I should clarify I have no idea how to program python, I
> joined this mailing list in anticipation of learning soon. And
> thought I'd have a go playing around with your code and code given to
> you (worst possible place to start, I'm sure)
>
> But from the answers already given to you, this seems to work and as you
> requested, in reality, this keeps your code to 1 line...
>
> I'm sure this is wrong, and isn't what you want, but hey, Correct
> working code doesn't seem to matter to you anyway...
>
> (I think "Europe/Kiev" is Greece but I don't know)
>
> from datetime import datetime, time, timedelta
> import time
> import pytz
>
> def is_dst(zonename):
>      tz = pytz.timezone(zonename)
>      now = pytz.utc.localize(datetime.utcnow())
>      return now.astimezone(tz).dst() != timedelta(0)
>
> def dst_greece():
>      if is_dst("Europe/Kiev") :
>          diff = 2
>      else:
>          diff = 3
>      return diff
>
> lastvisit = (datetime.utcnow() +timedelta(hours=dst_greece())).strftime(
> '%y-%m-%d %H:%M:%S' )
>
>
> On 12 November 2013 10:15, Ferrous Cranus <nikos.gr33k at gmail.com
> <mailto:nikos.gr33k at gmail.com>> wrote:
>
>     Στις 8/11/2013 11:11 μμ, ο/η Νίκος Αλεξόπουλος έγραψε:
>
>         Is there someway to write the following line even better with the
>
>         ability to detect daylight saving time by itself so i don't have to
>         alter the line manually when time changes?
>
>         lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
>         '%y-%m-%d %H:%M:%S' )        # MySQL datetime format
>
>         Thanks.
>
>
>
>     Example:
>
>     #check if date entered as intented, format it properly for MySQL
>     lastvisit = datetime.strptime(lastvisit, '%d %m
>     %Y').strftime('%Y-%m-%d')
>
>     The above code gets the datetime string in a specific way and then
>     convert it to another format. All happens in one line clear and simple.
>
>     Now, in the following statement there shopuld be some way to check
>     for daylight saving time so i would aboiut add +2 in winter or +3
>     for greek summer time.
>
>
>     lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
>     '%y-%m-%d %H:%M:%S' )          # MySQL datetime format
>
>     I think + timedelta(hours=2) should be substituted with something
>     else but i don't with what.
>     --
>     https://mail.python.org/__mailman/listinfo/python-list
>     <https://mail.python.org/mailman/listinfo/python-list>
>
>


this requires a function to be declared while i just want just an 
argument inside timedelta( ) that will automatically detect DST or not.

No function declaration but instead something like:
lastvisit = ( datetime.utcnow() + timedelta( some_arg_here_that_can 
detect greece's_DST_or_not) ).strftime)     '%y-%m-%d %H:%M:%S' ) 
    # MySQL datetime format






More information about the Python-list mailing list