Converts the number in the/proc/uptime file to long and then to the date

Source: Internet
Author: User

Ideas:
1, formatted to read the two number of files, saved in two strings.
2,/proc/uptime in seconds, discard decimal. Converts the integer part to long.
3, from long to date.

#include <stdio.h>

int main ()
{
    FILE *fp = NULL;
    fp = fopen ("uptime.c", "R");
    if (fp = = NULL)
    {
        printf ("Open File failed!\n");
        return 0;
    }
    Save two numbers, the length is slightly larger than the number of files
    //You can use Cat/proc/uptime in advance to see
    char t[20];
    Char u[20];
    FSCANF (FP, "%s", T, u);

    Count the number of integer digits of the first number, and turn to long, omitting the 2nd number processing
   unsigned long dt = 0;
    int i;
    For (i=0 i<20; i++)
    {
        if (t[i] = = '. ')
            break;
    int j = 1;
    For (i--i>=0; i--)
    {
        dt + = (t[i]-' 0 ') * j;
        J *=;

    Long turns to date and outputs
    unsigned long day, hour, minute, second;
    Day = dt/(60*60*24);
    DT = dt% (60*60*24);
    hour = DT/(60*60);
    DT = dt% (60*60);
    minute = DT/60;
    second = dt%;

    printf ("%ld:%ld:%ld:%ld\n", day, hour, minute, second);

    Fclose (FP);
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.