2

I want to implement Unix milliseconds converter to human readable date format. using this post https://stackoverflow.com/a/1657307/1979882 I found I can receive such string but I can't setup a time zone inside the code. The code uses only system time zone.

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

int main ( int argc, char *argv[] )
{
    time_t now;
    struct tm *lcltime;

    now = time ( NULL );
    lcltime = localtime ( &now );

    printf ( "The time is %d:%d\n", lcltime->tm_hour, lcltime->tm_min );

    return 0;
}

Is it possible to customize it?

EDIT

I need some Java-like method:

SimpleDateFormat df= new SimpleDateFormat("dd MMM hh:mm z",Locale.ENGLISH);
df.setTimeZone("Europe/London");
System.out.println("London Time " + df.format(System.currentTime()));
df.setTimeZone("Asia/Benjin");
System.out.println("Benjin Time " + df.format(System.currentTime()));

EDIT_2

I found I can setup time zone using setenv(...) url https://stackoverflow.com/a/1620451/1979882

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

int main (int argc, char *argv[])
{
    struct tm *mt;
    time_t mtt;
    char ftime[10];

    setenv("TZ", "PST8PDT", 1);
    tzset();
    mtt = time(NULL);
    mt = localtime(&mtt);
    strftime(ftime,sizeof(ftime),"%Z %H%M",mt);

    printf("%s\n", ftime);
}

But in changes the system time zone. I need just time zone modification only inside my code.

Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • Can you be more specific what you want to do? `localtime` returns a time that in local time zone as the system. – fluter May 02 '16 at 07:20
  • I'm somewhat surprised [`strftime`](http://en.cppreference.com/w/c/chrono/strftime) doesn't fit the bill for what you're trying to do, since it can be used for format time and dates to look like damn near anything one can logically (and illogically) conceive. – WhozCraig May 02 '16 at 07:23
  • 2
    Look just above *Program to print the current formatted Greenwich Mean Time* on the page you reference. It should explain setting the environment for a localtime different that already defined for your system. Recall, `localtime` makes a call to `tzset` and will calculate a time value based on your current system config. If you want to check the offset from UTC, then call `gmtime` instead. – David C. Rankin May 02 '16 at 07:24
  • I've updated my post to clear my question. – Vyacheslav May 02 '16 at 07:28

1 Answers1

1

You can change the argument to localtime based on the timezone difference required.

now += (time_t)(3600*TIME_DIFF_IN_HRS)
lcltime = localtime ( &now );

To be totally portable, you can get the current time zone by referring this post

Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
  • is it possible to know `TIME_DIFF_IN_HRS` for a particular time zone? Or I have to predefine it? – Vyacheslav May 02 '16 at 07:43
  • That depends on the time zone you want to display and the time zone you are in. e.g. If you are in India and you want to display GMT, this will be -5.5. – Rishikesh Raje May 02 '16 at 07:46