seconds to YYYYMMDDHHMMSS

  • Follow


Hi folks,

i am in need to convert the number of seconds since 01/01/1970 UTC to
a human readable representation like constructed like, year, month,
day, hour, minute and seconds.

I can use any ready-to-use routine (I mean SUSV#/ANSI C). Is anybody
aware of theory reference for guiding me on this task?

Thanks a lot for your time and cooperation.

best regards.

PS: If possible, reply to me too.
0
Reply gustavo 11/5/2003 2:48:09 PM

Gustavo Rios wrote:
> Hi folks,
> 
> i am in need to convert the number of seconds since 01/01/1970 UTC to
> a human readable representation like constructed like, year, month,
> day, hour, minute and seconds.

localtime() and strftime()


-- 
Vale !
		Christianus Auriocus

0
Reply Christian 11/5/2003 3:11:36 PM


* Gustavo (gustavo.rios@terra.com.br) wrote:
> i am in need to convert the number of seconds since 01/01/1970 UTC to
> a human readable representation like constructed like, year, month,
> day, hour, minute and seconds.
> 
> I can use any ready-to-use routine (I mean SUSV#/ANSI C). Is anybody
> aware of theory reference for guiding me on this task?

printf("now = %s\n", asctime(gmtime(time)));

That's "human readable". If you need exactly the format in $subject
use strftime(3) instead of asctime(3).


ciao
-- 
"Unix was the first OS where you could carry the media and system
documentation around in a briefcase. This was fixed in BSD4.2."
0
Reply Philipp 11/6/2003 10:14:55 AM

[This followup was posted to comp.unix.programmer]

posting.google.com>, gustavo.rios@terra.com.br says...
> Hi folks,
> 
> i am in need to convert the number of seconds since 01/01/1970 UTC to
> a human readable representation like constructed like, year, month,
> day, hour, minute and seconds.
> 
> I can use any ready-to-use routine (I mean SUSV#/ANSI C). Is anybody
> aware of theory reference for guiding me on this task?
> 
> Thanks a lot for your time and cooperation.
> 
> best regards.
> 
> PS: If possible, reply to me too.
> 

You can used the "localtime" function as follows :



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

static	char	*months[12] = { "Jan" , "Feb" , "Mar" , "Apr" , "May" , 
			"Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , 
			"Dec" } ;
static	char	*weekdays[7] = { "Sun" , "Mon" , "Tue" , "Wed" , "Thu" ,
							"Fri" , "Sat" } ;

char *format_time_date(clock,buffer)
time_t clock; char *buffer;
{
	struct tm	*now;

	now = localtime(&clock);
	sprintf(buffer,"%s %s %d %02d:%02d:%02d %d",
		weekdays[now->tm_wday],months[now->tm_mon],
		now->tm_mday,now->tm_hour,now->tm_min,
		now->tm_sec,1900+now->tm_year);
	return(buffer);
} /* end of format_time_date */
0
Reply Master 11/6/2003 2:20:55 PM

Philipp Buehler <pb+usenet-kw45@usenet-nov-2003.fips.de> wrote in message news:<slrnbqk2q2.kmj.pb+usenet-kw45@tubb.sysfive.com>...
> * Gustavo (gustavo.rios@terra.com.br) wrote:
> > i am in need to convert the number of seconds since 01/01/1970 UTC to
> > a human readable representation like constructed like, year, month,
> > day, hour, minute and seconds.
> > 
> > I can use any ready-to-use routine (I mean SUSV#/ANSI C). Is anybody
> > aware of theory reference for guiding me on this task?
> 
> printf("now = %s\n", asctime(gmtime(time)));
> 
> That's "human readable". If you need exactly the format in $subject
> use strftime(3) instead of asctime(3).
> 
> 
> ciao

Folks! There was a mistaken in my email: In fact, the correct sentece
is:

	I can not use any read-to-use functions (ANSI C/SUSV#), i.e., i do
need to develop my own.

May some one help me?
0
Reply gustavo 11/6/2003 2:35:00 PM

* Gustavo (gustavo.rios@terra.com.br) wrote:
> 	I can not use any read-to-use functions (ANSI C/SUSV#), i.e., i do
> need to develop my own.

Oh.. :)

Well, go to some opensource OS like Linux or *BSD and look into 
the libc code? ;-)


ciao
-- 
"Unix was the first OS where you could carry the media and system
documentation around in a briefcase. This was fixed in BSD4.2."
0
Reply Philipp 11/6/2003 3:48:31 PM

On 6 Nov 2003 06:35:00 -0800, Gustavo Rios <gustavo.rios@terra.com.br> wrote:
>  Philipp Buehler <pb+usenet-kw45@usenet-nov-2003.fips.de> wrote in message news:<slrnbqk2q2.kmj.pb+usenet-kw45@tubb.sysfive.com>...
> > > i am in need to convert the number of seconds since 01/01/1970 UTC to
> > > a human readable representation like constructed like, year, month,
> > > day, hour, minute and seconds.
> > 
>  Folks! There was a mistaken in my email: In fact, the correct sentece
>  is:
>  
>  	I can not use any read-to-use functions (ANSI C/SUSV#), i.e., i do
>  need to develop my own.

You could use the time since midnight in conjunction with Julan dates.
See http://aa.usno.navy.mil/data/docs/JulianDate.html and 
    http://aa.usno.navy.mil/faq/docs/JD_Formula.html
for information on the JD calculation.  There was an article in the
July 2003 issue of Sys Admin magazine (www.sysadminmag.com) about JD.
I think that the source code is available from their web site.


Mike

-- 
Michael Zawrotny
Institute of Molecular Biophysics
Florida State University                | email:  zawrotny@sb.fsu.edu
Tallahassee, FL 32306-4380              | phone:  (850) 644-0069
0
Reply Michael 11/6/2003 5:15:27 PM

2003/11/6, 06:35(-08), Gustavo Rios:
[...]
>> > i am in need to convert the number of seconds since 01/01/1970 UTC to
>> > a human readable representation like constructed like, year, month,
>> > day, hour, minute and seconds.
[...]
> 	I can not use any read-to-use functions (ANSI C/SUSV#), i.e., i do
> need to develop my own.
[...]

Here is an implementation of strftime in awk (not much tested
but should be OK for Y/m/d/H/M/S/w).
Translating it to C syntax should be straightforward.

#! /usr/bin/awk -f
function floor(x \
              , t)
{
  t = int(x)
  return t - (x < t)
}

function rest(a, b)
{
  return a - floor(a / b) * b
}


function is_leap_year(Y) {
  return ((Y % 4) == 0 && \
	   ((Y % 100) != 0 || (Y % 400) == 0 || Y <= 1752))
}

function wide_strftime(fmt, t \
		       , fm, fd, tmp, i, c)
{
  # this is as the gawk strftime except that it accepts a far wider
  # range (provided your awk numbers are 64 bit large).
  # This one assumes a GMT timezone and a POSIX LC_TIME.
  # It should be valid from 292,271,021,077 BCE (Julian
  # Calendar, so, far before the Big Bang) to 292,277,026,596
  # (Gregorian Calendar).
  # The computed date is in the Gregorian Calendar from 1752/9/14 on and
  # in the Julian Calendar before (the day before is 1752/9/2) just as
  # the POSIX cal(1). This corresponds to the date when Great Britain
  # adopted the Gregorian calendar, that may be different in
  # other countries.
  # Appart from the directives defined by SUSv3, there's also %J which
  # gives the Julian day number (number of days since 4713/1/1 12:00
  # BCE), useful for google "daterange"s, and %s as in GNU strftime, the
  # t argument.
  # Dates before 0001/1/1 (there's no year 0) are noted -<n>. -0001/1/1
  # is January the 1st 0001 BCE.
  # Note that %g %C and %y are negative for a negative year.
  # the T array is not declared local, so you can access it
  # after a call to wide_strftime to access individual values.
  
  split("Sunday,Monday,Tuesday,Wednesday,Thursday," \
        "Friday,Saturday", fd, ",")
  split("January,February,March,April,May,June,July,August," \
        "September,October,November,December", fm, ",")
  T["s"] = sprintf("%.17g", t)
  T["d"] = floor(t / 86400)
  T["S"] = t - T["d"] * 86400
  T["d"] += 719468 
  T["J"] = T["d"] + 1721120 # Julian day
  T["w"] = rest(T["d"] + 3, 7)
  T["a"] = substr("SunMonTueWedThuFriSat", T["w"] * 3 + 1, 3)
  T["A"] = fd[T["w"] + 1]
  T["j"] = 60
  if (T["d"] < 640102)
    T["d"] += 2
  else {
    if (T["d"] < 640211) T["j"] -= 11
    tmp = floor((4 * T["d"] + 3) / 146097)
    T["d"] += tmp - floor(tmp / 4)
  }
  T["Y"] = floor((4 * T["d"] + 3) / 1461)
  T["d"] -= floor(T["Y"] * 1461 / 4)
  T["j"] += T["d"]
  T["m"] = int((5 * T["d"] + 2) / 153)
  T["d"] -= int((153 * T["m"] + 2) / 5) - 1
  T["H"] = sprintf("%.2d", int(T["S"] / 3600))
  if (T["H"] < 12) {
    T["p"] = "AM"
    T["J"]--
  } else
    T["p"] = "PM"
  T["S"] %= 3600
  T["M"] = sprintf("%.2d", int(T["S"] / 60))
  T["S"] %= 60
  T["m"] += 3
  if (T["m"] > 12) { T["Y"]++; T["m"] -= 12; T["j"] -= 365 }
  if (T["m"] > 2 && is_leap_year(T["Y"])) T["j"]++
  T["G"] = T["Y"]
  T["U"] = int((T["j"] - 1) / 7)
  T["W"] = T["U"]
  tmp = (T["j"] - 1) % 7
  if (tmp >= T["w"]) T["U"]++
  if (tmp >= (T["w"] + 6) % 7) T["W"]++
  T["V"] = T["W"]
  tmp = (T["j"] + 7 - T["w"]) % 7 # 1/1 week day as 0=Mo .. 6=Tu
  if (tmp > 3) T["V"]++
  if (T["V"] == 0) { # tmp is 1 (Su), 2 (Sa) or 3 (Fr)
    T["V"] = 52 + (tmp > 2 - is_leap_year(T["Y"] - 1))
    T["G"]--
  } else if (T["V"] == 53 && T["d"] - (T["w"] + 6) % 7 > 28) {
    T["V"] = 1
    T["G"]++
  }
  if (T["Y"] <= 0) T["Y"]-- # there's no year 0
  if (T["G"] <= 0) T["G"]-- # there's no year 0
  T["C"] = sprintf("%02.15g", int(T["Y"] / 100)) # -100 is Century -1
  T["y"] = sprintf("%.2d", T["Y"] % 100)
  T["g"] = sprintf("%.2d", T["G"] % 100)
  T["b"] = substr("JanFebMarAprMayJunJulAugSepOctNovDec", T["m"] * 3 - 2, 3)
  T["h"] = T["b"]
  T["B"] = fm[T["m"]]
  T["Y"] = sprintf("%04.15g", T["Y"])
  T["G"] = sprintf("%04.15g", T["G"])
  T["c"] = sprintf("%.3s %.3s%3d %.2d:%.2d:%.2d %s", \
		   T["a"], T["b"], T["d"], T["H"], T["M"], T["S"], T["Y"])
  T["e"] = sprintf("%2d", T["d"])
  T["d"] = sprintf("%.2d", T["d"])
  T["I"] = sprintf("%.2d", T["H"] % 12)
  T["j"] = sprintf("%.3d", T["j"])
  T["m"] = sprintf("%.2d", T["m"])
  T["S"] = sprintf("%.2d", T["S"])
  T["U"] = sprintf("%.2d", T["U"])
  T["V"] = sprintf("%.2d", T["V"])
  T["W"] = sprintf("%.2d", T["W"])
  T["D"] = T["m"] "/" T["d"] "/" T["y"]
  T["F"] = T["Y"] "-" T["m"] "-" T["d"]
  T["n"] = "\n"
  T["r"] = T["I"] ":" T["M"] ":" T["S"] " " T["p"]
  T["R"] = T["H"] ":" T["M"]
  T["t"] = "\t"
  T["T"] = T["H"] ":" T["M"] ":" T["S"]
  T["x"] = T["D"]
  T["X"] = T["T"]
  T["z"] = "+0000"
  T["Z"] = "GMT"
  T["%"] = "%"

  tmp=length(fmt); o=""
  for (i = 1; i <= tmp; i++) {
    c=substr(fmt,i,1)
    if (c == "%" && i < tmp) {
      c=substr(fmt, ++i, 1)
      if (c in T) c = T[c]
      else c="%" c
    }
    o = o c
  }
  return o
}


BEGIN {
  # usage: arg0 <format> <epoch>
  print wide_strftime(ARGV[1], ARGV[2])
}


-- 
St�phane                      ["Stephane.Chazelas" at "free.fr"]
0
Reply Stephane 11/10/2003 10:13:18 PM

In article <c37fd0bd.0311060635.6a859db8@posting.google.com>,
Gustavo Rios <gustavo.rios@terra.com.br> wrote:
>	I can not use any read-to-use functions (ANSI C/SUSV#), i.e., i do
>need to develop my own.

Why can't you?  This is comp.unix.programmer, so I presume you're doing
this on a Unix system, and they should all have strftime().

If this is a class assignment, then I think the purpose of the exercise is
for you to figure out how to compute the parts of the time.  In that case,
it would be totally cheating for one of us to give you the answer.

-- 
Barry Margolin, barry.margolin@level3.com
Level(3), Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
0
Reply Barry 11/10/2003 11:08:26 PM

8 Replies
490 Views

(page loaded in 0.004 seconds)

Similiar Articles:









7/22/2012 11:11:36 PM


Reply: