Yet another 'date' question

Yet another 'date' question

Post by k.. » Wed, 16 Feb 1994 22:10:26



|>
|>Here's a short perl script that'll do what you want:
|>
|>#! /usr/local/bin/perl
|>require 'ctime.pl';
|>$time = time;
|>print(&ctime($time - (localtime($time))[6] * 86400));
|>

Here's a new 'date' question, but I think that this perl script recently
posted is close to the answer i need...

On the first of every month, I need to rename some files to
'originalname.yyyy_mm', where the year and month indicate the previous month.
I can have a cron job run on the first of each month, but I don't know how
to get the 'yyyy_mm' part.   If I change this perl script to:

require 'ctime.pl';
$time = time;
print(&ctime($time -86400));

This will return something like 'Mon Jan 31 12:50:01 GMT 1994'.  Is there
some simple way in perl to return this as '1994_Jan', or preferably '1994_01'?

-----------------
Joe Kazimierczyk

 
 
 

Yet another 'date' question

Post by J. Sei » Thu, 17 Feb 1994 02:42:14


|>

|> |>
|> |>Here's a short perl script that'll do what you want:
|> |>
|> |>#! /usr/local/bin/perl
|> |>require 'ctime.pl';
|> |>$time = time;
|> |>print(&ctime($time - (localtime($time))[6] * 86400));
|> |>
|>
|> Here's a new 'date' question, but I think that this perl script recently
|> posted is close to the answer i need...
|>
|> On the first of every month, I need to rename some files to
|> 'originalname.yyyy_mm', where the year and month indicate the previous month.
|> I can have a cron job run on the first of each month, but I don't know how
|> to get the 'yyyy_mm' part.   If I change this perl script to:
|>
|> require 'ctime.pl';
|> $time = time;
|> print(&ctime($time -86400));
|>
|> This will return something like 'Mon Jan 31 12:50:01 GMT 1994'.  Is there
|> some simple way in perl to return this as '1994_Jan', or preferably '1994_01'?
|>
|> -----------------
|> Joe Kazimierczyk

`date +"%Y_%m"`

Joe Seigh

 
 
 

Yet another 'date' question

Post by Rob M » Thu, 17 Feb 1994 18:39:06


: On the first of every month, I need to rename some files to
: 'originalname.yyyy_mm', where the year and month indicate the previous month.
: I can have a cron job run on the first of each month, but I don't know how
: to get the 'yyyy_mm' part.   If I change this perl script to:

[ deleted ]

: some simple way in perl to return this as '1994_Jan', or preferably '1994_01'?

How about date +"19%y_%m" ??? This certainly works here (SunOS 4.1A.1)

Rob
--
---------------------------------------------------------------------------

-- You may post,  repost or publish *ANY* communication received from me --
---------------------------------------------------------------------------

 
 
 

Yet another 'date' question

Post by k.. » Fri, 18 Feb 1994 01:08:15


|>

|>|>

|>|> |>
|>|> |>Here's a short perl script that'll do what you want:
|>|> |>
|>|> |>#! /usr/local/bin/perl
|>|> |>require 'ctime.pl';
|>|> |>$time = time;
|>|> |>print(&ctime($time - (localtime($time))[6] * 86400));
|>|> |>
|>|>
|>|> Here's a new 'date' question, but I think that this perl script recently
|>|> posted is close to the answer i need...
|>|>
|>|> On the first of every month, I need to rename some files to
|>|> 'originalname.yyyy_mm', where the year and month indicate the previous month.
|>|> I can have a cron job run on the first of each month, but I don't know how
|>|> to get the 'yyyy_mm' part.   If I change this perl script to:
|>|>
|>|> require 'ctime.pl';
|>|> $time = time;
|>|> print(&ctime($time -86400));
|>|>
|>|> This will return something like 'Mon Jan 31 12:50:01 GMT 1994'.  Is there
|>|> some simple way in perl to return this as '1994_Jan', or preferably '1994_01'?
|>|>
|>|> -----------------
|>|> Joe Kazimierczyk

|>
|>`date +"%Y_%m"`
|>
|>Joe Seigh
|>

Sorry if I'm being thick, but where does `date +"%Y_%m"` fit in this perl
script?

----------------
Joe Kazimierczyk

 
 
 

Yet another 'date' question

Post by David Rybo » Fri, 18 Feb 1994 02:52:08



||>|> This will return something like 'Mon Jan 31 12:50:01 GMT 1994'.  Is there
||>|> some simple way in perl to return this as '1994_Jan', or preferably '1994_01'?
||>
||>`date +"%Y_%m"`

|Sorry if I'm being thick, but where does `date +"%Y_%m"` fit in this perl
|script?

$ date +"%Y_%m"
1994_02
$

or

$ date +"19%y_%m"
1994_02
$

This does not go into the perl script at all!!!

 
 
 

Yet another 'date' question

Post by Stephen P. Pott » Fri, 18 Feb 1994 04:37:40



   ||>|> This will return something like 'Mon Jan 31 12:50:01 GMT 1994'.  Is there
   ||>|> some simple way in perl to return this as '1994_Jan', or preferably '1994_01'?
   ||>
   ||>`date +"%Y_%m"`

   |Sorry if I'm being thick, but where does `date +"%Y_%m"` fit in this perl
   |script?

   This does not go into the perl script at all!!!

Then the original poster did not answer the question that was asked.  The
question was (basically) how to turn 'Mon Jan 31 12:50:01 GMT 1994' into
'1994_Jan' or '1994_01' *in perl*.

The simple answer is (I forget the original timelocal() function):

($day, $month, $dat, $time, $timezone, $year) = split(' ', "Mon Jan 31 12:50:01 GMT 1994");
print "$year_$month";

You can get a little fancier and make an array of the months and return the
value of the month.

Steve
--
  University of Florida, Department of Computer and Information Sciences

              smail: Stephen P Potter, E309A CSE, UF Campus
Click here to find out about the <a href="http://www.cis.ufl.edu/perl">Perl World Wide Web Page</a>

 
 
 

Yet another 'date' question

Post by k.. » Fri, 18 Feb 1994 22:37:23


Hello all,

The original question which started this thread was:  How do I get the
YYYY_MM of the PREVIOUS month?  As far as I can tell, 'date +"%Y_%m"' only
gives me the CURRENT date.

Ultimately, what I need to do is:  on the first of every month, rename
a file to incorporate the YYYY_MM of the month just ended.  For example,
on Mar 1, 1994, I need to 'mv filename filename.1994_02', and
on Jan 1, 1995, I need to 'mv filename filename.1994_12'.

The closest answer I've come up with so far is:

a perl script called perl-time:
        require 'ctime.pl';
        $time = time - 86400;
        ($day, $month, $dat, $time, $timezone, $year)=split(' ',&ctime($time));
        printf("%s_%s", $year, $month);

Then from my cron job which runs on the 1st of each month:
        cp filename filename.`perl perl-time`

The month format is not exactly what I want, but I can live with it.
If anyone has a simpler solution, I'd be glad to see it.

Thanks,

Joe Kazimierczyk

 
 
 

Yet another 'date' question

Post by Martin N. Ste » Sat, 19 Feb 1994 00:11:33


: Hello all,

: The original question which started this thread was:  How do I get the
: YYYY_MM of the PREVIOUS month?  As far as I can tell, 'date +"%Y_%m"' only
: gives me the CURRENT date.

: Ultimately, what I need to do is:  on the first of every month, rename
: a file to incorporate the YYYY_MM of the month just ended.  For example,
: on Mar 1, 1994, I need to 'mv filename filename.1994_02', and
: on Jan 1, 1995, I need to 'mv filename filename.1994_12'.

: The closest answer I've come up with so far is:

: a perl script called perl-time:
:       require 'ctime.pl';
:       $time = time - 86400;
:       ($day, $month, $dat, $time, $timezone, $year)=split(' ',&ctime($time));
:       printf("%s_%s", $year, $month);

: Then from my cron job which runs on the 1st of each month:
:       cp filename filename.`perl perl-time`

: The month format is not exactly what I want, but I can live with it.
: If anyone has a simpler solution, I'd be glad to see it.

Well, given that cronjobs are executed with a /bin/sh it makes little sense
to fork perl....

How about:

* * 1 * * /mung-file in crontab

where /mung-file is:

#!/bin/sh

year=`date | cut -c 25-`
month=`date | cut -c 5-7`

case $month in
'Jan') monthnum=12
       year=`echo "$year 1 - f" | dc`;;
'Feb') monthnum=01;;
'Mar') monthnum=02;;
'Apr') monthnum=03;;
'May') monthnum=04;;
'Jun') monthnum=05;;
'Jul') monthnum=06;;
'Aug') monthnum=07;;
'Sep') monthnum=08;;
'Oct') monthnum=09;;
'Nov') monthnum=10;;
'Dec') monthnum=11;;
esac

mv filename filename_$year_$month

Martin
--

They were wonderful in the ambulance, they gave him artificial respiration, I
wish they'd given him real respiration.
        -- Inside Victor Lewis Smith

 
 
 

Yet another 'date' question

Post by Conrad Kimba » Sat, 19 Feb 1994 15:55:22



>Hello all,

>The original question which started this thread was:  How do I get the
>YYYY_MM of the PREVIOUS month?  As far as I can tell, 'date +"%Y_%m"' only
>gives me the CURRENT date.

>Ultimately, what I need to do is:  on the first of every month, rename
>a file to incorporate the YYYY_MM of the month just ended.  For example,
>on Mar 1, 1994, I need to 'mv filename filename.1994_02', and
>on Jan 1, 1995, I need to 'mv filename filename.1994_12'.

>The closest answer I've come up with so far is:

>a perl script called perl-time:
>    require 'ctime.pl';
>    $time = time - 86400;
>    ($day, $month, $dat, $time, $timezone, $year)=split(' ',&ctime($time));
>    printf("%s_%s", $year, $month);

>Then from my cron job which runs on the 1st of each month:
>    cp filename filename.`perl perl-time`

>The month format is not exactly what I want, but I can live with it.
>If anyone has a simpler solution, I'd be glad to see it.

Simpler?  What I have is not simpler, but IMHO more robust - it doesn't
require perl to be present, and it doesn't require that the job be
run _exactly_ on the first of the month (as your algorithm does).

awk "BEGIN {
          `date '+year=%y; month=%m`
          if (year > 70) year += 1900
          else           year += 2000
          month -= 1
          if (month < 1) { month += 12; year -= 1 }
          printf(\"%d_%02d\n\", year, month)
          exit
        }"
--
--
Conrad Kimball        | Client Server Tech Services, Boeing Computer Services

(206) 865-6410        | Seattle, WA  98124-0346

 
 
 

Yet another 'date' question

Post by john.j.rushford.. » Sun, 20 Feb 1994 14:27:32




>>Hello all,

>>The original question which started this thread was:  How do I get the
>>YYYY_MM of the PREVIOUS month?  As far as I can tell, 'date +"%Y_%m"' only
>>gives me the CURRENT date.

Attached is a C program that I put together for my use in shell programming,
hope I don't get flamed for posting C code here.  I call this program ydate.
It works exactly like the date command except you can't set the system time
with it.

Using ydate without any options prints yesterdays date.  All the formating
options you're familiar with in date(1) are available.  With the -n option,
you may find any date into the future or in the past by adding or subtracting
the number of days as an argument to the -n option.  The program is limited
to dates after 1900 and may not be totally portable.

---------------------------- CUT HERE ----------------------------------------
/*
 *    NAME
 *        ydate.c - Print yesterdays date.
 *
 *    SYNOPSIS
 *        ydate [-n days] [+format_string]
 *
 *    DESCRIPTION
 *        Ydate writes yesterdays date and time to the standard output.
 *
 *        Ydate accepts one option:
 *
 *        -n days
 *            Adds the specified number of days to the current system
 *            time.  If days is a negative integer, then the specified
 *            number of days is subtracted from the current system time.
 *
 *        If an argument to ydate is given that begins with a '+', then
 *    the output is controlled by the contents of the rest of the
 *    string.  Normal text is output unmodified, while field descriptors
 *    in the format string are substituted for.
 *
 *        Ydate is essentially a wrapper around ascftime(3) on System V
 *    machines and a wrapper around strftime(3) on SUN systems, see there
 *    for a description of the available formatting options.
 *
 *        If no format string is given, or if it does not begin with a '+',
 *    then the default format of "%a %b %e %H:%M:%S %Z %Y" will be used.
 *    This produces the traditional style of output, such as:
 *      
 *            Sun Mar 17 10:32:47 EST 1991.
 *
 *    COMPILE
 *        System V:  cc -s -O -o ydate ydate.c
 *
 *        SUN:  cc -s -O -DSUN -o ydate ydate.c
 *
 *    SEE ALSO
 *        ascftime(3), strftime(3), time(3)
 *
 */

/*LINTLIBRARY*/


#include <stdio.h>
#ifndef SUN
#include <sys/types.h>
#endif
#include <time.h>

#define ONEDAY 86400
#define DEFAULT "%a %b %e %H:%M:%S %Z %Y"
#define USAGE(a) (void) fprintf (stderr, \
    "usage: %s [-n days] [+format_string]\007\n", a)

#ifndef __STDC__
typedef enum boolean {B_FALSE, B_TRUE} boolean_t;
#endif

main(argc, argv)
int argc;
char **argv;
{
    boolean_t    errflag = B_FALSE,    /* errors */
                 nflag  = B_FALSE;     /* used -n option */
    char         buf[256],             /* character array for date string */
                 *fmt = DEFAULT;       /* date format string */
    extern char  *optarg;
    extern int   optind;
    int          c;
    struct tm    *dt;
    time_t       clock,
                 time ();
    void         exit ();

    /* current system time */
    (void) time (&clock);

    while ((c = getopt (argc, argv, "n:")) != -1)
        switch (c) {
            case 'n':  clock += (atoi (optarg) * ONEDAY);
                       nflag = B_TRUE;
                       break;
            case '?':  errflag = B_TRUE;
                       break;
        }

    /* exit on errors */
    if (errflag) {
        USAGE(argv[0]);
        exit (1);
    }

    /* unless the -n option is used, subtract a day from the current time */
    if (!nflag) clock -= ONEDAY;

    /* use the user supplied format if one is specified */
    if (optind < argc && argv[optind][0] == '+') fmt = &argv[optind][1];

/* print the date */
    dt = (struct tm *) localtime (&clock);
#ifdef SUN
    if (strftime (buf, 256, fmt, dt) > 0) (void) printf ("%s\n", buf);
#else
    if (ascftime (buf, fmt, dt) > 0) (void) printf ("%s\n", buf);
#endif
    else (void) fprintf (stderr, "bad conversion.\n");

    return 0;

Quote:}

 
 
 

Yet another 'date' question

Post by Tom Christianse » Wed, 23 Feb 1994 02:10:32




    :: The original question which started this thread was:  How do I get the
    :: YYYY_MM of the PREVIOUS month?  
    [...]
    :: a perl script called perl-time:
    [...]

:Well, given that cronjobs are executed with a /bin/sh it makes little sense
:to fork perl....

``Given that system XYZ is written in language ABC, it makes little sense
to fork anything else than ABC'' would appear to be what you said.  What
an odd notion.

:How about:
:* * 1 * * /mung-file in crontab
:where /mung-file is:
:#!/bin/sh

Ok, there's one fork for the /bin/sh.

:year=`date | cut -c 25-`

There's one fork for the date plus one for the cut.  Up to 3 now.

:month=`date | cut -c 5-7`

That's 5.

:case $month in
:'Jan') monthnum=12
:       year=`echo "$year 1 - f" | dc`;;

That's two more, putting you up to 7 so far.  I realize
that echo is sometimes builtin, but then you're only
spared the exec, not the fork.

:mv filename filename_$year_$month

And there's one more, putting us at six forks, except for January,
which merits eight instead.

(Plus I think he said cp, not mv.)

So, just tell me what it was you're trying to save here??

Here's the basic algorithm:

    ($m,$y) = (localtime(time))[4,5];
    $y += 1900;
    if ($m == 0) { $m = 12; $y--; }
    printf "%4d_%02d\n", $y, $m;

Now, if you put that in a file (perhaps calling it "lastmonth") and
execute it, it will give you the right format at far fewer forks.

So, you're cronline could be this:

    cp foo foo.`lastmonth`

That's two forks, not 6 or 8.

*OR*, if what you're doing is a mv not a cp, make the baklastmonth program:

    #!/usr/bin/repl
    ($m,$y) = (localtime(time))[4,5];
    $y += 1900;
    if ($m == 0) { $m = 12; $y--; }

        rename ($_, $_ . sprintf(".%4d_%02d\n", $y, $m))
            || warn "can't rename $_ to lastmonth: $!";
    }

Which still costs 1 fork no matter how many files you want to rename.

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212

 
 
 

Yet another 'date' question

Post by Tom Christianse » Wed, 23 Feb 1994 02:14:17



:Attached is a C program that I put together for my use in shell programming,
:hope I don't get flamed for posting C code here.  I call this program ydate.
:It works exactly like the date command except you can't set the system time
:with it.

While this is not intended as a flame, I will not that this is one more
C program to make sure is running on every system you need it on,
one more binary whose source you might misplace, etc.

Just consider what every single additional one of these entails when you're
running 50 systems with 7 architectures.

--tom
--

      "Will Hack Perl for Fine Food and Fun"
        Boulder Colorado  303-444-3212

 
 
 

Yet another 'date' question

Post by Mark Brad » Wed, 23 Feb 1994 06:06:29


I seem to have missed the original posting in this thread, but the
following script may be of interest to somebody if not to the original
poster.  It produces the date of the most recent Sunday before today,
writing it to stdout in ISO standard form (e.g. 1994-05-29).  It's assumed
that your "cal" command can work with numeric month and year arguments
and that your "date" command accepts a +format argument.

#!/bin/sh
# sunday - returns the date (yyyy-mm-dd) of the last Sunday before today.

case "$#" in
0)      ;;
*)      echo "Usage: $0" >&2
        exit 1
esac

set `date '+19%y %m %d'`        # year in $1, month in $2, day in $3

sunday=`cal $2 $1 | sed '2,$s/$/ /
                         s/ \([1-9] \)/0\1/g
                         /'"$3"' /!d
                         s/^\(..\).*/\1/'`

case "$sunday" in             # if "  ", must go back a month
"  ") case "$2" in
        01)     set `expr $1 - 1` 12;;  # previous year
        1[12])  set $1 `expr $2 - 1`;;  # previous month, which is 2 digits
        *)      set $1 0`expr $2 - 1`;; # previous month, which is 1 digit
        esac

        sunday=`cal $2 $1 | grep '[0-9]' | tail -1 | sed 's/\(..\).*/\1/'`;;
esac

echo $1-$2-$sunday
exit 0
--
Mark Brader             "Do UNIX users ever think about the fact that most
SoftQuad Inc.            of their financial dealings are processed in
Toronto                  languages that they wouldn't be caught dead in?"

This article is in the public domain, including the shell script.

 
 
 

Yet another 'date' question

Post by Mark Brad » Wed, 23 Feb 1994 15:39:15


Quote:> The original question which started this thread was:  How do I get the
> YYYY_MM of the PREVIOUS month?  As far as I can tell, 'date +"%Y_%m"' only
> gives me the CURRENT date.

> Ultimately, what I need to do is:  on the first of every month, rename
> a file to incorporate the YYYY_MM of the month just ended.  For example,
> on Mar 1, 1994, I need to 'mv filename filename.1994_02' ...

I'm not sure how portable it is these days that programs can have their
local time zone specified in an environment variable.  If you can do that,
and if there is a time zone west of you that is recognized, and if you
can be *sure* that the renaming will happen *soon after midnight*,
then you can just change the time zone westward and run date with a
format.  For example, the following works for me in Toronto:

        mv filename filename.`TZ=PST8PDT date +%y_%m`

(though it only gives a 2-digit year, as my system's "date" doesn't
have a format giving a 4-digit year :-().

If you want to be a bit more robust, though, you can handle it easily
enough this way:

        set `date '+19%y %m'` # don't use 19%y if you can get 4-digit years
        case $2 in
        1|01)   suffix=`expr $1 - 1`_12;;
        1[12])  suffix=$1_`expr $2 - 1`;;
        *)      suffix=$1_0`expr $2 - 1`;;
        esac
        mv filename filename.$suffix

If you want to preserve the arguments the script was called with, then
wrap a pair of parentheses around the above lines.
--
Mark Brader        "... given time, a generally accepted solution to this
Toronto             problem will evolve, as it has in the past for ... [other]
utzoo!sq!msb         issues, only to be replaced by the next issue, which no-one

This article is in the public domain.

 
 
 

Yet another 'date' question

Post by Conrad Kimba » Sat, 26 Feb 1994 15:15:38




>Martin> Well, given that cronjobs are executed with a /bin/sh it makes little sense
>Martin> to fork perl....

>Martin> How about:

>Martin> * * 1 * * /mung-file in crontab

>Martin> where /mung-file is:

>Martin> #!/bin/sh

>Martin> year=`date | cut -c 25-`
>Martin> month=`date | cut -c 5-7`

>Martin> case $month in
>Martin> 'Jan') monthnum=12
>Martin>        year=`echo "$year 1 - f" | dc`;;
>Martin> 'Feb') monthnum=01;;
>Martin> 'Mar') monthnum=02;;
>Martin> 'Apr') monthnum=03;;
>Martin> 'May') monthnum=04;;
>Martin> 'Jun') monthnum=05;;
>Martin> 'Jul') monthnum=06;;
>Martin> 'Aug') monthnum=07;;
>Martin> 'Sep') monthnum=08;;
>Martin> 'Oct') monthnum=09;;
>Martin> 'Nov') monthnum=10;;
>Martin> 'Dec') monthnum=11;;
>Martin> esac

>Martin> mv filename filename_$year_$month

>Uhhh, I don't get it.  It makes *more* sense to fork eight *other*
>processes than to fork *one* Perl job to do the whole thing?  Gack.  I
>wouldn't want to be a user on *your* system. :-)

Let me see.... you're worried about an extra 7 processes run once
a month?  I'm glad I don't have *you* making decisions about how
to optimize my systems.  You'll spend far more machine resources
installing and maintaining perl than you'll _ever_ burn up by
running these extra processes once a month.
--
--
Conrad Kimball        | Client Server Tech Services, Boeing Computer Services

(206) 865-6410        | Seattle, WA  98124-0346
 
 
 

1. Calculating yesterday's date using GNU 'date' (was Re: Require a script)

Using GNU 'date':

$ date --date '1 years 2 days ago'
Sun Sep 19 20:14:53 CEST 1999

The GNU 'date' command comes with the GNU 'sh-utils' package available
on all GNU FTP mirrors.

/A

--
Andreas K?h?ri, <URL:http://hello.to/andkaha/>. Junk mail, no.
------------------------------------------------------------------------
What part of "GNU" did you not understand? <URL:http://www.gnu.org/>

2. How to mount tape

3. grep using date problem (fgrep `date '+%D'` ~/.diary)?

4. Updating the time in csh prompt with every command line entry

5. Yet another 'restart socket server question'

6. VPN

7. Yet another 'find' question..

8. Symbios 53C875 SCSI

9. Yet Another 'Headless Linux' Question

10. 'date' and 'clock' show different times

11. time difference between 'date' & 'cron'

12. Another 'date' question

13. Process START times (ps aux) don't agree with 'date'