Network time to C time

Discussion in 'C Programming' started by Andrew Chalk, Nov 4, 2007.

  1. Andrew Chalk

    Andrew Chalk Guest

    Network time uses 1900 as its base. The C time library uses 1970. I want to
    convert a network time value to a C library time. I.e. I have to subtract
    the number of seconds from 1900 to 1970. Is there any library function to do
    this?

    Thanks.
     
    Andrew Chalk, Nov 4, 2007
    #1
    1. Advertising

  2. Andrew Chalk

    CBFalconer Guest

    Andrew Chalk wrote:
    >
    > Network time uses 1900 as its base. The C time library uses 1970.
    > I want to convert a network time value to a C library time. I.e.
    > I have to subtract the number of seconds from 1900 to 1970. Is
    > there any library function to do this?


    For heavens sake, why? That number is a constant. Just subtract
    it. And all this is off-topic anyhow.

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Nov 4, 2007
    #2
    1. Advertising

  3. Andrew Chalk

    Richard Guest

    CBFalconer <> writes:

    > Andrew Chalk wrote:
    >>
    >> Network time uses 1900 as its base. The C time library uses 1970.
    >> I want to convert a network time value to a C library time. I.e.
    >> I have to subtract the number of seconds from 1900 to 1970. Is
    >> there any library function to do this?

    >
    > For heavens sake, why? That number is a constant. Just subtract
    > it. And all this is off-topic anyhow.


    How the hell is asking about the existence for a C library function to
    manipulate dates and times "Off Topic"?!?!?!?! Streuth.

    >
    > --
    > Chuck F (cbfalconer at maineline dot net)
    > <http://cbfalconer.home.att.net>
    > Try the download section.
     
    Richard, Nov 4, 2007
    #3
  4. In article <>,
    Richard Heathfield <> wrote:
    >CBFalconer said:
    >
    >> Andrew Chalk wrote:
    >>>
    >>> Network time uses 1900 as its base. The C time library uses 1970.
    >>> I want to convert a network time value to a C library time. I.e.
    >>> I have to subtract the number of seconds from 1900 to 1970. Is
    >>> there any library function to do this?

    >>
    >> For heavens sake, why? That number is a constant. Just subtract
    >> it.

    >
    >Hardly a constant. Not all C libraries use 1970 as their base, so the
    >number of seconds between the network epoch and a C library epoch is in
    >fact not constant at all.


    True. Because "networks" are not mentioned in the C standards documents.
    And, I think, neither are "epochs".

    >> And all this is off-topic anyhow.

    >
    >Why?


    Because "networks" are not mentioned in the C standards documents.
     
    Kenny McCormack, Nov 4, 2007
    #4
  5. In article <>,
    CBFalconer <> wrote:
    >Andrew Chalk wrote:


    >> Network time uses 1900 as its base. The C time library uses 1970.
    >> I want to convert a network time value to a C library time. I.e.
    >> I have to subtract the number of seconds from 1900 to 1970. Is
    >> there any library function to do this?


    >For heavens sake, why? That number is a constant. Just subtract
    >it.


    Having it as a library function would save questions about (e.g.,)
    whether either (or both) libraries take leap-seconds into account.

    Also, the number of seconds between 1900 and 1970 is not representable
    in a signed long (just misses -- signed long gets you a little over
    68 years). That implies that if network time is defined as an
    unsigned long that it runs out before C time runs out; if it is defined
    some other way then a straight-forward subtraction is not appropriate
    in C89. (Possibly network time is defined as long long, but if so then
    it wouldn't be representable in C89, which seems unlikely.)
    --
    "There are some ideas so wrong that only a very intelligent person
    could believe in them." -- George Orwell
     
    Walter Roberson, Nov 4, 2007
    #5
  6. CBFalconer said:

    > Andrew Chalk wrote:
    >>
    >> Network time uses 1900 as its base. The C time library uses 1970.
    >> I want to convert a network time value to a C library time. I.e.
    >> I have to subtract the number of seconds from 1900 to 1970. Is
    >> there any library function to do this?

    >
    > For heavens sake, why? That number is a constant. Just subtract
    > it.


    Hardly a constant. Not all C libraries use 1970 as their base, so the
    number of seconds between the network epoch and a C library epoch is in
    fact not constant at all.

    > And all this is off-topic anyhow.


    Why?

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Nov 4, 2007
    #6
  7. "Andrew Chalk" <> writes:
    > Network time uses 1900 as its base. The C time library uses 1970. I
    > want to convert a network time value to a C library time. I.e. I
    > have to subtract the number of seconds from 1900 to 1970. Is there
    > any library function to do this?


    No, there's no standard library function to do this. There may well
    be a non-standard function; a newsgroup that deals with your operating
    system is likely to be more helpful.

    Incidentally, standard C doesn't require time_t to represent seconds
    since 1970, though that's the most common implementation.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 4, 2007
    #7
  8. CBFalconer <> writes:
    > Andrew Chalk wrote:
    >> Network time uses 1900 as its base. The C time library uses 1970.
    >> I want to convert a network time value to a C library time. I.e.
    >> I have to subtract the number of seconds from 1900 to 1970. Is
    >> there any library function to do this?

    >
    > For heavens sake, why? That number is a constant. Just subtract
    > it. And all this is off-topic anyhow.


    No, the question isn't off-topic (in comp.lang.c); the answer just
    happens to be no.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 4, 2007
    #8
  9. Andrew Chalk

    Andrew Chalk Guest

    And that constant is, what?

    "CBFalconer" <> wrote in message
    news:...
    > Andrew Chalk wrote:
    >>
    >> Network time uses 1900 as its base. The C time library uses 1970.
    >> I want to convert a network time value to a C library time. I.e.
    >> I have to subtract the number of seconds from 1900 to 1970. Is
    >> there any library function to do this?

    >
    > For heavens sake, why? That number is a constant. Just subtract
    > it. And all this is off-topic anyhow.
    >
    > --
    > Chuck F (cbfalconer at maineline dot net)
    > <http://cbfalconer.home.att.net>
    > Try the download section.
    >
    >
    >
    > --
    > Posted via a free Usenet account from http://www.teranews.com
    >
     
    Andrew Chalk, Nov 4, 2007
    #9
  10. Andrew Chalk

    Thad Smith Guest

    Andrew Chalk wrote:
    > Network time uses 1900 as its base. The C time library uses 1970. I want to
    > convert a network time value to a C library time. I.e. I have to subtract
    > the number of seconds from 1900 to 1970. Is there any library function to do
    > this?


    You have received earlier comments that
    1. the number of seconds is a constant so you can simply subtract it
    2. the number of seconds is not constants, since different C
    implementations use different starting points
    3. you may need to consider leap seconds and might not be able to
    represent the time in a single C89 variable

    These are all valid comments, in general. The simple problem
    description leaves details open to interpretation.

    For a given pair of defined time scales, i.e., a particular network time
    specification and a particular C library implementation which uses a
    representation of number of elapsed seconds since a starting point,
    there is likely a single value to can be used to convert one time scale
    to another.

    If you a writing a portable implementation for all Standard C
    implementations, but a single network standard, you can convert the
    network time to a struct tm representation of local time, then use
    mktime() to obtain the time_t representation.

    If it is important to accurately handle leap seconds, you need to
    consult the specification for your network time representation and
    verify that your target C libraries have adequate handling. For most
    uses it doesn't matter.

    --
    Thad
     
    Thad Smith, Nov 4, 2007
    #10
  11. "Andrew Chalk" <> writes:
    > "CBFalconer" <> wrote in message
    > news:...
    >> Andrew Chalk wrote:
    >>> Network time uses 1900 as its base. The C time library uses 1970.
    >>> I want to convert a network time value to a C library time. I.e.
    >>> I have to subtract the number of seconds from 1900 to 1970. Is
    >>> there any library function to do this?

    >>
    >> For heavens sake, why? That number is a constant. Just subtract
    >> it. And all this is off-topic anyhow.
    >>

    > And that constant is, what?


    Please don't top-post. Read the following:

    http://www.caliburn.nl/topposting.html
    http://www.cpax.org.uk/prg/writings/topposting.php

    Please don't quote signatures (the stuff following the "-- " line)
    unless you're actually commenting on them. More generally, when you
    post a followup, retain just enough of the parent article so your
    reply makes sense on its own.

    I've corrected both here.

    The number of seconds between midnight 1900-01-01 and midnight
    1970-01-01 is 2208988800. (That ignores leap seconds, but many time
    representations also ignore leap seconds, which weren't introduced
    until 1972 anyway.)

    Knowing that might or might not be helpful. Though the C standard
    says remarkably little about how times are represented, many systems
    use a 32-bit signed integer representing seconds since 1970-01-01;
    some systems do the same thing, but with a 64-bit signed intger.
    (Using 1970-01-01 is a Unix standard, not a C standard, but it's
    spread to some other operating systems for compatibility reasons.)

    The value 2208988800 is (barely) too large to be represented in a
    32-bit signed integer. There are probably ways around that, but
    they'll depend on exactly how your "network time" is represented.
    Adding 1104494400 twice *might* be a solution.

    It's very likely that there's already a function that will reliably
    convert between "network time" and "Unix time". You should ask in
    comp.unix.programmer rather than soliciting guesses in comp.lang.c.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 4, 2007
    #11
  12. On Sun, 4 Nov 2007 09:59:38 -0600, "Andrew Chalk"
    <> wrote:

    >Network time uses 1900 as its base. The C time library uses 1970. I want to
    >convert a network time value to a C library time. I.e. I have to subtract
    >the number of seconds from 1900 to 1970. Is there any library function to do
    >this?


    While Unix may use 1970 as time 0, the standard does not require any
    of the library functions to do so.

    Furthermore, the representation of time is not required to be in
    seconds. (For example, Excel uses a floating point value of days and
    fractions thereof.)

    You can compute the value you need using the difftime function.


    Remove del for email
     
    Barry Schwarz, Nov 5, 2007
    #12
  13. In article <>,
    Barry Schwarz <> wrote:
    >On Sun, 4 Nov 2007 09:59:38 -0600, "Andrew Chalk"
    ><> wrote:
    >
    >>Network time uses 1900 as its base. The C time library uses 1970. I want to
    >>convert a network time value to a C library time. I.e. I have to subtract
    >>the number of seconds from 1900 to 1970. Is there any library function to do
    >>this?

    >
    >While Unix may use 1970 as time 0, the standard does not require any
    >of the library functions to do so.
    >
    >Furthermore, the representation of time is not required to be in
    >seconds. (For example, Excel uses a floating point value of days and
    >fractions thereof.)


    I doubt Excel is mentioned in the C standards documents.

    Therefore, it is OT here.
     
    Kenny McCormack, Nov 5, 2007
    #13
  14. Barry Schwarz <> writes:
    > On Sun, 4 Nov 2007 09:59:38 -0600, "Andrew Chalk"
    > <> wrote:
    >>Network time uses 1900 as its base. The C time library uses 1970. I want to
    >>convert a network time value to a C library time. I.e. I have to subtract
    >>the number of seconds from 1900 to 1970. Is there any library function to do
    >>this?

    >
    > While Unix may use 1970 as time 0, the standard does not require any
    > of the library functions to do so.
    >
    > Furthermore, the representation of time is not required to be in
    > seconds. (For example, Excel uses a floating point value of days and
    > fractions thereof.)


    True, though Excel's time representation presumably isn't time_t.
    Probably Excel runs on a system where time_t represents seconds since
    1970 (in the C implementation if not in the operating system), and it
    defines its own internal type for representing dates and times. But
    yes, a C implementation *could* define time_t as a floating-point type
    representing days and fractions thereof.

    > You can compute the value you need using the difftime function.


    Um, maybe. difftime() computes the difference between two time_t
    values, yielding a double result. But there's no guarantee that
    1900-01-01 00:00:00 can be represented as a time_t (and on many
    systems it can't). For that matter, there's no guarantee that
    1970-01-01 00:00:00 is within the range of time_t (though I suspect it
    is under all existing implementations).

    But the computation isn't difficult. It's 70 years of 365 days each,
    each day being 86400 seconds (24 * 60 * 60), plus 17 leap days of
    86400 seconds each (1900 was not a leap year).

    I'm assuming that this "network time" uses 1900-01-01 as its epoch.
    The OP just said 1900, and I've think I've heard of representations
    that use 1900-03-01 or 1901-01-01 to avoid the leap year irregularity.
    If the OP is referring to NTP, then yes, the epoch is 1900-01-01 (and
    the representation, which uses 32 bits for the whole number of seconds
    and 32-bits for the fraction, will wrap around in 2036).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 5, 2007
    #14
  15. Andrew Chalk

    Joe Wright Guest

    Keith Thompson wrote:
    > Barry Schwarz <> writes:
    >> On Sun, 4 Nov 2007 09:59:38 -0600, "Andrew Chalk"
    >> <> wrote:
    >>> Network time uses 1900 as its base. The C time library uses 1970. I want to
    >>> convert a network time value to a C library time. I.e. I have to subtract
    >>> the number of seconds from 1900 to 1970. Is there any library function to do
    >>> this?

    >> While Unix may use 1970 as time 0, the standard does not require any
    >> of the library functions to do so.
    >>
    >> Furthermore, the representation of time is not required to be in
    >> seconds. (For example, Excel uses a floating point value of days and
    >> fractions thereof.)

    >
    > True, though Excel's time representation presumably isn't time_t.
    > Probably Excel runs on a system where time_t represents seconds since
    > 1970 (in the C implementation if not in the operating system), and it
    > defines its own internal type for representing dates and times. But
    > yes, a C implementation *could* define time_t as a floating-point type
    > representing days and fractions thereof.
    >
    >> You can compute the value you need using the difftime function.

    >
    > Um, maybe. difftime() computes the difference between two time_t
    > values, yielding a double result. But there's no guarantee that
    > 1900-01-01 00:00:00 can be represented as a time_t (and on many
    > systems it can't). For that matter, there's no guarantee that
    > 1970-01-01 00:00:00 is within the range of time_t (though I suspect it
    > is under all existing implementations).
    >
    > But the computation isn't difficult. It's 70 years of 365 days each,
    > each day being 86400 seconds (24 * 60 * 60), plus 17 leap days of
    > 86400 seconds each (1900 was not a leap year).
    >
    > I'm assuming that this "network time" uses 1900-01-01 as its epoch.
    > The OP just said 1900, and I've think I've heard of representations
    > that use 1900-03-01 or 1901-01-01 to avoid the leap year irregularity.
    > If the OP is referring to NTP, then yes, the epoch is 1900-01-01 (and
    > the representation, which uses 32 bits for the whole number of seconds
    > and 32-bits for the fraction, will wrap around in 2036).
    >

    The idea that seconds since 1970 will wrap in 2036 assumes an integer
    and 31 bits, not 32. With 32 bits we can live into 2109.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
     
    Joe Wright, Nov 6, 2007
    #15
  16. Andrew Chalk

    Richard Bos Guest

    Joe Wright <> wrote:

    > Keith Thompson wrote:
    > > I'm assuming that this "network time" uses 1900-01-01 as its epoch.
    > > The OP just said 1900, and I've think I've heard of representations
    > > that use 1900-03-01 or 1901-01-01 to avoid the leap year irregularity.
    > > If the OP is referring to NTP, then yes, the epoch is 1900-01-01 (and
    > > the representation, which uses 32 bits for the whole number of seconds
    > > and 32-bits for the fraction, will wrap around in 2036).
    > >

    > The idea that seconds since 1970 will wrap in 2036 assumes an integer
    > and 31 bits, not 32. With 32 bits we can live into 2109.


    No, it's NTP, with 32 bits unsigned, and hence 136.summat years, since
    1900, which will overflow in 2036. As noted, BTW, in the relevant RFC.

    Richard
     
    Richard Bos, Nov 6, 2007
    #16
  17. Joe Wright <> writes:
    > Keith Thompson wrote:

    [...]
    >> If the OP is referring to NTP, then yes, the epoch is 1900-01-01 (and
    >> the representation, which uses 32 bits for the whole number of seconds
    >> and 32-bits for the fraction, will wrap around in 2036).
    >>

    > The idea that seconds since 1970 will wrap in 2036 assumes an integer
    > and 31 bits, not 32. With 32 bits we can live into 2109.


    NTP uses a 32-bit unsigned integer to represent seconds since 1900
    (plus another 32 bits to represent fractional seconds); this wraps
    around in 2036. Many Unix-like systems use a 32-bit signed integer to
    represent seconds since 1970; this overflows in 2038.

    For Unix-like systems, switching from 32-bit signed to 32-bit unsigned
    would delay the overflow to 2106 (not 2109). However, both NTP and
    Unix are migrating to 64 bits, so there's no need to use unsigned
    integers and thereby lose the ability to represent dates before the
    epoch.

    These are, of course, just examples of possible representations for
    time_t.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 6, 2007
    #17
  18. CBFalconer <> writes:

    > Andrew Chalk wrote:
    >>
    >> Network time uses 1900 as its base. The C time library uses 1970.
    >> I want to convert a network time value to a C library time. I.e.
    >> I have to subtract the number of seconds from 1900 to 1970. Is
    >> there any library function to do this?

    >
    > For heavens sake, why? That number is a constant. Just subtract
    > it. And all this is off-topic anyhow.


    Is it? Does it include, for example, leap seconds?

    Chip

    --
    Charles M. "Chip" Coldwell
    "Turn on, log in, tune out"
    Somerville, Massachusetts, New England
     
    Chip Coldwell, Nov 6, 2007
    #18
  19. In article <>,
    Chip Coldwell <> wrote:
    >CBFalconer <> writes:
    >
    >> Andrew Chalk wrote:
    >>>
    >>> Network time uses 1900 as its base. The C time library uses 1970.
    >>> I want to convert a network time value to a C library time. I.e.
    >>> I have to subtract the number of seconds from 1900 to 1970. Is
    >>> there any library function to do this?

    >>
    >> For heavens sake, why? That number is a constant. Just subtract
    >> it. And all this is off-topic anyhow.

    >
    >Is it?


    It is OT because there is no mention of networks in the C standards
    documents. In the dogma of this ng, the instant you mention something
    not mentioned in the C standards documents, you lose.

    >Does it include, for example, leap seconds?


    Others have pointed out that there were no leap seconds prior to 1970.
     
    Kenny McCormack, Nov 6, 2007
    #19
  20. Andrew Chalk

    Richard Guest

    (Kenny McCormack) writes:

    > In article <>,
    > Chip Coldwell <> wrote:
    >>CBFalconer <> writes:
    >>
    >>> Andrew Chalk wrote:
    >>>>
    >>>> Network time uses 1900 as its base. The C time library uses 1970.
    >>>> I want to convert a network time value to a C library time. I.e.
    >>>> I have to subtract the number of seconds from 1900 to 1970. Is
    >>>> there any library function to do this?
    >>>
    >>> For heavens sake, why? That number is a constant. Just subtract
    >>> it. And all this is off-topic anyhow.

    >>
    >>Is it?

    >
    > It is OT because there is no mention of networks in the C standards
    > documents. In the dogma of this ng, the instant you mention something
    > not mentioned in the C standards documents, you lose.
    >
    >>Does it include, for example, leap seconds?

    >
    > Others have pointed out that there were no leap seconds prior to 1970.


    Are leap seconds mentioned in the C-Standard? ...
     
    Richard, Nov 6, 2007
    #20
    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. Kal
    Replies:
    1
    Views:
    9,537
    Kevin Spencer
    Jun 21, 2004
  2. Jane Davis

    Network Service account over network

    Jane Davis, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    457
    Kevin Spencer
    Jun 22, 2005
  3. Bill Volk
    Replies:
    1
    Views:
    3,156
    Bill Volk
    Jul 2, 2003
  4. kin
    Replies:
    0
    Views:
    922
  5. king
    Replies:
    1
    Views:
    287
Loading...

Share This Page