time.strftime('%m-%d-%Y %H:%m:%S') to log is out of order

Discussion in 'Python' started by davidj411, Jul 21, 2009.

  1. davidj411

    davidj411 Guest

    I am using a recursive function to print the time and a few other
    things on each pass. ( the function calculates size of file that is
    being transferred and if not 100 % copied, it waits 20 secs and checks
    again).

    i would expect the time to be correct anytime it is used:

    <--code below -->>
    print time.strftime('%m-%d-%Y %H:%m:%S')
    <--code above -->>

    here is an example of what i am seeing:

    16:07:16
    16:07:36
    16:07:56
    16:07:16
    16:07:36
    16:07:56
    16:07:16
    16:07:36
    16:07:56
     
    davidj411, Jul 21, 2009
    #1
    1. Advertising

  2. davidj411

    Jon Clements Guest

    On 21 July, 22:00, davidj411 <> wrote:
    > I am using a recursive function to print the time and a few other
    > things on each pass. ( the function calculates size of file that is
    > being transferred and if not 100 % copied, it waits 20 secs and checks
    > again).
    >
    > i would expect the time to be correct anytime it is used:
    >
    > <--code below -->>
    > print time.strftime('%m-%d-%Y %H:%m:%S')
    > <--code above -->>
    >
    > here is an example of what i am seeing:
    >
    > 16:07:16
    > 16:07:36
    > 16:07:56
    > 16:07:16
    > 16:07:36
    > 16:07:56
    > 16:07:16
    > 16:07:36
    > 16:07:56


    I assume month, day and year are actually being output and that you've
    removed it from your post.

    Err, what else do you expect to happen if you're doing this
    recursively?
     
    Jon Clements, Jul 21, 2009
    #2
    1. Advertising

  3. davidj411

    Simon Forman Guest

    On Jul 21, 5:00 pm, davidj411 <> wrote:
    > I am using a recursive function to print the time and a few other
    > things on each pass. ( the function calculates size of file that is
    > being transferred and if not 100 % copied, it waits 20 secs and checks
    > again).
    >
    > i would expect the time to be correct anytime it is used:
    >
    > <--code below -->>
    > print time.strftime('%m-%d-%Y %H:%m:%S')
    > <--code above -->>
    >
    > here is an example of what i am seeing:
    >
    > 16:07:16
    > 16:07:36
    > 16:07:56
    > 16:07:16
    > 16:07:36
    > 16:07:56
    > 16:07:16
    > 16:07:36
    > 16:07:56


    Your output doesn't match your format string:

    In [1]: import time

    In [2]: print time.strftime('%m-%d-%Y %H:%m:%S')
    07-21-2009 17:07:16

    There's no way to tell why your output times seem to repeat without
    seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H:
    %m:%S')" line.
     
    Simon Forman, Jul 21, 2009
    #3
  4. >>>>> davidj411 <> (d) wrote:

    >d> I am using a recursive function to print the time and a few other
    >d> things on each pass. ( the function calculates size of file that is
    >d> being transferred and if not 100 % copied, it waits 20 secs and checks
    >d> again).


    >d> i would expect the time to be correct anytime it is used:


    >d> <--code below -->>
    >d> print time.strftime('%m-%d-%Y %H:%m:%S')
    >d> <--code above -->>


    >d> here is an example of what i am seeing:


    >d> 16:07:16
    >d> 16:07:36
    >d> 16:07:56
    >d> 16:07:16
    >d> 16:07:36
    >d> 16:07:56
    >d> 16:07:16
    >d> 16:07:36
    >d> 16:07:56


    You probably meant: print time.strftime('%m-%d-%Y %H:%M:%S')
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 21, 2009
    #4
  5. davidj411

    Jon Clements Guest

    On 21 July, 22:38, Piet van Oostrum <> wrote:
    > >>>>> davidj411 <> (d) wrote:

    > >d> I am using a recursive function to print the time and a few other
    > >d> things on each pass. ( the function calculates size of file that is
    > >d> being transferred and if not 100 % copied, it waits 20 secs and checks
    > >d> again).
    > >d> i would expect the time to be correct anytime it is used:
    > >d> <--code below -->>
    > >d> print time.strftime('%m-%d-%Y %H:%m:%S')
    > >d> <--code above -->>
    > >d> here is an example of what i am seeing:
    > >d> 16:07:16
    > >d> 16:07:36
    > >d> 16:07:56
    > >d> 16:07:16
    > >d> 16:07:36
    > >d> 16:07:56
    > >d> 16:07:16
    > >d> 16:07:36
    > >d> 16:07:56

    >
    > You probably meant: print time.strftime('%m-%d-%Y %H:%M:%S')
    > --
    > Piet van Oostrum <>
    > URL:http://pietvanoostrum.com[PGP 8DAE142BE17999C4]
    > Private email:


    Good spot!
     
    Jon Clements, Jul 21, 2009
    #5
  6. davidj411

    davidj411 Guest

    On Jul 21, 5:29 pm, Simon Forman <> wrote:
    > On Jul 21, 5:00 pm, davidj411 <> wrote:
    >
    >
    >
    >
    >
    > > I am using a recursive function to print the time and a few other
    > > things on each pass. ( the function calculates size of file that is
    > > being transferred and if not 100 % copied, it waits 20 secs and checks
    > > again).

    >
    > > i would expect the time to be correct anytime it is used:

    >
    > > <--code below -->>
    > > print time.strftime('%m-%d-%Y %H:%m:%S')
    > > <--code above -->>

    >
    > > here is an example of what i am seeing:

    >
    > > 16:07:16
    > > 16:07:36
    > > 16:07:56
    > > 16:07:16
    > > 16:07:36
    > > 16:07:56
    > > 16:07:16
    > > 16:07:36
    > > 16:07:56

    >
    > Your output doesn't match your format string:
    >
    > In [1]: import time
    >
    > In [2]: print time.strftime('%m-%d-%Y %H:%m:%S')
    > 07-21-2009 17:07:16
    >
    > There's no way to tell why your output times seem to repeat without
    > seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H:
    > %m:%S')" line.


    sorry, yes, i did manually filter the output.

    here is the function:

    def log_out(msg,servername='std.out'):
    print msg
    open(log_dir + '\\' + servername + ".log",'a').write(servername + "-"
    + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n')

    on each pass, it should output the newer time (whether recursive or
    not, right) ?
     
    davidj411, Jul 21, 2009
    #6
  7. davidj411

    MRAB Guest

    davidj411 wrote:
    > On Jul 21, 5:29 pm, Simon Forman <> wrote:
    >> On Jul 21, 5:00 pm, davidj411 <> wrote:
    >>
    >>
    >>
    >>
    >>
    >>> I am using a recursive function to print the time and a few other
    >>> things on each pass. ( the function calculates size of file that is
    >>> being transferred and if not 100 % copied, it waits 20 secs and checks
    >>> again).
    >>> i would expect the time to be correct anytime it is used:
    >>> <--code below -->>
    >>> print time.strftime('%m-%d-%Y %H:%m:%S')
    >>> <--code above -->>
    >>> here is an example of what i am seeing:
    >>> 16:07:16
    >>> 16:07:36
    >>> 16:07:56
    >>> 16:07:16
    >>> 16:07:36
    >>> 16:07:56
    >>> 16:07:16
    >>> 16:07:36
    >>> 16:07:56

    >> Your output doesn't match your format string:
    >>
    >> In [1]: import time
    >>
    >> In [2]: print time.strftime('%m-%d-%Y %H:%m:%S')
    >> 07-21-2009 17:07:16
    >>
    >> There's no way to tell why your output times seem to repeat without
    >> seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H:
    >> %m:%S')" line.

    >
    > sorry, yes, i did manually filter the output.
    >
    > here is the function:
    >
    > def log_out(msg,servername='std.out'):
    > print msg
    > open(log_dir + '\\' + servername + ".log",'a').write(servername + "-"
    > + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n')
    >
    > on each pass, it should output the newer time (whether recursive or
    > not, right) ?


    Maybe it does, but you were outputting the month ("07") instead of the
    minutes; the seconds were changing.
     
    MRAB, Jul 21, 2009
    #7
  8. >>>>> davidj411 <> (d) wrote:
    >>>
    >>> > <--code below -->>
    >>> > print time.strftime('%m-%d-%Y %H:%m:%S')
    >>> > <--code above -->>


    >d> here is the function:


    >d> def log_out(msg,servername='std.out'):
    >d> print msg
    >d> open(log_dir + '\\' + servername + ".log",'a').write(servername + "-"
    >d> + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n')


    >d> on each pass, it should output the newer time (whether recursive or
    >d> not, right) ?


    How come your strftime is now different from the first posting? Are you
    cheating?

    If you want to ask a question about why your code is not working
    properly there are a few important rules:

    1. Copy and paste your code, and at least all relevant stuff (preferably
    a minimal example that shows the problem). *DO NOT RETYPE THE CODE*

    2. Copy and paste the output. *DO NOT RETYPE THE OUTPUT*

    3. Tell what the expected or desired output is.
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 21, 2009
    #8
  9. davidj411

    Simon Forman Guest

    On Jul 21, 5:53 pm, davidj411 <> wrote:
    > On Jul 21, 5:29 pm, Simon Forman <> wrote:
    >
    >
    >
    > > On Jul 21, 5:00 pm, davidj411 <> wrote:

    >
    > > > I am using a recursive function to print the time and a few other
    > > > things on each pass. ( the function calculates size of file that is
    > > > being transferred and if not 100 % copied, it waits 20 secs and checks
    > > > again).

    >
    > > > i would expect the time to be correct anytime it is used:

    >
    > > > <--code below -->>
    > > > print time.strftime('%m-%d-%Y %H:%m:%S')
    > > > <--code above -->>

    >
    > > > here is an example of what i am seeing:

    >
    > > > 16:07:16
    > > > 16:07:36
    > > > 16:07:56
    > > > 16:07:16
    > > > 16:07:36
    > > > 16:07:56
    > > > 16:07:16
    > > > 16:07:36
    > > > 16:07:56

    >
    > > Your output doesn't match your format string:

    >
    > > In [1]: import time

    >
    > > In [2]: print time.strftime('%m-%d-%Y %H:%m:%S')
    > > 07-21-2009 17:07:16

    >
    > > There's no way to tell why your output times seem to repeat without
    > > seeing the code that surrounds your "print time.strftime('%m-%d-%Y %H:
    > > %m:%S')" line.

    >
    > sorry, yes, i did manually filter the output.
    >
    > here is the function:
    >
    > def log_out(msg,servername='std.out'):
    >         print msg
    >         open(log_dir + '\\' + servername + ".log",'a').write(servername + "-"
    > + time.strftime('%m-%d-%Y %H:%M:%S') + " " + msg+'\n')
    >
    > on each pass, it should output the newer time (whether recursive or
    > not, right) ?


    Well, as Piet van Oostrum pointed out, your problem in the first code
    you posted was that you used '%m' rather than '%M' for the minutes.
    (Good eye Van Oostrum!) But now in this function you seem to have the
    correct '%M' field. Are you still having the same output after
    changing that?


    In any event, here's a rewritten version of that function that's a
    little cleaner, FWIW.


    from os.path import join
    from time import strftime

    format = '%m-%d-%Y %H:%M:%S'

    def log_out(msg, servername='std.out'):
    print msg
    msg = "%s - %s %s\n" % (servername, strftime(format), msg)
    log_file = open(join(log_dir, servername + ".log"), 'a')
    try:
    log_file.write(msg)
    finally:
    log_file.close()


    But why not just use the logging module?
     
    Simon Forman, Jul 22, 2009
    #9
  10. davidj411

    davidj411 Guest

    i never heard of the logging module, but this function seemed simple
    enough.

    i assume this link is what you refering to:
    http://docs.python.org/library/logging.html

    thanks for the helpful info. i think "Piet van Oostrum" has resolved
    my issue. good eyes!
     
    davidj411, Jul 22, 2009
    #10
  11. >>>>> davidj411 <> (d) wrote:

    >d> i never heard of the logging module, but this function seemed simple
    >d> enough.


    >d> i assume this link is what you refering to:
    >d> http://docs.python.org/library/logging.html


    >d> thanks for the helpful info. i think "Piet van Oostrum" has resolved
    >d> my issue. good eyes!


    Without glasses I wouldn't have noticed it :=)
    --
    Piet van Oostrum <>
    URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
    Private email:
     
    Piet van Oostrum, Jul 22, 2009
    #11
  12. davidj411

    Ben Guest

    On Jul 22, 1:04 pm, davidj411 <> wrote:

    > i think "Piet van Oostrum" has resolved my issue.
    > good eyes!


    Well, he *is* Dutch...

    -Ben
     
    Ben, Jul 28, 2009
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sheila King
    Replies:
    5
    Views:
    973
    Moderator, k12.ed.math
    Apr 22, 2005
  2. Gabriel Genellina
    Replies:
    2
    Views:
    742
    Simon Mullis
    Nov 12, 2010
  3. Greg Krohn
    Replies:
    1
    Views:
    966
    Denis Kasak
    Feb 17, 2009
  4. J
    Replies:
    2
    Views:
    692
  5. Leena Jethwa

    Create a log-in & log-out page

    Leena Jethwa, May 1, 2009, in forum: Ruby
    Replies:
    2
    Views:
    107
    Mike Stephens
    May 1, 2009
Loading...

Share This Page