Simple date formatting?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple date formatting?
# 1  
Old 05-01-2009
Simple date formatting?

Hi guys,

I have some embedded perl within my shell script to get me the modification time/date of a file which returns me the following string:

Fri May 1 09:52:58 2009

I have managed to get the bits i need such as 1-May-2009, but what i would prefer is 010509 instead...

Here is my Perl:

Code:
get_time()
{
        perl -e '
        use File::stat;
        use POSIX qw(strftime);
        use Time::localtime;

$file="$ARGV[0]";
$date_string = scalar ctime(stat($file)->mtime);

my ($wkday,$month,$day,$time,$year) = split(/\s+/, $date_string);
print "$day-$month-$year\n";
print "file $file updated at $date_string\n";
' "$1"
}

get_time $1

Basically I'm looking for a way to get the modification time in the format 'DD-MM-YY' but have failed so far...any ideas guys??

I am using Solaris so some date functions may not be available...

Before i am re-directed to a search lol i know there are already utilties for date calc but this doesnt match any of those suggestions.
# 2  
Old 05-01-2009
I normally use gnu date for these types of date conversions, which you can install on Solaris easily:

Code:
$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

  -d, --date=STRING         display time described by STRING, not `now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format.
                            Example: Mon, 07 Aug 2006 12:34:56 -0600
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC=`date', `seconds', or `ns' for
                            date and time to the indicated precision.
                            Date and time components are separated by
                            a single space: 2006-08-07 12:34:56-06:00
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time
      --help     display this help and exit
      --version  output version information and exit

FORMAT controls the output.  The only valid option for the second form
specifies Coordinated Universal Time.  Interpreted sequences are:

  %%   a literal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 21)
  %d   day of month (e.g, 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric timezone (e.g., -0400)
  %:z  +hh:mm numeric timezone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':

  -  (hyphen) do not pad the field
  _  (underscore) pad with spaces
  0  (zero) pad with zeros
  ^  use upper case if possible
  #  use opposite case if possible

After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.

Report bugs to <bug-coreutils@gnu.org>.

# 3  
Old 05-01-2009
Code:
my @now = localtime(time);
my $datestamp = sprintf("%02d%02d%02d", $now[3], $now[4]+1,$now[5]+1900);
print $datestamp


cheers,
Devaraj Takhellambam
# 4  
Old 05-01-2009
thanks guys but i dont think you have understood what i am trying to achieve,

I already have the ctime of the file which is like 'Fri May 1 09:52:58 2009'

But i need to change it so that it is 010509.

The perl i am using doesnt let me do this:

Code:
get_time()
{
        perl -e '
        use File::stat;
        use POSIX qw(strftime);
use Time::localtime;

$file="$ARGV[0]";
$date_string = scalar ctime(stat($file)->mtime);
$date_time=stat($file)->mtime;

my @lt = localtime($date_time};
my $datestamp = sprintf("%02d-%02d-%02d", $lt[3], $lt[4]+1,$lt[5]+1900);
print "Timestamp: $datestamp\n";


my ($wkday,$month,$day,$time,$year) = split(/\s+/, $date_string);
print "$day-$month-$year\n";
print "file $file updated at $date_string\n";
' "$1"
}

get_time $1

The lines in bold return me an array which when i access them like $lt[0] etc return empty values...

All i get is:

Timestamp: 00-01-1900
1-May-2009
file output.txt updated at Fri May 1 09:52:58 2009

Smilie
# 5  
Old 05-01-2009
Look into Date::Format, it might make your life a little easier here:

Date::Format - Date formating subroutines - search.cpan.org
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need Date Formatting help

Hi, How can i store the date + time from the output of the ls command in loop in a variable date1? -rw-rw---- 1 user1 admin 500002 Jan 2 21:24 P002607.cssI then want to convert Jan 2 21:24 to this date format 2014-01-02 21:24:00 and save it in date2 variable. Then i would like to add... (1 Reply)
Discussion started by: mohtashims
1 Replies

2. Shell Programming and Scripting

Date formatting in AIX

Can you help in formating the date command in aix to get the following format Oct 11 21:52 Fri Oct 11 21:52:01 PDT 2013 Required output: Oct 11 21:52 Fri Oct 11 21:52:01 PDT 2013 (1 Reply)
Discussion started by: chandu123
1 Replies

3. Shell Programming and Scripting

Formatting date

Hi all Y=`date +'%Y'` M=`date +'%m'` D=`date +'%d'` if && ;then yesterday=$Y$M`expr $D + 30` echo $yesterday else if && ; then yesterday=$Y$M`expr $D + 29` echo $yesterday else if ; then yesterday=$Y$M`expr $D + 27` echo $yesterday else yesterday=$Y$M`expr $D - 1` echo... (8 Replies)
Discussion started by: ultimatix
8 Replies

4. Shell Programming and Scripting

Formatting a date

Hi, the date value retrieved by a parameter from the table is of the format dd/mm/yyyy. please let me know how to convert this to YYYYMMDD using sed thanks (4 Replies)
Discussion started by: swasid
4 Replies

5. OS X (Apple)

Date Formatting, etc.

Hi - I'm using GeekTool to customize my desktop in OS X 10.5.8 I'm a complete novice as far as UNIX commands, just know enough to be dangerous. I have a command entered as a Shell to display my events from iCal: This makes my events show something like this: While this is... (1 Reply)
Discussion started by: patricksprague
1 Replies

6. Shell Programming and Scripting

date formatting

Hi i need to have the date in the format like dd-mon-yyyy my script goes like this #!/usr/bin/bash for f in /space/can /home/lbs/current/externalcdrbackup/L_CDR_Configuration/1/200903122* ; do awk '{sum++;}END{for(i in sum) {print d,h,m,i, sum}}' "d=$(date +'%m-%d-%Y')" "h=$(date +'%H')"... (8 Replies)
Discussion started by: aemunathan
8 Replies

7. UNIX for Dummies Questions & Answers

Date formatting

Running bash how do I input the date in the command line like 3/20/90 and get an output formmated like March, 20 1990. (8 Replies)
Discussion started by: knc9233
8 Replies

8. HP-UX

a simple way of converting a date in seconds to normal date

Hi all! I'm working on a HPUX system, and I was wondering if there is a simple way to convert a date from seconds (since 1970) to a normal date. Thanks (2 Replies)
Discussion started by: travian
2 Replies

9. Shell Programming and Scripting

date formatting

Date format MM/DD/YYYY required is YYYYMMDD, I tried using sed but could not get it any help please. (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Formatting date

i need date in the following format December 14, 2005. With date +"%b %d, %Y" command i am getting the following output :- Dec 14, 2005. can anyone pls tell me how to get the full month name (2 Replies)
Discussion started by: radhika03
2 Replies
Login or Register to Ask a Question