Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > date problem

Reply
Thread Tools

date problem

 
 
Gabkin
Guest
Posts: n/a
 
      07-11-2004
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      07-11-2004
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
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-11-2004
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


 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      07-11-2004
Gabkin <(E-Mail Removed)> 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;
 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      07-11-2004
Bob <(E-Mail Removed)> 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;
 
Reply With Quote
 
Chris F.A. Johnson
Guest
Posts: n/a
 
      07-11-2004
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
 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      07-11-2004
Purl Gurl <(E-Mail Removed)> 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;
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      07-11-2004
Purl Gurl <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
(remove '.invalid' and reverse each component for email address)

 
Reply With Quote
 
Peter J. Acklam
Guest
Posts: n/a
 
      07-11-2004
Bob <(E-Mail Removed)> wrote:

> Peter J. Acklam wrote:
>
> > Bob <(E-Mail Removed)> 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;
 
Reply With Quote
 
Tapani Tarvainen
Guest
Posts: n/a
 
      07-11-2004
Bob <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP General 11 11-08-2003 11:24 PM
Given a date, how to find the beginning date and ending date of that week Matt ASP .Net 1 11-08-2003 09:14 PM
Given a date, how to find the beginning date and ending date of that week Matt C Programming 3 11-08-2003 09:07 PM
Given a date, how to find the beginning date and ending date of that week Matt C++ 2 11-08-2003 08:30 PM



Advertisments