LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C built-in function for a Bash script (https://www.linuxquestions.org/questions/programming-9/c-built-in-function-for-a-bash-script-173447/)

Linh 04-22-2004 04:15 PM

C built-in function for a Bash script
 
Bash script
now=`date "+%s"`
The above Bash script convert today 's date to a number.

1) Are the year, month, date, hour, minutes, seconds
and thousands of a second involved in this conversion ?

2) What is the mathematical formular for this conversion ?

3) What is the C built-in function for the above Bash
script ?

jlliagre 04-23-2004 03:08 AM

This is a 32 bit unsigned integer representing the number of seconds since what is called the epoch, January the 1st, 1970 UTC.

Hko 04-23-2004 04:26 AM

Re: C built-in function for a Bash script
 
Quote:

Originally posted by Linh
Bash script
now=`date "+%s"`
The above Bash script convert today 's date to a number.

1) Are the year, month, date, hour, minutes, seconds
and thousands of a second involved in this conversion ?

No, just seconds. But you can get the nanoseconds part of the time since the epoch with: date "+%N"
Or in C using the gettimeofday() function.

Quote:

2) What is the mathematical formular for this conversion ?
None actually. It's the other way around: The system (kernel) base format for the current time is in "number of seconds since the epoch". All other time formats are conversions from this.

Quote:

3) What is the C built-in function for the above Bash
script ?

That is the time() function. This example below does the same as: date "+%s".
Code:

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

int main()
{
        printf("%ld\n", (long int)time(NULL));
        return 0;
}


Linh 04-23-2004 09:23 AM

reply
 
jlliagre, and Hko. Thank you both for your help.


All times are GMT -5. The time now is 01:12 AM.