Calculate the current date.


 
Thread Tools Search this Thread
Top Forums Programming Calculate the current date.
# 1  
Old 11-03-2011
Calculate the current date.

Give your an integer (e.g. 0x0076f676) representing the number of minutes elapsed since January 1, 1996.
How to calculate the current date which format should be "year-month-day-hour-minutes" ?
# 2  
Old 11-03-2011
Quote:
Originally Posted by qcmao
Give your an integer (e.g. 0x0076f676) representing the number of minutes elapsed since January 1, 1996.
How to calculate the current date which format should be "year-month-day-hour-minutes" ?
I'm not quite following this.

Are you saying you want to work out what the current year month and day is from the number of minutes elapsed since January 1, 1996?
# 3  
Old 11-03-2011
This looked interesting, if a bit like homework. Tinker with this:
Code:
//Give your an integer (e.g. 0x0076f676) representing the number of minutes elapsed since January 1, 1996. 
//How to calculate the current date which format should be "year-month-day-hour-minutes" ?
// jan1.c
// usage:  ./jan1 0x0012345

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <strings.h>

time_t jan_1_1996(void)  // create artificial epoch seconds 
{
    struct tm j;
    j.tm_year= 1996 - 1900;
    j.tm_mon= 0;
    j.tm_mday=1;
    j.tm_hour=0;
    j.tm_min= 0;
    j.tm_sec= 0;
    j.tm_isdst=0;
    return (time_t)mktime(&j);
}

time_t
hex2dec(const char *hex)
{
    const char *p=hex;
    if(memcmp(p, "0x", 2)== 0) 
       p+=2;
    return (time_t) strtol(hex, (char **)0, 16);
}

void
get_date(const char *hex)
{
    time_t elapsed= jan_1_1996();
    char tmp[80]={0x0};
    elapsed+=( 60 * hex2dec(hex));
    strftime(tmp,80, "%Y-%m-%d-%H-%M", localtime(&elapsed) );
    printf("Corrected date is for %s: %s\n", hex, tmp);
}

int main(int argc, char **argv)
{
    if(argc==2)
      get_date(argv[1]);
    return 0;  
}

There is NO error checking in this code at all. Not meant for production systems.
# 4  
Old 11-03-2011
If you don't care about what language to use, Perl would be much easier.

Code:
#!/usr/bin/perl

use POSIX;

$t = mktime(0, 0, 0, 1, 0, 96);
$i = 0x0076f676 * 60;
$s = strftime "%Y-%m-%d-%H-%M", localtime($t + $i);
print "$s\n";


Last edited by MacMonster; 11-03-2011 at 12:19 PM.. Reason: Something duplicated.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace a parameter(variable) date value inside a text files daily with current date?

Hello All, we what we call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to... (1 Reply)
Discussion started by: pradeepp
1 Replies

2. UNIX for Beginners Questions & Answers

“sed” replace date in text file with current date

We want to call a parameter file (.txt) where my application read dynamic values when the job is triggered, one of such values are below: abc.txt ------------------ Code: line1 line2 line3 $$EDWS_DATE_INSERT=08-27-2019 line4 $$EDWS_PREV_DATE_INSERT=08-26-2019 I am trying to write a... (3 Replies)
Discussion started by: pradeepp
3 Replies

3. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

4. UNIX for Beginners Questions & Answers

UNIX script to replace old date with current date dynamically in multiple files present in a folder

I am trying to work on a script where it is a *(star) delimited file has a multiple lines starts with RTG and 3rd column=TD8 I want to substring the date part and I want to replace with currentdate minus 15 days. Here is an example. iam using AIX server $ cat temp.txt RTG*888*TD8*20180201~... (1 Reply)
Discussion started by: Shankar455
1 Replies

5. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

6. UNIX for Dummies Questions & Answers

Adding hours and minutes to current date (Only to date not to time)

Hi, I want to add some hours and minutes to the current date. For example, if the current date is "July 16, 2012 15:20", i want to add 5 hours 30 minutes to "July 16, 2012 00:00" not to "July 16, 2012 15:20". Please help. Thanks! (4 Replies)
Discussion started by: manojgarg
4 Replies

7. UNIX for Dummies Questions & Answers

Delete a row from a file if one column containing a date is greater than the current system date

Hello gurus, I am hoping someone can help me with the required code/script to make this work. I have the following file with records starting at line 4: NETW~US60~000000000013220694~002~~IT~USD~2.24~20110201~99991231~01~01~20101104~... (4 Replies)
Discussion started by: chumsky
4 Replies

8. Shell Programming and Scripting

Date One Week Ago From Given Date, Not From Current Date

Hi all, I've used various scripts in the past to work out the date last week from the current date, however I now have a need to work out the date 1 week from a given date. So for example, if I have a date of the 23rd July 2010, I would like a script that can work out that one week back was... (4 Replies)
Discussion started by: Donkey25
4 Replies

9. Shell Programming and Scripting

calculate the date of next satureday of current date.

I want to calculate the date of next satureday of current date using shell script. Suppose, today is 27-feb-08 I want to get the date of next satureday, which means 01-mar-08, in the formate '' YYMMDD ". I do this in ksh.. Please tell me any type of command which help me out. Thanks in... (3 Replies)
Discussion started by: rinku
3 Replies

10. Shell Programming and Scripting

Perl: Extracting date from file name and comparing with current date

I need to extract the date part from the file name (20080221 in this ex) and compare it with the current date and delete it, if it is a past date. $file = exp_ABCD4_T-2584780_upto_20080221.dmp.Z really appreciate any help. thanks mkneni (4 Replies)
Discussion started by: MKNENI
4 Replies
Login or Register to Ask a Question