How to Convert Ticks to a Date

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Email

Computers store the current time in terms of the number of "ticks" that have elapsed since a certain date, known as the beginning of the "epoch." A tick is a metaphor for the ticking sound made by an analogue clock every second, however, the length of a tick varies for each operating system. Unix systems have a one-second tick, while Windows systems use a 100-nanosecond tick and Mac OS X systems have a one-millisecond tick. Similarly, different systems use different dates to mark the beginning of their epoch. For Windows, that date is Jan. 1, 1601, at precisely midnight UT/GMT. For Unix and Mac OS X, the epoch begins on Jan. 1, 1970, at precisely midnight UT/GMT. You can write a simple program in the free language Ruby that will take a number of ticks and compute the current date from them.

Skill level:
Moderately Easy

Other People Are Reading

Things you need

  • Ruby

Show MoreHide

Instructions

  1. 1

    Open a text editor and save the document with the name "tick2time.rb."

  2. 2

    Paste the following code within it:

    !/usr/bin/ruby

    ticks = Integer(ARGV[1])

    if ARGV[0] == "--s":

    # do nothing
    

    elsif ARGV[0] == "--ms":

    ticks = ticks / 1000
    

    elsif ARGV[0] == "--ns":

    ticks = ticks / 1000000
    

    end

    puts Time.at(ticks)

    The first line is only for users of Mac OS X or Linux: it should identify the location where Ruby is installed. The program takes the number of ticks from the command line along with a "tick length" argument that can be set to "--s" for second, "--ms" for millisecond or "--ns" for nanosecond. Whatever the tick length is, it is adjusted to the second-length tick that Ruby expects for its Time function.

  3. 3

    Save your work.

Tips and warnings

  • The epoch is the epoch for the current operating system. So, "tick2time.rb --s 0" will give different results on Windows and Unix computers, since each computer begins its epoch at drastically different times.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.