Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'example:': 0.03; 'else:': 0.03; 'argument': 0.05; '#if': 0.07; 'detect': 0.07; 'diff': 0.07; 'string': 0.09; 'alter': 0.09; 'dst': 0.09; 'skip:% 20': 0.09; 'sure)': 0.09; 'wrong,': 0.09; 'subject:question': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'andy': 0.16; 'declaration': 0.16; 'declared': 0.16; 'simple.': 0.16; 'substituted': 0.16; 'ubuntu.': 0.16; 'wrote:': 0.18; 'pointed': 0.19; 'properly': 0.19; 'entered': 0.20; 'solution.': 0.20; 'thanks.': 0.20; 'seems': 0.21; 'machine': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'skip:+ 40': 0.24; 'skip:+ 50': 0.24; 'switched': 0.24; 'test.': 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; 'host': 0.29; 'correct': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; "doesn't": 0.30; 'start,': 0.30; 'statement': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; 'code': 0.31; 'lines': 0.31; 'another': 0.32; 'url:python': 0.33; 'running': 0.33; 'not.': 0.33; 'skip:d 20': 0.34; "i'd": 0.34; 'could': 0.34; 'knows': 0.35; 'possible.': 0.35; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'format.': 0.36; 'joined': 0.36; 'url:listinfo': 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'skip:- 20': 0.37; 'list': 0.37; 'clear': 0.37; 'server': 0.38; 'nov': 0.38; 'little': 0.38; 'ability': 0.39; '12,': 0.39; 'itself': 0.39; 'sure': 0.39; 'mailing': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'even': 0.60; 'manually': 0.60; 'took': 0.61; 'matter': 0.61; 'happen': 0.63; 'different': 0.65; 'great': 0.65; 'to:addr:gmail.com': 0.65; 'eastern': 0.68; 'winter': 0.68; 'saving': 0.69; '8bit%:92': 0.71; 'soon.': 0.71; '8bit%:100': 0.72; 'hey,': 0.75; 'greece': 0.84; 'greek': 0.84; 'joel': 0.91; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=GEZqoYa++9aTvg2565qaJJrqUsVK83N6tooogcyxaMo=; b=DbQ5HiVFaQlUiim3x0YwTdtfQt42dieckeJbiH+JzVeq817R+tkSpr2oYTfDL7ITQK m65ad9Nkw9EWrvmzasWN78QO1AU+a/L7cFD02P6/tQInDmdmuhbdR1UGF30cQJpfFDMd 4ZjKDITmEcXju3fgYZI9LteCDeqJyLv8X4O0qjM8+ZQACaSxlbM9h45GD7VwYmgiHrNC 1Ju4irYszp+fvh/g6AKZiFxRWbMGpKohwuf52Kk269eh8M3D/Xut38xKOUjvWOsOVRu3 0nIsQzGl51m8XY2IECjP4AgqzgHY1KMEoA4gzcanBFyWO9D3kCQmoFfifD+sQF3VAttp f4BQ== MIME-Version: 1.0 X-Received: by 10.220.194.133 with SMTP id dy5mr23674344vcb.3.1384265029226; Tue, 12 Nov 2013 06:03:49 -0800 (PST) In-Reply-To: References: Date: Tue, 12 Nov 2013 09:03:49 -0500 Subject: Re: datetime question From: Joel Goldstick To: Ferrous Cranus Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 153 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384265037 news.xs4all.nl 16006 [2001:888:2000:d::a6]:40767 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59195 On Tue, Nov 12, 2013 at 8:32 AM, Ferrous Cranus wro= te: > =CE=A3=CF=84=CE=B9=CF=82 12/11/2013 2:47 =CE=BC=CE=BC, =CE=BF/=CE=B7 Andy= Lawton =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: >> >> 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 =3D pytz.timezone(zonename) >> now =3D pytz.utc.localize(datetime.utcnow()) >> return now.astimezone(tz).dst() !=3D timedelta(0) >> >> def dst_greece(): >> if is_dst("Europe/Kiev") : >> diff =3D 2 >> else: >> diff =3D 3 >> return diff >> >> lastvisit =3D (datetime.utcnow() +timedelta(hours=3Ddst_greece())).strft= ime( >> '%y-%m-%d %H:%M:%S' ) >> >> >> On 12 November 2013 10:15, Ferrous Cranus > > wrote: >> >> =CE=A3=CF=84=CE=B9=CF=82 8/11/2013 11:11 =CE=BC=CE=BC, =CE=BF/=CE=B7= =CE=9D=CE=AF=CE=BA=CE=BF=CF=82 =CE=91=CE=BB=CE=B5=CE=BE=CF=8C=CF=80=CE=BF= =CF=85=CE=BB=CE=BF=CF=82 =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5: >> >> Is there someway to write the following line even better with th= e >> >> ability to detect daylight saving time by itself so i don't have >> to >> alter the line manually when time changes? >> >> lastvisit =3D ( datetime.utcnow() + timedelta(hours=3D2) ).strft= ime( >> '%y-%m-%d %H:%M:%S' ) # MySQL datetime format >> >> Thanks. >> >> >> >> Example: >> >> #check if date entered as intented, format it properly for MySQL >> lastvisit =3D 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 =3D ( datetime.utcnow() + timedelta(hours=3D2) ).strftime( >> '%y-%m-%d %H:%M:%S' ) # MySQL datetime format >> >> I think + timedelta(hours=3D2) should be substituted with something >> else but i don't with what. >> -- >> https://mail.python.org/__mailman/listinfo/python-list >> >> >> > > > this requires a function to be declared while i just want just an argumen= t > inside timedelta( ) that will automatically detect DST or not. > > No function declaration but instead something like: > lastvisit =3D ( datetime.utcnow() + timedelta( some_arg_here_that_can det= ect > 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 =3D (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 =3D pytz.timezone(zonename) now =3D pytz.utc.localize(datetime.utcnow()) return now.astimezone(tz).dst() !=3D timedelta(0) def dst_greece(): #if is_dst("Europe/Kiev") : if is_dst("US/Eastern") : diff =3D -6 else: diff =3D -5 return diff lastvisit =3D (datetime.utcnow() +timedelta(hours=3Ddst_greece())).strftime('%y-%m-%d %H:%M:%S' ) lastvisit2 =3D (datetime.now()).strftime('%y-%m-%d %H:%M:%S' ) print lastvisit print lastvisit2 ---------------------- --=20 Joel Goldstick http://joelgoldstick.com