Στις 12/11/2013 4:03 μμ, ο/η Joel Goldstick έγραψε:
On Tue, Nov 12, 2013 at 8:32 AM, Ferrous Cranus <nikos.gr...@gmail.com> wrote:
Στις 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.gr...@gmail.com
<mailto:nikos.gr...@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



--
https://mail.python.org/mailman/listinfo/python-list

I think that is a great solution.  What would happen if you just did this:
lastvisit2 = (datetime.now()).strftime('%y-%m-%d %H:%M:%S' )

Why don't you try it out and see what you find?

(PS, this was pointed out to me by a pro, so I did a little reading in
the python docs and took Andy's excellent code and added a couple of
lines to test.)

I switched to my local timezone "US/Eastern" to test. My machine is
running Ubuntu.  It seems to know that it is in the Eastern US.  If
your server knows where it lives, then this should work for you also.
If it is set to a different timezone, I'm not sure I could help you,
but I'm sure someone could.  You even might ask your host if this is
possible.

----------------------
#! /usr/bin/env python

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") :
      if is_dst("US/Eastern") :
          diff = -6
      else:
          diff = -5
      return diff

lastvisit = (datetime.utcnow()
+timedelta(hours=dst_greece())).strftime('%y-%m-%d %H:%M:%S' )

lastvisit2 = (datetime.now()).strftime('%y-%m-%d %H:%M:%S' )

print lastvisit
print lastvisit2

----------------------




Joel i must thank you for your help.

I cannot believe it was so simple.

lastvisit = ( datetime.now() ).strftime('%y-%m-%d %H:%M:%S') # MySQL datetime format


Tnhe server is self aware of its location so why use utcnow() + timedelte( some_digit_here ) when you can just use just now()

Great solution, no need for function declaration and importing of new pytz modules.

Simple and straightforward, Thank you!
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to