date problem

Discussion in 'Perl Misc' started by Gabkin, Jul 11, 2004.

  1. Gabkin

    Gabkin Guest

    I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
    program. (Note the _three_ digits allocated for Seconds, not the usual
    _two_)

    date +%Y%m%d%H%M%S will give YYYYMMDDHHMMSS which is close but it doesnt
    have that extra S at the end.

    I am using "date (sh-utils) 2.0.15" and "perl, v5.8.2" on Cygwin.

    Is there a quick and easy way of getting that extra degree of precision
    with 'date'?
    Failing that, a way to get it with perl?

    Thanks
     
    Gabkin, Jul 11, 2004
    #1
    1. Advertising

  2. Gabkin wrote:
    > I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
    > program. (Note the _three_ digits allocated for Seconds, not the
    > usual _two_)


    <snip>

    > Is there a quick and easy way of getting that extra degree of
    > precision with 'date'?


    > Failing that, a way to get it with perl?


    In Perl there is the Time::HiRes module.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 11, 2004
    #2
    1. Advertising

  3. Gabkin wrote:
    > I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
    > program. (Note the _three_ digits allocated for Seconds, not the usual
    > _two_)
    >
    > date +%Y%m%d%H%M%S will give YYYYMMDDHHMMSS which is close but it
    > doesnt have that extra S at the end.


    Well, hmmm, when I look at my watch the possible values for seconds range
    from 0 to 59. This is only _two_ digits which implies that your additional
    third digit would always be zero.
    Something along the line of
    s/(..)$/0$1/
    should add that digit.

    jue
     
    Jürgen Exner, Jul 11, 2004
    #3
  4. Gabkin <> wrote:

    > I need to generate a date in the format YYYYMMDDHHMMSSS for a
    > perl program. (Note the _three_ digits allocated for Seconds,
    > not the usual _two_)
    >
    > date +%Y%m%d%H%M%S will give YYYYMMDDHHMMSS which is close but
    > it doesnt have that extra S at the end.


    Assuming the third S is for deciseconds, try the Time::HiRes
    module. If you have it installed, try

    perldoc Time::HiRes

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #4
  5. Bob <> wrote:

    > On Sun, 11 Jul 2004 16:21:38 +0200, Gabkin wrote:
    >
    > > Is there a quick and easy way of getting that extra degree of
    > > precision with 'date'?

    >
    > Precision? There will never be more than two digits in the
    > seconds field (00-60 -- yes, 60. See man date!).


    "Precision" is the number of digits relative to the decimal point.
    Higher precision means more digits after the decimal point, i.e.,
    higher resolution. For instance, a precision of 3 would include
    milliseconds.

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #5
  6. On 2004-07-11, Gabkin wrote:
    > I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
    > program. (Note the _three_ digits allocated for Seconds, not the usual
    > _two_)
    >
    > date +%Y%m%d%H%M%S will give YYYYMMDDHHMMSS which is close but it doesnt
    > have that extra S at the end.
    >
    > I am using "date (sh-utils) 2.0.15" and "perl, v5.8.2" on Cygwin.
    >
    > Is there a quick and easy way of getting that extra degree of precision
    > with 'date'?


    date +%Y%m%d%H%M%03S

    --
    Chris F.A. Johnson http://cfaj.freeshell.org/shell
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License
     
    Chris F.A. Johnson, Jul 11, 2004
    #6
  7. Purl Gurl <> wrote:

    > Gabkin wrote:
    >
    > > I need to generate a date in the format YYYYMMDDHHMMSSS for a
    > > perl program. (Note the _three_ digits allocated for Seconds,
    > > not the usual _two_)

    >
    > Others have explained why you cannot have three digit
    > resolution.


    Which is nonsense. OP wants an "extra degree of precision", which
    will be seconds and deciseconds.

    > > I am using ... "perl, v5.8.2" on Cygwin.

    >
    > At a DOS command line, test this hack,
    >
    > date.|time
    >
    > That will return milliseconds along with wording you
    > really do not need.


    Really? On my Windows box I only get centiseconds:

    C:\>date.|time
    The current time is: 20:42:23,97
    Enter the new time: The system cannot accept the date entered.
    The system cannot accept the time entered.
    Enter the new time:

    > You will find adapting this to Perl to be fun, with a need to
    > send a ^D signal.


    Gibberish. If you solve this by sending a signal it must be ^C.
    It doesn't understand ^D. Actually, it just prints "^D".

    A much better way is using "echo", which will give you the prompt
    back:

    C:\>echo date.|time
    The current time is: 20:43:55,08
    Enter the new time: date.
    The system cannot accept the time entered.
    Enter the new time:
    C:\>

    > Press ENTER to continue running your Perl program,
    > in lieu of piping a control D signal, if you use
    > a backtick or system command, system ("date.|time");


    Piping an empty string is much easier.

    > A quick and easy way to accomplish your precise task
    > is to compile a cpp executable written for your task.
    > An example is beneath my signature.


    Using Time::HiRes is so much easier.

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #7
  8. Purl Gurl <> wrote in
    news::

    > A quick and easy way to accomplish your precise task
    > is to compile a cpp executable written for your task.
    > An example is beneath my signature.


    cpp refers to the c pre-processor. ITYM C++.

    > void main(void)


    See http://www.eskimo.com/~scs/C-faq/q11.14.html

    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
     
    A. Sinan Unur, Jul 11, 2004
    #8
  9. Bob <> wrote:

    > Peter J. Acklam wrote:
    >
    > > Bob <> wrote:
    > >
    > > "Precision" is the number of digits relative to the decimal
    > > point. Higher precision means more digits after the decimal
    > > point, i.e., higher resolution. For instance, a precision of
    > > 3 would include milliseconds.

    >
    > Ahh ... In that case we need the first digit of nanoseconds:
    >
    > set $(date +%Y%m%d%H%M%S%N)
    > echo ${1:0:15}
    >
    > although that could result in a rounding error.


    Huh. I didn't know date has a %N. I see GNU date has, although I
    don't have a Solaris box here to check if that too has %N.

    > P.S. "Precision" does not imply relationship to a decimal
    > point. It means "number of significant digits"


    I was using the lingo from numerical analysis related to floating
    point arithmetic. And there, the total number of significant
    digits is called "accuracy", whereas "precision" is the number of
    digits relative to the decimal point.

    > For example, saying there are a million fish in the sea is less
    > precise than saying there are 1,247,683 fish in the sea.


    Right, which is "precision" as used in everyday language. And
    that was probably what OP used too.

    > My initial reading was that there could never be more than two
    > significant digits when counting seconds in a minute.


    That was mine too. :)

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #9
  10. Bob <> writes:

    > Ahh ... In that case we need the first digit of nanoseconds:
    >
    > set $(date +%Y%m%d%H%M%S%N)


    Interesting. In HP-UX date +%N gives emperor name (for Japanese
    calendar), not nanoseconds... it doesn't seem to be defined
    at all by POSIX. But it does seem to work with Gnu date.

    > echo ${1:0:15}


    That is also non-POSIX, it assumes bash or ksh93 or similar.
    But with Gnu date you can simply use "%-1N" to get only the first
    digit of nanoseconds (= deciseconds).

    --
    Tapani Tarvainen
     
    Tapani Tarvainen, Jul 11, 2004
    #10
  11. Purl Gurl <> wrote:

    > Peter J. Acklam:
    >
    > > Really? On my Windows box I only get centiseconds:

    >
    > > C:\>date.|time
    > > The current time is: 20:42:23,97

    >
    > C:\APACHE\USERS\TEST>date.|time
    > Current time is 12:01:01.35p
    >
    > That is a gross resolution of 350 milliseconds
    > with a precision variant range of plus zero to
    > plus nine milliseconds.


    It's exactly the same resolution as I have. What do you mean?
    I see no milliseconds there. If they are there, please show me.

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #11
  12. Purl Gurl <> wrote in
    news::

    > You do have experience in C language, ANSI C, C++ and
    > Microsoft C++ yes?


    Yes.

    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
     
    A. Sinan Unur, Jul 11, 2004
    #12
  13. Purl Gurl <> wrote:

    > A. Sinan Unur wrote:
    >
    > > Yes.

    >
    > That is it? No translation of your abbreviationese?


    I haven't seen ITYM before, but I'm quite certain it stands for "I
    think you mean".

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #13
  14. (Peter J. Acklam) wrote in news::

    > Purl Gurl <> wrote:
    >
    >> A. Sinan Unur wrote:
    >>
    >> > Yes.

    >>
    >> That is it? No translation of your abbreviationese?

    >
    > I haven't seen ITYM before, but I'm quite certain it stands for "I
    > think you mean".


    http://www.webster-dictionary.org/definition/itym

    --
    A. Sinan Unur
    d
    (remove '.invalid' and reverse each component for email address)
     
    A. Sinan Unur, Jul 11, 2004
    #14
  15. Gabkin

    William Park Guest

    In <comp.unix.shell> Chris F.A. Johnson <> wrote:
    > On 2004-07-11, Gabkin wrote:
    > > I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
    > > program. (Note the _three_ digits allocated for Seconds, not the
    > > usual _two_)
    > >
    > > date +%Y%m%d%H%M%S will give YYYYMMDDHHMMSS which is close but it
    > > doesnt have that extra S at the end.
    > >
    > > I am using "date (sh-utils) 2.0.15" and "perl, v5.8.2" on Cygwin.
    > >
    > > Is there a quick and easy way of getting that extra degree of
    > > precision with 'date'?

    >
    > date +%Y%m%d%H%M%03S


    This will pad 0 on the left. So, 2 second becomes 002 instead of 02.
    Usually, SSS means SS.s where 's' is 1/10 of second. In that case, just
    append 0.

    --
    William Park, Open Geometry Consulting, <>
    Toronto, Ontario, Canada
     
    William Park, Jul 11, 2004
    #15
  16. Gabkin

    Lukas Mai Guest

    Purl Gurl schrob:
    [...]
    > This will compile on Microsoft C++ 5 and 6 versions,
    > precisely as written.


    "Microsoft C++" doesn't exist AFAIK.

    [...]

    > /* File: millitm.cpp Function: return millisecond date stamp*/


    > #include <stdio.h>


    <stdio.h> is deprecated; use <cstdio> instead.

    > #include <time.h>


    <time.h> is deprecated; use <ctime> instead.

    > #include <sys/timeb.h>


    > void main(void)


    main must return int; (void) is redundant => int main()

    > {
    > struct _timeb store_time;
    > char *return_time;


    > _ftime(&store_time);
    > return_time = ctime(&(store_time.time));

    ^ ^ those aren't needed
    Should be std::ctime instead of ctime.

    > printf("%.19s.%hu %s", return_time, store_time.millitm, &return_time[20]);


    Should be std::printf instead of printf.

    > }


    Are you sure you don't want C instead?

    HTH, Lukas
    --
    perl -eval "-MPerlIO=via;printf'Just another Perl hacker%c '=>44"
     
    Lukas Mai, Jul 11, 2004
    #16
  17. Gabkin wrote:
    > I need to generate a date in the format YYYYMMDDHHMMSSS for a perl
    > program. (Note the _three_ digits allocated for Seconds, not the
    > usual _two_)


    sub timestamp {
    require Time::HiRes;
    my ($epoch, $micro) = Time::HiRes::gettimeofday();
    my ($s, $mi, $h, $d, $mo, $y) = (localtime $epoch)[0..5];
    sprintf '%d%02d%02d%02d%02d%02d%.0f',
    $y+1900, $mo+1, $d, $h, $mi, $s, $micro/100000
    }

    print timestamp();

    Outputs e.g.:
    200407112303299

    P.S.
    http://groups.google.com/groups?selm=

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jul 11, 2004
    #17
  18. Purl Gurl <> wrote:

    > A. Sinan Unur wrote:
    >
    > > [...]

    >
    > Ok, so now extract a precise quote from my article
    > in which I write cpp means c plus plus.


    Then what do you mean by "cpp"? I have never seen "cpp" mean
    anything but the C preprocessor, which isn't relevant here.

    $ whatis cpp
    cpp (1) - The C Preprocessor

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #18
  19. Purl Gurl <> wrote:

    > That's right. It is not relevant nor are you
    > an officially recognized spokesperson for
    > this A. Sinan Unur character.


    I simply asked you a question. I see you have no answer.

    Peter

    --
    #!/local/bin/perl5 -wp -*- mode: cperl; coding: iso-8859-1; -*-
    # matlab comment stripper (strips comments from Matlab m-files)
    s/^((?:(?:[])}\w.]'+|[^'%])+|'[^'\n]*(?:''[^'\n]*)*')*).*/$1/x;
     
    Peter J. Acklam, Jul 11, 2004
    #19
  20. Gabkin

    Lukas Mai Guest

    Purl Gurl schrob:
    > Lukas Mai wrote:


    > > Purl Gurl:


    > > > This will compile on Microsoft C++ 5 and 6 versions,
    > > > precisely as written.

    >
    > > "Microsoft C++" doesn't exist AFAIK.

    >
    > WTFO.


    > Well excuse me! Microsoft Visual C++ 6.0 Professional Edition.


    > Feel better?


    Not really; MSVC++ 6 is obsolete. It's older than the C++ standard.

    > > > /* File: millitm.cpp Function: return millisecond date stamp*/
    > > > #include <stdio.h>

    >
    > > <stdio.h> is deprecated; use <cstdio> instead.


    > No.


    Yes: <http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.4>.
    It's in section D.5 [depr.c.headers] of the standard.

    > > > #include <time.h>

    >
    > > <time.h> is deprecated; use <ctime> instead.


    > No.


    See above.

    > > > #include <sys/timeb.h>


    > What? No comment on this one?


    That's not a standard header.

    >
    > > > void main(void)

    >
    > > main must return int; (void) is redundant => int main()


    > No.


    Yes. g++ will complain about this:
    error: `main' must return `int'
    error: return type for `main' changed to `int'

    See 3.6.1 [basic.start.main] and <http://users.aber.ac.uk/auj/voidmain.shtml>.

    > > > return_time = ctime(&(store_time.time));

    > > ^ ^ those aren't needed


    > Yeah, so what?


    > > Should be std::ctime instead of ctime.


    > No.


    Yes: See 17.4.1.1 [lib.contents].

    > > > printf("%.19s.%hu %s", return_time, store_time.millitm, &return_time[20]);

    >
    > > Should be std::printf instead of printf.

    >
    > No.


    See above.

    > > Are you sure you don't want C instead?


    > Are you sure you don't want a living brain instead?
    > Playing Code Cop is so very stupid and so very annoying.


    I don't need to play Code Cop, my compiler warns me about dubious
    constructs.

    > How I write my code is not yours to dictate, Osama.
    > Why are you trolling me, so obviously trolling me?


    Nice ad hominem attack. I do not dictate anything; if you don't want
    comments, then don't post code to a public forum.

    > Have you no pride? Have you no dignity?
    > This name "Lukas Mai" is now known to readers to be a troll.
    > Are you proud?


    > Purl Gurl


    HTH, Lukas
    PS: This is no longer on-topic here. F'up2p.
    --
    print+74.117.115.116,,qq.\c!..not::.her,Perl=>q$hacker,$,!($,=$")
     
    Lukas Mai, Jul 12, 2004
    #20
    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. Matt
    Replies:
    1
    Views:
    610
    Matthew Speed
    Nov 8, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
  3. Matt
    Replies:
    2
    Views:
    522
    Pete Becker
    Nov 8, 2003
  4. Matt
    Replies:
    3
    Views:
    726
    Richard Heathfield
    Nov 8, 2003
  5. Matt
    Replies:
    11
    Views:
    408
    Aaron Bertrand [MVP]
    Nov 8, 2003
Loading...

Share This Page