date problem

G

Gabkin

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
 
G

Gunnar Hjalmarsson

Gabkin said:
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_)

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.
 
J

Jürgen Exner

Gabkin said:
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
 
P

Peter J. Acklam

Gabkin said:
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
 
P

Peter J. Acklam

Bob said:
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
 
C

Chris F.A. Johnson

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
 
P

Peter J. Acklam

Purl Gurl said:
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.
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
 
P

Peter J. Acklam

Bob said:
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
 
T

Tapani Tarvainen

Bob said:
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).
 
P

Peter J. Acklam

Purl Gurl said:
Peter J. Acklam:



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
 
P

Peter J. Acklam

Purl Gurl said:
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
 
W

William Park

In said:
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.
 
L

Lukas Mai

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>

#include <time.h>

#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
 
G

Gunnar Hjalmarsson

Gabkin said:
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/[email protected]
 
P

Peter J. Acklam

Purl Gurl said:
A. Sinan Unur said:

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
 
P

Peter J. Acklam

Purl Gurl said:
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
 
L

Lukas Mai

Purl Gurl schrob:
Lukas Mai wrote:


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.

See above.
What? No comment on this one?

That's not a standard header.

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 said:
^ ^ those aren't needed
Yeah, so what?

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top