Date to second since 1970


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Date to second since 1970
# 1  
Old 06-16-2008
Date to second since 1970

Hello,
I have found a piece of code which converts seconds elapsed since 1970 for an input date. Can anyone please explain a little bit the code below:

Code:
#!/bin/bash
# function to reproduce UNIX time() value
second()
{
set -- $(TZ=GMT date '+%Y %m %d')
local y=$1 m=1$2 d=1$3
((m-=103, d-=101, m<0 && (m+=12, --y)))
((d+=(m*153 + 2)/5+y*365+y/4-y/100+y/400-719468))
echo $((d*86400))
}


Last edited by Yogesh Sawant; 06-16-2008 at 02:22 AM.. Reason: added code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Answers to Frequently Asked Questions

Compare date in .txt with system date and remove if it's lesser than system date

I m working on shell scripting and I m stuck where in my .txt file there is column as expiry date and I need to compare that date with system date and need to remove all the rows where expiry date is less than system date and create a new .txt with update. (1 Reply)
Discussion started by: Stuti
1 Replies

2. UNIX for Beginners Questions & Answers

Compare date in .txt with system date and remove if it's lesser than system date

Can someone help me with the code wherein there is a file f1.txt with different column and 34 column have expiry date and I need to get that and compare with system date and if expiry date is <system date remove those rows and other rows should be moved to new file f2.txt . I don't want to delete... (2 Replies)
Discussion started by: Stuti
2 Replies

3. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

4. Shell Programming and Scripting

Write Linux script to convert timestamps older than 1.1.1970 to 1.1.1980

I am having problems because some of my files have timestamps that are earlier that 1.1.1970, the Unix start of time convention. So I would like to write a script that finds all files in home folder and subfolders with timestamps earlier than 1.1.1970 and converts them to 1.1.1980. I... (3 Replies)
Discussion started by: francus
3 Replies

5. AIX

The result of Jan 01 (1970) by last command

Dears, My boss asked me to record the login information on AIX server, so I used "last" command to get i want. But it is so strange when i get a record the login date is Jan,01 1970 (as attached picture). does anyone know what happen? Thanks and Regards, (6 Replies)
Discussion started by: Zack.Chiang
6 Replies

6. Shell Programming and Scripting

Converting a date to friday date and finding Min/Max date

Dear all, I have 2 questions. I have a file with many rows which has date of the format YYYYMMDD. 1. I need to change the date to that weeks friday date(Ex: 20120716(monday) to 20120720). Satuday/Sunday has to be changed to next week friday date too. 2. After converting the date to... (10 Replies)
Discussion started by: 2001.arun
10 Replies

7. Shell Programming and Scripting

Check if a date field has date or timestamp or date&timestamp

Hi, In a field, I should receive the date with time stamp in a particular field. But sometimes the vendor sends just the date or the timestamp or correctl the date&timestamp. I have to figure out the the data is a date or time stamp or date&timestamp. If it is date then append "<space>00:00:00"... (1 Reply)
Discussion started by: machomaddy
1 Replies

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

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

10. UNIX for Dummies Questions & Answers

days since 1970

How can I get the syatem to give me the days since 1970 (as seen in the shadow file)? (4 Replies)
Discussion started by: 98_1LE
4 Replies
Login or Register to Ask a Question
gettimeofday(2) 						System Calls Manual						   gettimeofday(2)

Name
       gettimeofday, settimeofday - get or set date and time

Syntax
       #include <sys/time.h>

       gettimeofday(tp, tzp)
       struct timeval *tp;
       struct timezone *tzp;

       settimeofday(tp, tzp)
       struct timeval *tp;
       struct timezone *tzp;

Description
       The  system  call returns the system's notion of the current Greenwich time and the current time zone.  Time returned is expressed relative
       in seconds and microseconds since midnight January 1, 1970.

       The structures pointed to by tp and tzp are defined in <sys/time.h> as:

       struct timeval {
	    long tv_sec;	/* seconds since Jan. 1, 1970 */
	    long tv_usec;  /* and microseconds */
       };

       struct timezone {
	    int  tz_minuteswest;     /* of Greenwich */
	    int  tz_dsttime;	/* type of dst correction to apply */
       };

       The timezone structure indicates the local time zone (measured in minutes of time westward from Greenwich), and a flag  that,  if  nonzero,
       indicates that Daylight Saving time applies locally during the appropriate part of the year.

       Only the superuser can set the time of day.

Return Values
       A  0  return  value  indicates  that  the call succeeded.  A -1 return value indicates an error occurred, and in this case an error code is
       stored into the global variable errno.

Diagnostics
       The call fails under the following conditions:

       [EFAULT]       An argument address referenced invalid memory.

       [EPERM]	      A user other than the superuser attempted to set the time.

See Also
       date(1), stime(2), ctime(3)

																   gettimeofday(2)