Author Topic: LInux get month  (Read 9889 times)

Offline dbfn

  • Jr. Member
  • *
  • Posts: 17
LInux get month
« on: February 02, 2010, 11:15:33 PM »
So,is it possible to get a month using the Unix Time?
If it's not or there's a better way,could you teach me?

Offline Keith Kanios

  • Full Member
  • **
  • Posts: 383
  • Country: us
    • Personal Homepage
Re: LInux get month
« Reply #1 on: February 03, 2010, 03:25:24 AM »
Are you looking to do this by manual calculations, or do you intend on using something like strftime()?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: LInux get month
« Reply #2 on: February 03, 2010, 03:56:19 AM »
Well... not directly (that I know of). Unix tends to tell you the time in seconds since the "dawn of time" - 1/1/70. To extract a month from that, you'll need to divide by 60 a couple of times, then by 24, then by 365. Figure out how many leapyears in that, and whether "this" is a leapyear. You'll need a table of days in the months, in which February is adjusted to 29 if it's a leapyear. If you want a name, not just a number, you'll need a table of month names, too.

Here (if I can figure out how to attach a file) is an example that spits out a string similar to C. (a "challenge" suggested by Ed Beroset, IIRC). I used three-letter names for the months (and day-of-week). This requires multiplying the index by three... and adding a space after. It would have been smarter to put the spaces in the table, multiply the index by four, and have the space, too (I think).

(I don't think this is a "homework question". If it is, and you turn this in as your own work, you will almost surely get busted. Plus, you won't learn anything!)

Questions and/or critiques welcome.

Best,
Frank


Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: LInux get month
« Reply #3 on: February 03, 2010, 04:17:36 AM »
strftime()? strftime()??? Oh yeah, dbfn is the "mixed C and asm" guy. Yeah, we could do it that way, too. Shouldn't be hard... although it does involve structures. :)

Of course, if we want to do it in a way in which somebody else has already written all the code, we could just type "date" and it'll tell us what month it is. :)

Best,
Frank


Offline dbfn

  • Jr. Member
  • *
  • Posts: 17
Re: LInux get month
« Reply #4 on: February 03, 2010, 10:25:15 PM »
Hum...I'd like to do it directly.
But anyway,Frank I got a light when you said about the leapyears and the "array".It's not difficult.I'm very dumb sometimes...: |
And...by the way...I'm 15.It's no homework.I do it as a hobby.

« Last Edit: February 03, 2010, 10:27:37 PM by dbfn »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: LInux get month
« Reply #5 on: February 05, 2010, 10:24:44 AM »
I do it as a hobby, too... and I'm 65.

Somewhat less "directly", here's a version that does the easy parts "directly" in asm (int 80h), and calls C for the hard parts. Spits out the month, as requested. "Mixed C and asm", for no good reason than to see if I can. This is not the usually recommended method to write programs for Linux!

Best,
Frank