Working with bash and date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Working with bash and date
# 8  
Old 01-20-2011
If you have a C compiler available on solaris I've written a little C program that prints a time with secs adjustment (positive or negative) you can also specify the output format but it defaults to %d/%b/%Y %T

dateadj.c
Code:
#include <time.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
   long long adj = 0;
   char res[1024], fmt[1024] = "%d/%b/%Y %T";
   time_t secs_now;
   struct tm *time_now = (struct tm *)malloc(sizeof(struct tm));
   if (argc > 1) adj=atoll(argv[argc-1]);
   if (argc > 2) strcpy(fmt, argv[1]);
   if (argc > 1 && *argv[argc-1] == '@') secs_now=atoll(argv[argc-1]+1);
   else secs_now = time(NULL)+adj;
   *time_now = *localtime(&secs_now);
   strftime(res, 1024, fmt, time_now);
   free(time_now);
   puts(res);
   return 0;
}

Compile with cc -o dateadj dateadj.c, or replace cc with gcc if you have it.

Some usage examples:
Code:
$ ./dateadj
21/Jan/2011 08:15:22
$ ./dateadj -60
21/Jan/2011 08:14:26
$ ./dateadj '%H%M' -60
0814
$ ./dateadj @1294581600 
10/Jan/2011 00:00:00


Last edited by Chubler_XL; 01-20-2011 at 05:44 PM..
# 9  
Old 01-20-2011
@Chubler_XL: Thanks. unfortunately I don't have a C compiler on board, but I'll keep your code just in case! Smilie

@Tytalus: Thanks for your feedback, your example works:
root@rms# perl -e 'print scalar(localtime(time()-60))."\n"'
Thu Jan 20 15:19:51 2011

Now, how can I extract the hour an minute values??? Smilie

---------- Post updated at 11:08 AM ---------- Previous update was at 09:25 AM ----------

Solved myself! ^_^

This is for the minute:
Code:
timetmp=$(perl -e 'print scalar(localtime(time()-60))'|awk '{printf $4}' |awk -F":" '{printf $2}')

Btw, thanks to all for tips! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Best way to get a bash script working in C

Ahoy friends. Currently i got a bash script running to manage my minecraft servers. All of them are stored in /home/minecraft_servers directory. Using my script im able to start a server (e.g. ./minecraft start ftb_continuum) because server name and server name are the same.(e.g.... (2 Replies)
Discussion started by: Knogle
2 Replies

2. UNIX for Beginners Questions & Answers

How bash treats literal date value and retrieve year, month and date?

Hi, I am trying to add few (say 3 days) to sysdate using - date -d '+ 3 days' +%y%m%d and it works as expected. But how to add few (say 3 days) to a literal date value and how bash treats a literal value as a date. Can we say just like in ORACLE TO_DATE that my given literal date value... (2 Replies)
Discussion started by: pointers1234
2 Replies

3. Shell Programming and Scripting

Script to get last working date

Problem : we need a command that will give previous day date and previous day should be mon-fri only. Example :if we are running a command on Monday , it will give Friday ‘s date not Sunday’s . Few more restrictions : Command should be working in NM n/w. Hard coding of date should be avoided.... (11 Replies)
Discussion started by: Downsouth
11 Replies

4. UNIX for Dummies Questions & Answers

Date Validation not working

Hi Experts, I have a date validation script in that i will validate the date for a given format and search in the logs for that date. The script logic is very simple like below. Validate_Date() { is_valid=1 while do date_format=$(date "+$1") echo -e "Please enter the $2 date like... (6 Replies)
Discussion started by: senthil.ak
6 Replies

5. Shell Programming and Scripting

Date command is not working properly

Hi, in my script, i take the last month by a=$(date --date '1 month ago' +%Y%m) i expect that it give me in this month "March" as result 201402, but linux gave me 201403. IMPe@ABC123:> ~/date --date '1 month ago' +%Y%m 201403 i'm reasonably confused. Any idea? Thanks in advance, ... (2 Replies)
Discussion started by: IMPe
2 Replies

6. Shell Programming and Scripting

Working with grep and Bash

Hi, I am currently working on a Bash shell script that - Downloads a webpage, in this case youtube.com - Extracts Number of views, Extracts Title of video, Extracts User who made it, and lastly Duration. Then I have to Out put this into columns. To me this sounds like crazyness. I'm very new... (6 Replies)
Discussion started by: Njzangel
6 Replies

7. Shell Programming and Scripting

date command not working

hi #!usr/bin/perl -w local ($date) = `/sbin/date "+%D %X" ` ; print $date when i run this in ksh shell it is giving the below error sh: /sbin/date: not found but same code is working and displaying date and time in sh shell. what could be the reason. pls help (10 Replies)
Discussion started by: psthariharan
10 Replies

8. HP-UX

Why Bash is not working in HP-UX ?

Why Bash is not working in HP-UX ? What is similiar exe which is in HP_UX as Bash? (9 Replies)
Discussion started by: girija
9 Replies

9. Homework & Coursework Questions

Date comparison with 'string date having slashes and time zone' in Bash only

1. The problem statement, all variables and given/known data: I have standard web server log file. It contains different columns (like IP address, request result code, request type etc) including a date column with the format . I have developed a log analysis command line utility that displays... (1 Reply)
Discussion started by: TariqYousaf
1 Replies

10. Shell Programming and Scripting

working with date

I wan to convert any field having date in the file from the follwoing format 5/1/2003 to 2003-05-01. This file is tab delimited. Please help. (2 Replies)
Discussion started by: dsravan
2 Replies
Login or Register to Ask a Question