datetime question

Andy Lawton andy at andylawton.eu
Tue Nov 12 07:47:58 EST 2013


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> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131112/9f4f0333/attachment.html>


More information about the Python-list mailing list