# Seconds Since the “Epoch”

I was supposed to write a RT (Real Time) logging which doesn’t call a single Linux CALL.
I had only seconds from 1st Jan 1970 (Called Eposh).

A value that approximates the number of seconds that have elapsed since the Epoch. A Coordinated Universal Time name (specified in terms of seconds (tm_sec), minutes (tm_min), hours (tm_hour), days since January 1 of the year (tm_yday), and calendar year minus 1900 (tm_year)) is related to a time represented as seconds since the Epoch, according to the expression below.

If the year is <1970 or the value is negative, the relationship is undefined. If the year is>=1970 and the value is non-negative, the value is related to a Coordinated Universal Time name according to the C-language expression, where tm_sec,  tm_min,  tm_hour,  tm_yday,  and  tm_year are all integer types:

```tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
```

The relationship between the actual time of day and the current value for seconds since the Epoch is unspecified.

How any changes to the value of seconds since the Epoch are made to align to a desired relationship with the current actual time is implementation-defined. As represented in seconds since the Epoch, each and every day shall be accounted for by exactly 86400 seconds.

Note:
The last three terms of the expression add in a day for each year that follows a leap year starting with the first leap year since the Epoch. The first term adds a day every 4 years starting in 1973, the second subtracts a day back out every 100 years starting in 2001, and the third adds a day back in every 400 years starting in 2001. The divisions in the formula are integer divisions; that is, the remainder is discarded leaving only the integer quotient.

You can convert epoch Seconds to current time please look at this LINK.

# Xenomai Timer

Xenomai Timer :Xenomai has two time sources: the sytem timer, which counts the number of nanoseconds since 1970, and a hardware dependent high resolution counter which counts the time since an unspecified point in time (usually the system boot time). This hardware dependent high resolution counter is called “tsc” on a PC, and gave its name to Xenomai native API calls.rt_timer_tsc returns the value of this hardware dependent high-resolution counter.
rt_timer_info returns the same thing in the tsc member of the RT_TIMER_INFO structure, and the value of the system timer at exactly the same time as when the high-resolution counter was read.

This allows to have a correspondence between the two time sources.

rt_alarm_inquire is not related to this and returns some information
about a given alarm. Now, if you allow me, a little advice for the implementation of a “timer library”: you could be tempted to create only one periodic alarm object with Xenomai, and to manage a timer list yourself. Don’t do this. Creating an alarm object for each timer library object make Xenomai aware of the existence of all your application timers, this has several

– it allows Xenomai to use its anticipation algorithm for all your timers
– if you are concerned about the scalability of Xenomai timers list
management, you can check the options in the “Scalability” menu of

Xenomai POSIX skin supports two clocks:
CLOCK_REALTIME maps to the nucleus system clock, keeping time as the amount of time since the Epoch, with a resolution of one system clock tick.

CLOCK_MONOTONIC maps to an architecture-dependent high resolution counter, so is suitable for measuring short time intervals. However, when used for sleeping (with clock_nanosleep()), the CLOCK_MONOTONIC clock has a resolution of one system clock tick, like the CLOCK_REALTIME clock.[1]

# Date difference with Time in (Perl)

#### time syntax same as above

``` sub validateDateFormat{ \$regexmmddyyyy='^(0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])[- /.]((?:19|20|21)\d\d)\$'; \$regexddmmyyyy='^(0[1-9]|[12][0-9]|3[01])[-/.](0[1-9]|1[012])[- /.]((?:19|20|21)\d\d)\$'; \$regexyyyymmdd='^((?:19|20|21)\d\d)[- /.](0[1-9]|1[012])[-/.](0[1-9]|[12][0-9]|3[01])\$'; \$date=shift; \$format = shift; if(\$format eq 'ddmmyyyy'){ if(eval \$date=~\$regexddmmyyyy){ \$date = \$1; \$month = \$2; \$year = \$3; if(isvaliddate(\$date,\$month,\$year) eq "true"){ return \$year.\$month.\$date; # Valid date }else{ return "false"; # Not a valid date } } }elsif(\$format eq 'mmddyyyy'){ if(eval \$date=~\$regexmmddyyyy){ \$date = \$2; \$month = \$1; \$year = \$3; if(isvaliddate(\$date,\$month,\$year) eq "true"){ return \$year.\$month.\$date; # Valid date }else{ return "false"; # Not a valid date } } }elsif(\$format eq 'yyyymmdd'){ if(eval \$date=~\$regexyyyymmdd){ \$date = \$3; \$month = \$2; \$year = \$1; if(isvaliddate(\$date,\$month,\$year) eq "true"){ return \$year.\$month.\$date; # Valid date }else{ return "false"; # Not a valid date } } }else{ return "false"; # Not a valid date } } ## HH:MM:SS is allowed only sub isvalidtime{ \$time = shift; \$regexhhmmss='^(0[1-9]|1[0-9]|2[0-3])[:](0[1-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])[:](0[1-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9])\$'; if(eval \$time=~\$regexhhmmss){ return "true"; }else{ return "false"; } } sub isvaliddate{ \$date = shift; \$month = shift; \$year = shift; #\$date #\$month #\$year if (\$date == 31 and (\$month == 4 or \$month == 6 or \$month == 9 or \$month == 11)) { return "false"; # 31st of a month with 30 days }elsif (\$date >= 30 and \$month == 2) { return "false"; # February 30th or 31st } elsif (\$month == 2 and \$date == 29 and not (\$year % 4 == 0 and (\$year % 100 != 0 or \$year % 400 == 0))) { return "false"; # February 29th outside a leap year } else { return "true"; # Valid date } } sub validateDateDifference{ \$startdate = shift; \$enddate = shift; \$format =shift; # Trimming dates and time " 20-01-2013 12.58.1 " --> "20-01-2013 12.59.32" \$startdate =~ s/^\s+|\s+\$//g; \$enddate =~ s/^\s+|\s+\$//g; \$format=~ s/^\s+|\s+\$//g; (\$startdate, \$startdate_time) = split(/ +/, \$startdate); (\$enddate, \$enddate_time) = split(/ +/, \$enddate); \$startdate = validateDateFormat(\$startdate,\$format); if(\$startdate eq "false"){ return "false"; # invalid date difference } \$enddate = validateDateFormat(\$enddate,\$format); if(\$enddate eq "false"){ return "false"; # invalid date difference } #converting into YYYYMMDD format if(\$enddate == \$startdate ){```

``` ```

``` if(isvalidtime(\$startdate_time) eq "false"){ print "reached"; return "false"; } if(isvalidtime(\$enddate_time) eq "false"){ print "reached"; return "false"; } \$startdate_time =~ s/://g; \$enddate_time =~ s/://g; if(\$startdate_time < \$enddate_time){ return "true"; # Valid date difference }else{ return "false"; # invalid date difference } } elsif(\$enddate > \$startdate ){ return "true"; # Valid date difference }else{ return "false"; # invalid date difference } } print 'validateDateDifference("12.12.2013 12:12:58","12.12.2013 12:12:59","ddmmyyyy")\t'.validateDateDifference("12.12.2013 12:12:58","12.12.2013 12:12:59","ddmmyyyy")."\n"; print 'validateDateDifference("12.12.2014 12:12:58","12.12.2014 12:12:12","ddmmyyyy")\t'.validateDateDifference("12.12.2014 12:12:58","12.12.2014 12:12:12","ddmmyyyy")."\n"; print 'validateDateDifference("12.12.2013 12:12:58","12.12.2013 12:12:59","mmddyyyy")\t'.validateDateDifference("12.12.2013 12:12:58","12.12.2013 12:12:59","mmddyyyy")."\n"; print 'validateDateDifference("12.12.2013 12:12:58","12.12.2013 12:12:59","mmmmyyyy")\t'.validateDateDifference("12.12.2013 12:12:58","12.12.2013 12:12:59","mmmmyyyy")."\n"; print 'validateDateDifference("2013-01-31 12:12:58","2013-12-02 12:12:59","yyyymmdd")\t'.validateDateDifference("2013-01-31 12:12:58","2013-12-02 12:12:59","yyyymmdd")."\n"; print 'validateDateDifference("2013-01-31 12:12:58","2013-12-02 12:12:59","yyyymmdd")\t'.validateDateDifference("2013-01-31 12:12:58","2013-12-02 12:12:59","yyyymmdd")."\n"; ```

# Timers in NS2

If you are learning ns2, timers are often an obstacle. They concern with the ability of calling classes methods in consequence of an event schedule. The difficult raises when trying to find out in which order methods are called. If timers are involved, you can’t expect to find nested calls of methods until the one you are interested in. In this section it is explained how to understand this behavior.

A TimeHandler, as name suggests, is an object created to manage time, and is defined in the common/timer-handler(.cc,.h) files of your ns2 distribution. This handler is in charge to schedule the execution of events during the simulation. The time the handler refers to is the simulation time, not depending on the time your machine uses to process the code.
A timer is like a state machine and is charachterized by three states. The first is `TIMER_IDLE` which means that timer can manage an event. The next state is `TIMER_PENDING`, describing a status in which the timer is waiting to manage an event, and so it can’t accept new events. This means that the timer has accepted to manage an event to be scheduled in the future and it is busy, waiting for this event. The last state is `TIMER_HANDLING` in which the event is processed, calling the `expire()` function. After processing the event, timer returns in idle state.
In cbr/cbr-module(.cc,.h) of the MIRACLE distribution you can find a simple timer example, exploited to manage CBR packets transmission.
In the header file we define a new TimerHandler object. It is NOT possible to use TimerHandler class as is, because each timer has to handle its events in a particular fashion, which depends on the content of `expire()` function.

```/**
* Timer used by CbrModule to schedule packet transmission
*/
class SendTimer : public TimerHandler
{
public:
SendTimer(CbrModule *m) : TimerHandler() { module = m; }```

protected:
virtual void expire(Event *e);
CbrModule* module;
};

We defined the constructor, the `expire()` function and a reference to the module the timer refers to.
On the other side, when defining CbrModule, we have to add `SendTimer_` as a friend class, in order to make it able to properly manage events. In this case `expire()` function has to call a CbrModule private method to activate packet sending processes.

`  friend class SendTimer;`

It is also necessary to define (in CbrModule) the timer itself, to have a reference to the object to whom submit events.

`  SendTimer sendTmr_;  /*timer which schedules packet transmissions*/`

In CbrModule.cc file, when you define the module constructor you have also to activate Timer constructor. This one has a reference to this to definitively associate `SendTimer` to this `CbrModule`. If you look at the definition of timer constructor, you’ll see that it only associates a CbrModule to the object (the one which submits events) to be managed by the timer.

```CbrModule::CbrModule()
: sendTmr_(this),```

At the top of the same file there is the definition of `expire()` function. When timer switchs to the `TIMER_HANDLING` state, it refers to this particular set of actions. When a CbrModule event expires, the function `transmit()` is called. Note that this function is defined in `CbrModule`, so Timer class has to be declaredas a friend of Module one. The `module` attribute refers to `CbrModule`, as explained before (see the constructor).

```void SendTimer::expire(Event *e)
{
module->transmit();
}```

Now we are ready to schedule an event. `TimerHandler` class offers two methods to do this. They work in a similar fashion: both of them schedule an event but, while one is made to be called occasionally (i.e., when the timer is not busy, `TIMER_IDLE`), the second is created to work in an always pending status, in order to consecutively recall `expire()` function. If timer is always in `TIMER_PENDING` status, no other (sporadic) events can be managed by the timer. The former is called `sched()` and the latter is called `resched()`. The `sched()` function works only if timer is `TIMER_IDLE` while `resched()` can potentially cancel a previously scheduled `TIMER_PENDING` event and to schedule its new event. In this way the timer is maintaned always busy for those methods which don’t call `resched()` function. If your method instead, has the privileged `resched()` call you can access and modify the events.
In `CbrModule` we want to periodically transmit a packet. To achieve this result we use `resched()` function.

```void CbrModule::start()
{
...
sendTmr_.resched(period_);
}```

At the end of `transmit()` method we reschedule again an event in order to create a “self-updating” loop. In this case, at the end of expire function, we schedule the timer for next `period_` instants, maintaining the loop.

Thanx to ns-miracles for this post