tm_yday to time_t?

Discussion in 'C Programming' started by bob@jfcl.com, Jun 30, 2006.

  1. Guest

    If I have a year and the day of the year (e.g. yyyy.ddd), is there
    some simple way to convert this to a time_t? mktime(), of course, does
    just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.

    There's the obvious brute force approach of subtracting 31, 28, 31,
    30, etc... until you arrive at the right result (being careful to allow
    for leap years, of course), but it seems like there ought to be some
    standard library routine for this purpose.

    It only has to work for "modern" days, post 1970.

    Thanks,
    Bob Armstrong
     
    , Jun 30, 2006
    #1
    1. Advertising

  2. Al Balmer Guest

    On 30 Jun 2006 15:14:32 -0700, wrote:

    >
    > If I have a year and the day of the year (e.g. yyyy.ddd), is there
    >some simple way to convert this to a time_t? mktime(), of course, does
    >just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.
    >
    > There's the obvious brute force approach of subtracting 31, 28, 31,
    >30, etc... until you arrive at the right result (being careful to allow
    >for leap years, of course), but it seems like there ought to be some
    >standard library routine for this purpose.
    >
    > It only has to work for "modern" days, post 1970.


    Google for "serial date" or "Julian date". You should find algorithms
    that can be adapted. I'd say more, but I'm out of here until next
    Wednesday!

    --
    Al Balmer
    Sun City, AZ
     
    Al Balmer, Jul 1, 2006
    #2
    1. Advertising

  3. Nelu Guest

    writes:

    > If I have a year and the day of the year (e.g. yyyy.ddd), is there
    > some simple way to convert this to a time_t? mktime(), of course, does
    > just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.
    >
    > There's the obvious brute force approach of subtracting 31, 28, 31,
    > 30, etc... until you arrive at the right result (being careful to allow
    > for leap years, of course), but it seems like there ought to be some
    > standard library routine for this purpose.
    >
    > It only has to work for "modern" days, post 1970.
    >


    So, you have the year and the day of the year and you want to get the
    right year, month and day (or the right time_t value based on those
    values)?

    mktime() need a pointer to a struct tm because it modifies it's
    argument, meaning that it normalizes your date. For example, the
    following program:

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

    int main(void) {

    struct tm mytm;

    mytm.tm_year=2006-1900;
    mytm.tm_mon=0;
    mytm.tm_mday=365;
    mytm.tm_isdst=-1;
    mytm.tm_hour=0;
    mytm.tm_min=0;
    mytm.tm_sec=0;

    if(mktime(&mytm)==-1) {
    printf("I cannot represent the calendar time :-(\n");
    exit(EXIT_FAILURE);
    }

    printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mon,mytm.tm_mday);

    return 0;
    }
    </code>

    Will output
    2006 11 31
    (if mktime can represent the time).

    The values of the 6 fields that matter for mktime (I don't count
    "dst" here, now) don't have to be in the specified ranges and mktime
    will change it's argument by setting the components to the correct
    values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
    more than the max number), hour=0, min=0 and sec=0 the values that
    exceed the ranges will be added (or substracted) to the higher group
    i.e. mday=32 means 31 day in January + 1 the next month which will
    make mon=1 and leave everything else unchanged.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Jul 1, 2006
    #3
  4. Guest

    Nelu wrote:
    > mytm.tm_mday=365;
    > ....


    That is just extraordinarily clever! And it actually works, too :)

    I knew there was some easier way to do it.

    Thanks,
    Bob
     
    , Jul 1, 2006
    #4
  5. Joe Wright Guest

    Nelu wrote:
    > writes:
    >
    >> If I have a year and the day of the year (e.g. yyyy.ddd), is there
    >> some simple way to convert this to a time_t? mktime(), of course, does
    >> just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.
    >>
    >> There's the obvious brute force approach of subtracting 31, 28, 31,
    >> 30, etc... until you arrive at the right result (being careful to allow
    >> for leap years, of course), but it seems like there ought to be some
    >> standard library routine for this purpose.
    >>
    >> It only has to work for "modern" days, post 1970.
    >>

    >
    > So, you have the year and the day of the year and you want to get the
    > right year, month and day (or the right time_t value based on those
    > values)?
    >
    > mktime() need a pointer to a struct tm because it modifies it's
    > argument, meaning that it normalizes your date. For example, the
    > following program:
    >
    > <code>
    > #include <stdio.h>
    > #include <time.h>
    > #include <stdlib.h>
    >
    > int main(void) {
    >
    > struct tm mytm;
    >
    > mytm.tm_year=2006-1900;
    > mytm.tm_mon=0;
    > mytm.tm_mday=365;
    > mytm.tm_isdst=-1;
    > mytm.tm_hour=0;
    > mytm.tm_min=0;
    > mytm.tm_sec=0;
    >
    > if(mktime(&mytm)==-1) {
    > printf("I cannot represent the calendar time :-(\n");
    > exit(EXIT_FAILURE);
    > }
    >
    > printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mon,mytm.tm_mday);
    >
    > return 0;
    > }
    > </code>
    >
    > Will output
    > 2006 11 31
    > (if mktime can represent the time).
    >
    > The values of the 6 fields that matter for mktime (I don't count
    > "dst" here, now) don't have to be in the specified ranges and mktime
    > will change it's argument by setting the components to the correct
    > values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
    > more than the max number), hour=0, min=0 and sec=0 the values that
    > exceed the ranges will be added (or substracted) to the higher group
    > i.e. mday=32 means 31 day in January + 1 the next month which will
    > make mon=1 and leave everything else unchanged.
    >

    November 31 ?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 1, 2006
    #5
  6. Nelu Guest

    Joe Wright <> writes:

    > Nelu wrote:
    > > writes:
    > >
    > >> If I have a year and the day of the year (e.g. yyyy.ddd), is there
    > >> some simple way to convert this to a time_t? mktime(), of course, does
    > >> just the opposite and uses tm_mon and tm_mday, ignoring tm_yday.
    > >>
    > >> There's the obvious brute force approach of subtracting 31, 28, 31,
    > >> 30, etc... until you arrive at the right result (being careful to allow
    > >> for leap years, of course), but it seems like there ought to be some
    > >> standard library routine for this purpose.
    > >>
    > >> It only has to work for "modern" days, post 1970.
    > >>

    > > So, you have the year and the day of the year and you want to get the
    > > right year, month and day (or the right time_t value based on those
    > > values)?
    > > mktime() need a pointer to a struct tm because it modifies it's
    > > argument, meaning that it normalizes your date. For example, the
    > > following program:
    > > <code>
    > > #include <stdio.h>
    > > #include <time.h>
    > > #include <stdlib.h>
    > > int main(void) {
    > > struct tm mytm;
    > > mytm.tm_year=2006-1900;
    > > mytm.tm_mon=0;
    > > mytm.tm_mday=365;
    > > mytm.tm_isdst=-1;
    > > mytm.tm_hour=0;
    > > mytm.tm_min=0;
    > > mytm.tm_sec=0;
    > > if(mktime(&mytm)==-1) {
    > > printf("I cannot represent the calendar time :-(\n");
    > > exit(EXIT_FAILURE);
    > > }
    > > printf("%d\t%d\t%d\n",mytm.tm_year+1900,mytm.tm_mon,mytm.tm_mday);
    > > return 0;
    > > }
    > > </code>
    > > Will output 2006 11 31
    > > (if mktime can represent the time).
    > > The values of the 6 fields that matter for mktime (I don't count
    > > "dst" here, now) don't have to be in the specified ranges and mktime
    > > will change it's argument by setting the components to the correct
    > > values. This means that if you say year=106, mon=0 (Jan) mday=32 (1
    > > more than the max number), hour=0, min=0 and sec=0 the values that
    > > exceed the ranges will be added (or substracted) to the higher group
    > > i.e. mday=32 means 31 day in January + 1 the next month which will
    > > make mon=1 and leave everything else unchanged.
    > >

    > November 31 ?
    >


    The month is 0 based. (0 - January, 11 - December). I set
    mytm.tm_mon=0. If you complain about 11 why don't you complain about
    0? :)

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Jul 1, 2006
    #6
  7. Nelu Guest

    Nelu <> writes:

    > Joe Wright <> writes:
    >
    > > Nelu wrote:
    > > > [snip]

    > > November 31 ?
    > >

    >
    > The month is 0 based. (0 - January, 11 - December). I set
    > mytm.tm_mon=0. If you complain about 11 why don't you complain about
    > 0? :)
    >


    Then again... scratch the last two sentences. Sorry. mday=365 is also out
    of range for day in month so it could've been interpreted that way for
    the month, too. I should've put tm_mon+1 instead on tm_mon in printf
    so it won't lead to confusion. I hope the OP got it right.

    --
    Ioan - Ciprian Tandau
    tandau _at_ freeshell _dot_ org (hope it's not too late)
    (... and that it still works...)
     
    Nelu, Jul 1, 2006
    #7
  8. Joe Wright Guest

    Nelu wrote:
    > Nelu <> writes:
    >
    >> Joe Wright <> writes:
    >>
    >>> Nelu wrote:
    >>>> [snip]
    >>> November 31 ?
    >>>

    >> The month is 0 based. (0 - January, 11 - December). I set
    >> mytm.tm_mon=0. If you complain about 11 why don't you complain about
    >> 0? :)
    >>

    >
    > Then again... scratch the last two sentences. Sorry. mday=365 is also out
    > of range for day in month so it could've been interpreted that way for
    > the month, too. I should've put tm_mon+1 instead on tm_mon in printf
    > so it won't lead to confusion. I hope the OP got it right.
    >

    Well, I apologize. tm_mon is indeed 0..11 (12 months) and tm_yday is
    0..365 (366 days).

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Jul 1, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFyaw==?=

    DateTime to time_t bug

    =?Utf-8?B?TWFyaw==?=, May 17, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    4,680
    Steven Cheng[MSFT]
    May 18, 2004
  2. Anand CS

    converting to time_t

    Anand CS, Jul 3, 2003, in forum: C Programming
    Replies:
    1
    Views:
    7,741
    Luke Driscoll
    Jul 3, 2003
  3. Dave Sinkula

    Casting (time_t)(-1)

    Dave Sinkula, Mar 2, 2004, in forum: C Programming
    Replies:
    4
    Views:
    641
    Dan Pop
    Mar 2, 2004
  4. j0mbolar

    time_t in seconds

    j0mbolar, Apr 18, 2004, in forum: C Programming
    Replies:
    6
    Views:
    10,290
    Dan Pop
    Apr 19, 2004
  5. Tomas Deman

    borland time_t conversion

    Tomas Deman, Jun 7, 2004, in forum: C Programming
    Replies:
    1
    Views:
    1,079
    Richard Bos
    Jun 7, 2004
Loading...

Share This Page