Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58864 > unrolled thread

datetime question

Started byΝίκος Αλεξόπουλος <nikos.gr33k@gmail.com>
First post2013-11-08 23:11 +0200
Last post2013-11-12 19:20 +0000
Articles 20 on this page of 66 — 21 participants

Back to article view | Back to comp.lang.python


Contents

  datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-08 23:11 +0200
    Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 21:29 +0000
      Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-09 00:01 +0200
        Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-08 17:06 -0500
          Re: datetime question rurpy@yahoo.com - 2013-11-11 14:49 -0800
            Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 18:18 -0500
        Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 22:18 +0000
        Re: datetime question Grant Edwards <invalid@invalid.invalid> - 2013-11-08 22:29 +0000
          Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-09 09:39 +1100
            Re: datetime question Roy Smith <roy@panix.com> - 2013-11-09 10:30 -0500
          Re: datetime question Gene Heskett <gheskett@wdtv.com> - 2013-11-09 10:39 -0500
          Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-10 09:25 +1100
        Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-08 22:49 +0000
          Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-09 01:02 +0200
            Re: datetime question Robert Kern <robert.kern@gmail.com> - 2013-11-08 23:23 +0000
            Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-09 10:30 +1100
            Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-09 00:12 +0000
            Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-09 12:45 +0000
              Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-09 15:43 +0200
                Re: datetime question unknown <unknown@unknown.com> - 2013-11-10 12:52 +0000
            Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-09 23:52 +1100
    Re: datetime question Gene Heskett <gheskett@wdtv.com> - 2013-11-09 19:55 -0500
    Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-10 19:47 +1100
    Re: datetime question Gene Heskett <gheskett@wdtv.com> - 2013-11-10 04:13 -0500
    Re: datetime question Νίκος Αλεξόπουλος <nikos.gr33k@gmail.com> - 2013-11-11 11:57 +0200
      Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 08:41 -0500
        Re: datetime question Roy Smith <roy@panix.com> - 2013-11-11 09:14 -0500
          Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-12 01:25 +1100
          Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-11 09:36 -0500
          Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-11 20:59 -0500
          Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-12 13:02 +1100
          Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-11 21:02 -0500
          Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 19:30 -0500
          Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-13 11:40 +1100
      Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-11 19:19 +0000
        Re: datetime question Ethan Furman <ethan@stoneleaf.us> - 2013-11-11 11:39 -0800
          Re: datetime question Grant Edwards <invalid@invalid.invalid> - 2013-11-11 20:45 +0000
          Re: datetime question mm0fmf <none@mailinator.com> - 2013-11-11 23:21 +0000
            Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-11 23:48 +0000
              Re: datetime question Grant Edwards <invalid@invalid.invalid> - 2013-11-12 04:49 +0000
                Re: datetime question alex23 <wuwei23@gmail.com> - 2013-11-12 17:25 +1000
                  Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-12 18:35 +1100
                  Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 09:20 +0000
                Re: datetime question Roy Smith <roy@panix.com> - 2013-11-12 09:18 -0500
    Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 12:15 +0200
      Re: datetime question Andy Lawton <andy@andylawton.eu> - 2013-11-12 12:47 +0000
        Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 15:32 +0200
          Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-12 09:03 -0500
            Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 16:12 +0200
              Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-13 01:57 +1100
                Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 17:24 +0200
                  Re: datetime question Tim Chase <python.list@tim.thechases.com> - 2013-11-12 09:54 -0600
                    Re: datetime question Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-11-12 17:57 +0200
                      Re: datetime question Tim Chase <python.list@tim.thechases.com> - 2013-11-12 10:12 -0600
                      Re: datetime question William Ray Wing <wrw@mac.com> - 2013-11-12 11:14 -0500
                      Re: datetime question Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-12 16:30 +0000
                      Re: datetime question MRAB <python@mrabarnett.plus.com> - 2013-11-12 19:09 +0000
                      Re: datetime question Joel Goldstick <joel.goldstick@gmail.com> - 2013-11-12 14:14 -0500
                      Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-12 19:24 +0000
                        Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 19:38 -0500
                      Re: datetime question Chris Angelico <rosuav@gmail.com> - 2013-11-13 09:42 +1100
                      Re: datetime question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-11-12 19:37 -0500
                      Re: datetime question MRAB <python@mrabarnett.plus.com> - 2013-11-13 01:08 +0000
                    Re: datetime question Alister <alister.ware@ntlworld.com> - 2013-11-12 16:11 +0000
                      Re: datetime question Roy Smith <roy@panix.com> - 2013-11-12 19:43 -0500
        Re: datetime question Denis McMahon <denismfmcmahon@gmail.com> - 2013-11-12 19:20 +0000

Page 3 of 4 — ← Prev page 1 2 [3] 4  Next page →


#59161

Fromalex23 <wuwei23@gmail.com>
Date2013-11-12 17:25 +1000
Message-ID<l5sl5n$202$1@dont-email.me>
In reply to#59149
On 12/11/2013 2:49 PM, Grant Edwards wrote:
> Don't forget that there are also some differences between American and
> Imperial whitespace.  Since it's ASCII whitespace, you should probably
> assume American...

 >>> sys.getsizeof(' ')
34
 >>> sys.getsizeof(u' ')
52

bad by design

[toc] | [prev] | [next] | [standalone]


#59162

FromChris Angelico <rosuav@gmail.com>
Date2013-11-12 18:35 +1100
Message-ID<mailman.2433.1384241724.18130.python-list@python.org>
In reply to#59161
On Tue, Nov 12, 2013 at 6:25 PM, alex23 <wuwei23@gmail.com> wrote:
> On 12/11/2013 2:49 PM, Grant Edwards wrote:
>>
>> Don't forget that there are also some differences between American and
>> Imperial whitespace.  Since it's ASCII whitespace, you should probably
>> assume American...
>
>
>>>> sys.getsizeof(' ')
> 34
>>>> sys.getsizeof(u' ')
> 52
>
> bad by design

Yeah, but the proper way to delimit whitespace is with parentheses,
not quotes. Look:

>>> sys.getsizeof(( ))
28

ChrisA

[toc] | [prev] | [next] | [standalone]


#59169

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-12 09:20 +0000
Message-ID<mailman.2442.1384248306.18130.python-list@python.org>
In reply to#59161
On 12/11/2013 07:25, alex23 wrote:
> On 12/11/2013 2:49 PM, Grant Edwards wrote:
>> Don't forget that there are also some differences between American and
>> Imperial whitespace.  Since it's ASCII whitespace, you should probably
>> assume American...
>
>  >>> sys.getsizeof(' ')
> 34
>  >>> sys.getsizeof(u' ')
> 52
>
> bad by design

Don't you start :)

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#59198

FromRoy Smith <roy@panix.com>
Date2013-11-12 09:18 -0500
Message-ID<roy-BEFC46.09185812112013@news.panix.com>
In reply to#59149
In article <l5sc04$3vd$1@reader1.panix.com>,
 Grant Edwards <invalid@invalid.invalid> wrote:

> On 2013-11-11, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> > On 11/11/2013 23:21, mm0fmf wrote:
> >> On 11/11/2013 19:39, Ethan Furman wrote:
> >>> On 11/11/2013 11:19 AM, Denis McMahon wrote:
> >>>> On Mon, 11 Nov 2013 11:57:36 +0200, ?????????? ?????????????????????? 
> >>>> wrote:
> >>>>
> >>>>>> lastvisit = ( datetime.utcnow() + timedelta(hours=2) ).strftime(
> >>>>>> '%y-%m-%d %H:%M:%S' )        # MySQL datetime format
> >>>>
> >>>>> Someone has an idea what to add to this line to automatically adjust
> >>>>> itself if DST happens?
> >>>>
> >>>> Yes, but the scarcity of universal whitespace is preventing me from
> >>>> posting it.
> >>>
> >>> Boy, are you in luck!  I just happen to have a whole heap of marked down
> >>> ascii white-space at bargain basement prices!
> >>
> >> Is that metric or imperial whitespace?
> >>
> >> You need metric whitespace for programs written in Europe.
> >
> > Except for the UK, our multi-national approach means that both imperial 
> > and metric whitespace are acceptable.
> 
> Don't forget that there are also some differences between American and
> Imperial whitespace.  Since it's ASCII whitespace, you should probably
> assume American...

And don't assume that the whitespace you find in a red state is the same 
as you'll find in a blue state.

[toc] | [prev] | [next] | [standalone]


#59176

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-11-12 12:15 +0200
Message-ID<l5sv4a$a2$1@dont-email.me>
In reply to#58864
Στις 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.

[toc] | [prev] | [next] | [standalone]


#59189

FromAndy Lawton <andy@andylawton.eu>
Date2013-11-12 12:47 +0000
Message-ID<mailman.2459.1384260501.18130.python-list@python.org>
In reply to#59176

[Multipart message — attachments visible in raw view] — view raw

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@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
>

[toc] | [prev] | [next] | [standalone]


#59191

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-11-12 15:32 +0200
Message-ID<l5tam0$a2$2@dont-email.me>
In reply to#59189
Στις 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@gmail.com
> <mailto:nikos.gr33k@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


[toc] | [prev] | [next] | [standalone]


#59195

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-11-12 09:03 -0500
Message-ID<mailman.2463.1384265037.18130.python-list@python.org>
In reply to#59191
On Tue, Nov 12, 2013 at 8:32 AM, Ferrous Cranus <nikos.gr33k@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.gr33k@gmail.com
>> <mailto:nikos.gr33k@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 Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#59197

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-11-12 16:12 +0200
Message-ID<l5td0v$a2$4@dont-email.me>
In reply to#59195
Στις 12/11/2013 4:03 μμ, ο/η Joel Goldstick έγραψε:
> On Tue, Nov 12, 2013 at 8:32 AM, Ferrous Cranus <nikos.gr33k@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.gr33k@gmail.com
>>> <mailto:nikos.gr33k@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!

[toc] | [prev] | [next] | [standalone]


#59201

FromChris Angelico <rosuav@gmail.com>
Date2013-11-13 01:57 +1100
Message-ID<mailman.2466.1384268259.18130.python-list@python.org>
In reply to#59197
On Wed, Nov 13, 2013 at 1:12 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
> Joel i must thank you for your help.
>
> I cannot believe it was so simple.
>
> Tnhe server is self aware of its location so why use utcnow() + timedelte(
> some_digit_here ) when you can just use just now()

Did you ever go and look at the docs, like you were advised to?

Next time you have a problem, go read the docs. You never know, you
might discover something!

ChrisA

[toc] | [prev] | [next] | [standalone]


#59205

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-11-12 17:24 +0200
Message-ID<l5th81$a2$5@dont-email.me>
In reply to#59201
Στις 12/11/2013 4:57 μμ, ο/η Chris Angelico έγραψε:
> On Wed, Nov 13, 2013 at 1:12 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>> Joel i must thank you for your help.
>>
>> I cannot believe it was so simple.
>>
>> Tnhe server is self aware of its location so why use utcnow() + timedelte(
>> some_digit_here ) when you can just use just now()
>
> Did you ever go and look at the docs, like you were advised to?
>
> Next time you have a problem, go read the docs. You never know, you
> might discover something!
>
> ChrisA
>


But what of the server was in California and i live in Greece?

How would datetime.now() work then?

now() would return the time fo the server not my local time, so if i 
change servers and the server isn't located to the same timezone as i'am 
locally, wouldn't that raise a problem?

[toc] | [prev] | [next] | [standalone]


#59210

FromTim Chase <python.list@tim.thechases.com>
Date2013-11-12 09:54 -0600
Message-ID<mailman.2472.1384271596.18130.python-list@python.org>
In reply to#59205
On 2013-11-12 17:24, Ferrous Cranus wrote:
> But what of the server was in California and i live in Greece?
> 
> How would datetime.now() work then?

Best practices say to move the value from local time to UTC as soon
as possible, then store/use the UTC time internally for all
operations.  Only when it's about to be presented to the user should
you convert it back to local time if you need to.

-tkc


[toc] | [prev] | [next] | [standalone]


#59212

FromFerrous Cranus <nikos.gr33k@gmail.com>
Date2013-11-12 17:57 +0200
Message-ID<l5tj62$a2$6@dont-email.me>
In reply to#59210
Στις 12/11/2013 5:54 μμ, ο/η Tim Chase έγραψε:
> On 2013-11-12 17:24, Ferrous Cranus wrote:
>> But what of the server was in California and i live in Greece?
>>
>> How would datetime.now() work then?
>
> Best practices say to move the value from local time to UTC as soon
> as possible, then store/use the UTC time internally for all
> operations.  Only when it's about to be presented to the user should
> you convert it back to local time if you need to.
>
> -tkc
>
>
>


or perhaps by confiruing the timezone of the server to use Greece's 
TimeZone by issuing a linux command?

[toc] | [prev] | [next] | [standalone]


#59215

FromTim Chase <python.list@tim.thechases.com>
Date2013-11-12 10:12 -0600
Message-ID<mailman.2476.1384272663.18130.python-list@python.org>
In reply to#59212
On 2013-11-12 17:57, Ferrous Cranus wrote:
> > Best practices say to move the value from local time to UTC as
> > soon as possible, then store/use the UTC time internally for all
> > operations.  Only when it's about to be presented to the user
> > should you convert it back to local time if you need to.
> 
> or perhaps by confiruing the timezone of the server to use Greece's 
> TimeZone by issuing a linux command?

Regardless of the server's configured TZ, best practice still says to
normalize everything to UTC (ESPECIALLY if Greece uses the
abomination of DST that we suffer here in the US) as soon as
possible and keep it that way for as long as possible.

-tkc

[toc] | [prev] | [next] | [standalone]


#59217

FromWilliam Ray Wing <wrw@mac.com>
Date2013-11-12 11:14 -0500
Message-ID<mailman.2477.1384272864.18130.python-list@python.org>
In reply to#59212
On Nov 12, 2013, at 10:57 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:

> Στις 12/11/2013 5:54 μμ, ο/η Tim Chase έγραψε:
>> On 2013-11-12 17:24, Ferrous Cranus wrote:
>>> But what of the server was in California and i live in Greece?
>>> 
>>> How would datetime.now() work then?
>> 
>> Best practices say to move the value from local time to UTC as soon
>> as possible, then store/use the UTC time internally for all
>> operations.  Only when it's about to be presented to the user should
>> you convert it back to local time if you need to.
>> 
>> -tkc
>> 
>> 
>> 
> 
> or perhaps by confiruing the timezone of the server to use Greece's TimeZone by issuing a linux command?
> -- 
> 

Thus totally screwing all the other users/sites hosted on that server? Thus totally screwing up all the housekeeping and maintenance OS tasks that are run by chron jobs?

Shakes head in dismay...

Bill

[toc] | [prev] | [next] | [standalone]


#59219

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-12 16:30 +0000
Message-ID<mailman.2480.1384273828.18130.python-list@python.org>
In reply to#59212
On 12/11/2013 16:12, Tim Chase wrote:
>
> Regardless of the server's configured TZ, best practice still says to
> normalize everything to UTC (ESPECIALLY if Greece uses the
> abomination of DST that we suffer here in the US) as soon as
> possible and keep it that way for as long as possible.
>
> -tkc
>
>

Why oh why do I have an image of people at Bletchley Park trying to 
switch Colossus from GMT to DOUBLE British Summer Time and back again?

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#59227

FromMRAB <python@mrabarnett.plus.com>
Date2013-11-12 19:09 +0000
Message-ID<mailman.2486.1384283359.18130.python-list@python.org>
In reply to#59212
On 12/11/2013 16:12, Tim Chase wrote:
> On 2013-11-12 17:57, Ferrous Cranus wrote:
>> > Best practices say to move the value from local time to UTC as
>> > soon as possible, then store/use the UTC time internally for all
>> > operations.  Only when it's about to be presented to the user
>> > should you convert it back to local time if you need to.
>>
>> or perhaps by confiruing the timezone of the server to use Greece's
>> TimeZone by issuing a linux command?
>
> Regardless of the server's configured TZ, best practice still says to
> normalize everything to UTC (ESPECIALLY if Greece uses the
> abomination of DST that we suffer here in the US) as soon as
> possible and keep it that way for as long as possible.
>
FTR, the countries in the EU all change at the same (UTC) time.

[toc] | [prev] | [next] | [standalone]


#59229

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-11-12 14:14 -0500
Message-ID<mailman.2487.1384283687.18130.python-list@python.org>
In reply to#59212
On Tue, Nov 12, 2013 at 2:09 PM, MRAB <python@mrabarnett.plus.com> wrote:
> On 12/11/2013 16:12, Tim Chase wrote:
>>
>> On 2013-11-12 17:57, Ferrous Cranus wrote:
>>>
>>> > Best practices say to move the value from local time to UTC as
>>> > soon as possible, then store/use the UTC time internally for all
>>> > operations.  Only when it's about to be presented to the user
>>> > should you convert it back to local time if you need to.
>>>
>>> or perhaps by confiruing the timezone of the server to use Greece's
>>> TimeZone by issuing a linux command?
>>
>>
>> Regardless of the server's configured TZ, best practice still says to
>> normalize everything to UTC (ESPECIALLY if Greece uses the
>> abomination of DST that we suffer here in the US) as soon as
>> possible and keep it that way for as long as possible.
>>
> FTR, the countries in the EU all change at the same (UTC) time.
> --
> https://mail.python.org/mailman/listinfo/python-list

In the US, the state of Indiana is really weird.  Three separate time
zone areas, that don't all flip in the same way.  See this for TZ
hell:  http://en.wikipedia.org/wiki/Indiana_time_zones

-- 
Joel Goldstick
http://joelgoldstick.com

[toc] | [prev] | [next] | [standalone]


#59231

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2013-11-12 19:24 +0000
Message-ID<l5tv9d$io2$2@dont-email.me>
In reply to#59212
On Tue, 12 Nov 2013 17:57:55 +0200, Ferrous Cranus wrote:

> or perhaps by confiruing the timezone of the server to use Greece's
> TimeZone by issuing a linux command?

If you have that degreee of control over the server, yes, but UTC is 
always safer, because if you need to move your server to a different site 
(eg disaster recovery) UTC is still UTC.

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#59266

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-11-12 19:38 -0500
Message-ID<mailman.2514.1384303207.18130.python-list@python.org>
In reply to#59231
On Tue, 12 Nov 2013 19:24:30 +0000 (UTC), Denis McMahon
<denismfmcmahon@gmail.com> declaimed the following:

>On Tue, 12 Nov 2013 17:57:55 +0200, Ferrous Cranus wrote:
>
>> or perhaps by confiruing the timezone of the server to use Greece's
>> TimeZone by issuing a linux command?
>
>If you have that degreee of control over the server, yes, but UTC is 
>always safer, because if you need to move your server to a different site 
>(eg disaster recovery) UTC is still UTC.

	And you don't have to run a batch job on your database to change
timestamps relative to the server local time.
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


Page 3 of 4 — ← Prev page 1 2 [3] 4  Next page →

Back to top | Article view | comp.lang.python


csiph-web